Search code examples
eclipse-jdt

Eclipse JDT Tutorials


This is a follow up question to Programatically writing Java

I am looking at JDT to construct a standalone app (not an eclipse plugin) to programatically write JUnit test classes.

I'd like to know if what I am intending is possible.

Additionally I'd like to know of some tutorials to get me started, the tutorial posted in my last question seems a little to advanced for me.


Solution

  • I found some code that uses the ASTParser standalone. This may help. You will need to add the following jars to your project. I cut and pasted from the .classpath file.

            <classpathentry kind="lib" path="/Applications/eclipse 3.6/plugins/org.eclipse.osgi_3.6.1.R36x_v20100806.jar"/>
        <classpathentry kind="lib" path="/Applications/eclipse 3.6/plugins/org.eclipse.core.contenttype_3.4.100.v20100505-1235.jar"/>
        <classpathentry kind="lib" path="/Applications/eclipse 3.6/plugins/org.eclipse.core.jobs_3.5.1.R36x_v20100824.jar"/>
        <classpathentry kind="lib" path="/Applications/eclipse 3.6/plugins/org.eclipse.core.resources_3.6.0.R36x_v20100825-0600.jar"/>
        <classpathentry kind="lib" path="/Applications/eclipse 3.6/plugins/org.eclipse.core.runtime_3.6.0.v20100505.jar"/>
        <classpathentry kind="lib" path="/Applications/eclipse 3.6/plugins/org.eclipse.equinox.common_3.6.0.v20100503.jar"/>
        <classpathentry kind="lib" path="/Applications/eclipse 3.6/plugins/org.eclipse.equinox.preferences_3.3.0.v20100503.jar"/>
        <classpathentry kind="lib" path="/Applications/eclipse 3.6/plugins/org.eclipse.jdt.junit.core_3.6.1.r361_v20100825-0800.jar"/>
        <classpathentry kind="lib" path="/Applications/eclipse 3.6/plugins/org.eclipse.jdt.core_3.6.1.xx-20101215-2100-e36.jar"/>
    

    And here is the test code that I found (I cleaned up a few warnings):

    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 TestAstParser {
        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.setKind(ASTParser.K_COMPILATION_UNIT);
            final CompilationUnit cu = (CompilationUnit) parser.createAST(null);
            cu.accept(new ASTVisitor() {
                Set<String> names = new HashSet<String>();
    
                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;
                }
            });
        }
    }
    

    The original post can be found here: http://www.programcreek.com/2011/01/a-complete-standalone-example-of-astparser/