Clone wiki

ppa / 2-Minute_Tutorial

2-Minute Tutorial

This tutorial shows how to get a typed CompilationUnit instance from a single Java file (potentially referring to other unavailable source files). We only give the high-level details here. For a complete tutorial, see the PPA Tutorial.

This example assumes that you are in a plug-in project that imports the following plug-in: org.eclipse.jdt.core. The example also assumes that the plug-in fragment ca.mcgill.cs.swevo.ppa is loaded (this is done automatically if you import jdt.core and you have installed PPA for Eclipse).

All PPA plug-ins include the source code so you can browse the code and the API using Eclipse (either Ctrl-Click on a type or use the type dialog using Ctrl-T).

import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.IBinding;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.Name;
import ca.mcgill.cs.swevo.ppa.PPAOptions;
import ca.mcgill.cs.swevo.ppa.util.PPACoreUtil;

public class PPAClient {

  // This must be called in an Eclipse plug-in, not in a 
  // standalone Java application.
  public void usePPA() {
    // The source file.
    File srcFile = new File("/tmp/MyJavaFile.java");
    
    // Obtaining a compilation unit using the default options.
    CompilationUnit cu = PPACoreUtil.getCU(file, new PPAOptions());
    
    // Walk through the compilation unit.
    ...

    // Obtain a name object and the corresponding binding
    Name nameNode = ...
    IBinding binding = nameNode.resolveBinding();
    ITypeBinding typeBinding = nameNode.resolveTypeBinding();
  }
}

Updated