A complete standalone example of ASTParser
This example belongs to Eclipse JDT Tutorial Series.
Normally, Eclipse JDT is used in a plug-in environment. This is the normal situation in which we deal with each project in the workspace. However, if we want to parse a large amount of java file, this approach may not be good, since it is not easy to import many projects to workspace manually. (You may be able to do this automatically, yet that’s another story)
Can we use Eclipse’s ASTParser to parse java source code in a standalone application, instead of a plug-in? I asked the same question, before I found the solution. Here is a complete code example which ASTParser works in a standalone application.
import java.util.HashSet; import java.util.Set; import org.eclipse.jdt.core.dom.AST; import org.eclipse.jdt.core.dom.ASTParser; import org.eclipse.jdt.core.dom.ASTVisitor; import org.eclipse.jdt.core.dom.CompilationUnit; import org.eclipse.jdt.core.dom.SimpleName; import org.eclipse.jdt.core.dom.VariableDeclarationFragment; public class Test { public static void main(String args[]){ ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setSource("public class A { int i = 9; \n int j; \n ArrayList<Integer> al = new ArrayList<Integer>();j=1000; }".toCharArray()); //parser.setSource("/*abc*/".toCharArray()); parser.setKind(ASTParser.K_COMPILATION_UNIT); //ASTNode node = parser.createAST(null); final CompilationUnit cu = (CompilationUnit) parser.createAST(null); cu.accept(new ASTVisitor() { Set names = new HashSet(); public boolean visit(VariableDeclarationFragment node) { SimpleName name = node.getName(); this.names.add(name.getIdentifier()); System.out.println("Declaration of '"+name+"' at line"+cu.getLineNumber(name.getStartPosition())); return false; // do not continue to avoid usage info } public boolean visit(SimpleName node) { if (this.names.contains(node.getIdentifier())) { System.out.println("Usage of '" + node + "' at line " + cu.getLineNumber(node.getStartPosition())); } return true; } }); } }
It will print out the following message:
Declaration of 'i' at line1 Declaration of 'j' at line2 Declaration of 'al' at line3
All problems, such as “The type org.eclipse.core.runtime.IProgressMonitor cannot be resolved. It is indirectly referenced from required .class files”, etc, are caused by missing libraries.
Here is the jar files required:
org.eclipse.core.contenttype.jar
org.eclipse.core.jobs.jar
org.eclipse.core.resources.jar
org.eclipse.core.runtime.jar
org.eclipse.equinox.common.jar
org.eclipse.equinox.preferences.jar
org.eclipse.jdt.core.jar
org.eclipse.osgi.jar
The following are what I used for this sample program.

These jar files will be under plugins directory, once you installed astview plugin. Here is post about how to add astview plugin. For convenience, I also made a rar file which includes all required jar files. You can download it directly here.
This is another article that might be useful for you to understand AST.
Related Articles:
-
http://www.ic.uff.br/~gmenezes Gleiph
-
http://www.ashwinjayaprakash.com/ Ashwin Jayaprakash
-
abdul
-
Fabricio
-
http://- spyros
-
Fabricio
-
Fabricio
-
sagar
-
sagar
-
giri