eclipse-jdt - Eclipse JDT 教程

标签 eclipse-jdt

这是对 Programatically writing Java 的后续问题

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

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

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

最佳答案

我发现了一些独立使用 ASTParser 的代码。这可能会有所帮助。您需要将以下 jars 添加到您的项目中。我从 .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/

关于eclipse-jdt - Eclipse JDT 教程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5423052/

相关文章:

eclipse - 编写 Eclipse 插件时,检查 IEditorPart 是否是 Java 编辑器的正确方法是什么?

java - JDT : Nesting MethodInvocation

java - 代码生成的简单 JDT 示例

java - 为什么我的 AST TypeDeclaration 缺少其方法和字段?

java - 如何检测 Eclipse 编译错误(Java - JDT)

Eclipse插件开发: Discouraged access: The type EclipseStarter is not accessible due to restriction on required library

java - 如何使用 Eclipse JDT ASTParser 获取方法的类名?

java - Eclipse JDT Java Parser 中是否有任何类型的 "line visitor"?如果没有,有人知道一个好的解决方法吗?

java - 将 pom.xml 与父级合并

Eclipse 在尝试重构方法的签名时出错