Eclipse JDT教程

时间:2011-03-24 17:24:32

标签: eclipse-jdt

这是Programatically writing Java

的后续问题

我正在寻找JDT来构建一个独立的应用程序(不是eclipse插件)来以编程方式编写JUnit测试类。

我想知道我的意图是否可行。

此外,我想了解一些让我入门的教程,我上一期提出的教程似乎对我来说有点先进。

1 个答案:

答案 0 :(得分:6)

我发现了一些使用ASTParser独立的代码。这可能有所帮助。您需要将以下jar添加到项目中。我剪切并粘贴了.classpath文件。

        <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"/>

这是我发现的测试代码(我清理了一些警告):

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;
            }
        });
    }
}

可以在此处找到原始帖子:http://www.programcreek.com/2011/01/a-complete-standalone-example-of-astparser/