How to use eclipse AST parser to parse java source code; both in a java standalone application, and in an eclipse plugin.
Keywords: Eclipse, AST, Parser
Abstract: AST parsing is needed when you want to make some kind of elaboration over source code. This article introduces the the Eclipse AST parser, make a simple example, and show hot to build a simple standalone application that uses it. At the end we build a relly simple eclipse plugin that uses the Eclipse AST parser.
Eclipse AST Parser
AST : tree representation of the abstract synctactic structure of the source code.
Eclipse AST Parser is a Parser class, used inside Eclipse, to compile source code, and produce the AST of java source code.
Below there is an example of the code needed to Obtain
// source is a string with the Java source String source = IO.readFile(srcFile.toString()); // JLS4 is available from Eclipse 3.7.2+ ASTParser parser = ASTParser.newParser(AST.JLS4); parser.setKind(ASTParser.K_COMPILATION_UNIT); parser.setSource(source.toCharArray()); // creates Syntax Tree CompilationUnit node = (CompilationUnit) parser.createAST(null); // instantiate your Visitor class MyASTVisitor visitor = new MyASTVisitor(); // starts the visit node.accept(visitor);
The visitor is a class, implementing the Visitor
design pattern, that is able to descend a full AST Syntax Tree and perform operations node by node. Below there is an example
/** * Visitor that retrieves informations from the {@link CompilationUnit} */ public class MyASTVisitor extends ASTVisitor { /** called when visitor walk in the main AST node */ @Override public boolean visit(CompilationUnit node) { System.out.println(" The Compilation Unit node: " + node); } /** called when Visitor walk in a "Method Declaration" */ @Override public boolean visit(MethodDeclaration node) { System.out.println(" - A method declaration node: " + node); } }
AST Parser inside an Eclipse Plug-in
The easy way to run your AST parser is to create an Eclipse Plugin; include org.eclipse.jdt
dependency from your plugin.xml, then write your java code.
AST Parser in a standalone java application
To use the AST parser in a standalone java application you need simply to import some jars. You can find these jars into the Eclipse/plugins
folder. Below there is the list of jars I use, divided into libraries needed at compile time and at runtime.
libraries needed a compile-time:
- org.eclipse.equinox.common_3.6.100.v20120522-1841.jar
- org.eclipse.jdt.core_3.8.1.v20120523-1356.jar
libraries needed at runtime:
- org.eclipse.core.contenttype_3.4.200.v20120523-2004.jar
- org.eclipse.core.jobs_3.5.200.v20120521-2346.jar
- org.eclipse.core.resources_3.8.0.v20120511-1410.jar
- org.eclipse.core.runtime_3.8.0.v20120521-2346.jar
- org.eclipse.equinox.common_3.6.100.v20120522-1841.jar
- org.eclipse.equinox.preferences_3.5.0.v20120522-1841.jar
- org.eclipse.jdt.core_3.8.1.v20120523-1356.jar
- org.eclipse.osgi_3.8.0.v20120522-1813.jar
Note the version of plugins depends on the Eclipse version you are using. In my case I’m developing over an Eclipse 4.2