如何在Eclipse之外的项目中使用Java Eclipse抽象语法树? (即不是eclipse插件) [英] How can I use the java Eclipse Abstract Syntax Tree in a project outside Eclipse? (ie not an eclipse plugin)

查看:127
本文介绍了如何在Eclipse之外的项目中使用Java Eclipse抽象语法树? (即不是eclipse插件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Eclipse之外的项目中使用java Eclipse抽象语法树? (即不是eclipse插件)

How can I use the java Eclipse Abstract Syntax Tree in a project outside Eclipse? (ie not an eclipse plugin)

我看过的所有Eclipse AST示例都是用于eclipse插件。有一种方法(即一个例子),一个使用eclipse AST的非eclipse项目的项目。

All the Eclipse AST examples that I've seen are for eclipse plugins. Is there a way (ie an example) of a project that uses the eclipse AST for a non-eclipse project.

推荐答案

以下是给定Java 1.5文件的代码。我很新,花了今天浏览一下,并尝试将代码放在下面。

Below is the code I used to do this given a Java 1.5 file. I'm very new to this and spent today browsing around, and trying things out to get the code below working.

public void processJavaFile(File file) {
    String source = FileUtils.readFileToString(file);
    Document document = new Document(source);
    ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setSource(document.get().toCharArray());
    CompilationUnit unit = (CompilationUnit)parser.createAST(null);
    unit.recordModifications();

    // to get the imports from the file
    List<ImportDeclaration> imports = unit.imports();
    for (ImportDeclaration i : imports) {
        System.out.println(i.getName().getFullyQualifiedName());
    }

    // to create a new import
    AST ast = unit.getAST();
    ImportDeclaration id = ast.newImportDeclaration();
    String classToImport = "path.to.some.class";
    id.setName(ast.newName(classToImport.split("\\.")));
    unit.imports().add(id); // add import declaration at end

    // to save the changed file
    TextEdit edits = unit.rewrite(document, null);
    edits.apply(document);
    FileUtils.writeStringToFile(file, document.get());

    // to iterate through methods
    List<AbstractTypeDeclaration> types = unit.types();
    for (AbstractTypeDeclaration type : types) {
        if (type.getNodeType() == ASTNode.TYPE_DECLARATION) {
            // Class def found
            List<BodyDeclaration> bodies = type.bodyDeclarations();
            for (BodyDeclaration body : bodies) {
                if (body.getNodeType() == ASTNode.METHOD_DECLARATION) {
                    MethodDeclaration method = (MethodDeclaration)body;
                    System.out.println("name: " + method.getName().getFullyQualifiedName());
                }
            }
        }
    }
}

这需要以下库:

commons-io-1.4.jar
org.eclipse.jdt.core_xxxx.jar
org.eclipse.core.resources_xxxx.jar
org.eclipse.core.jobs_xxxx.jar
org.eclipse.core.runtime_xxxx.jar
org.eclipse.core.contenttype_xxxx.jar
org.eclipse.equinox.common_xxxx.jar
org.eclipse.equinox.preferences_xxxx.jar
org.eclipse.osgi_xxxx.jar
org.eclipse.text_xxxx.jar

这篇关于如何在Eclipse之外的项目中使用Java Eclipse抽象语法树? (即不是eclipse插件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆