4

这是以编程方式编写 Java的后续问题

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

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

此外,我想知道一些教程来帮助我入门,我最后一个问题中发布的教程对我来说似乎有点高级。

4

1 回答 1

7

我发现了一些独立使用 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/

于 2011-04-24T02:24:44.037 回答