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

查看:30
本文介绍了如何在 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天全站免登陆