使用 Eclipse AST

发布于 2024-09-12 03:12:14 字数 324 浏览 9 评论 0原文

我最近需要修改一些Java代码(添加方法、更改某些字段的签名和删除方法),我认为所有这些都可以通过使用Eclipse SDK的AST来完成。

我从一些研究中知道如何解析源文件,但我不知道如何执行上述操作。有谁知道一个好的教程或者有人可以给我一个关于如何解决这些问题的简短解释吗?

非常感谢,

ExtremeCoder


编辑:

我现在开始更多地研究 JCodeModel,我认为这可能更容易使用,但我不知道是否可以将现有文档加载到其中?

如果这可行,请告诉我;)

再次感谢。

I have recently come into the need of modifying some Java code (adding methods, changing the signatures of some fields and removing methods) and I think that all of this can be accomplished though the use of the Eclipse SDK's AST.

I know from some research how to parse in a source file but I don't know how to do the things mentioned above. Does anyone know a good tutorial or could someone give me a brief explanation on how to resolve these issues?

Thanks a lot,

ExtremeCoder


Edit:

I have now started to look more into the JCodeModel and I think this could be much easier to use but I do not know if an exististing document can be loaded into it?

If this could work let me know ;)

Thanks again.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悲喜皆因你 2024-09-19 03:12:15

我不会在这里发布这个问题的整个源代码,因为它很长,但我会让人们开始。

您需要的所有文档都在这里: http://publib.boulder.ibm.com/infocenter/iadthelp/v6r0/index.jsp?topic =/org.eclipse.jdt.doc.isv/reference/api/org/eclipse/jdt/core/dom/package-summary.html

Document document = new Document("import java.util.List;\n\nclass X\n{\n\n\tpublic void deleteme()\n\t{\n\t}\n\n}\n");
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setSource(document.get().toCharArray());
CompilationUnit cu = (CompilationUnit)parser.createAST(null);
cu.recordModifications();

这将从您传递的源代码中为您创建一个编译单元现在

这是一个简单的函数,它打印出您所传递的类定义中的所有方法:

List<AbstractTypeDeclaration> types = cu.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("method declaration: ");
                System.out.println("name: " + method.getName().getFullyQualifiedName());
                System.out.println("modifiers: " + method.getModifiers());
                System.out.println("return type: " + method.getReturnType2().toString());
            }
        }
    }
}

这应该可以让您开始。

确实需要一些时间来适应这一点(就我而言需要花费很多时间)。但它确实有效,并且是我能掌握的最好方法。

祝你好运;)

ExtremeCoder


编辑:

在我忘记之前,这些是我用来让它工作的导入(我花了相当多的时间来组织这些):

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

其中 xxxx 代表版本号。

I will not post the whole source code to this problem here because it is quite long but I will get people started.

All the docs that you will need are here: http://publib.boulder.ibm.com/infocenter/iadthelp/v6r0/index.jsp?topic=/org.eclipse.jdt.doc.isv/reference/api/org/eclipse/jdt/core/dom/package-summary.html

Document document = new Document("import java.util.List;\n\nclass X\n{\n\n\tpublic void deleteme()\n\t{\n\t}\n\n}\n");
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setSource(document.get().toCharArray());
CompilationUnit cu = (CompilationUnit)parser.createAST(null);
cu.recordModifications();

That will create a compilation unit for you from the source code that you pass in.

Now this is a simple function that prints out all of the methods inside the class definitions in what you have passed:

List<AbstractTypeDeclaration> types = cu.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("method declaration: ");
                System.out.println("name: " + method.getName().getFullyQualifiedName());
                System.out.println("modifiers: " + method.getModifiers());
                System.out.println("return type: " + method.getReturnType2().toString());
            }
        }
    }
}

This should get you all started.

It does take some time to get used to this (a lot in my case). But it does work and is the best method I could get my hands on.

Good luck ;)

ExtremeCoder


Edit:

Before I forget, these are the imports that I used to get this working (I took quite a bit of time to get these organized):

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

Where xxxx represents a version number.

请远离我 2024-09-19 03:12:15

您可以使用 Eclipse 通过调用可操作 AST 的 API 来完成此操作。

或者,您可以应用程序转换来以不依赖于 AST 微观细节的方式实现您的效果。

例如,您可以编写以下程序转换:

add_int_parameter(p:parameter_list, i: identifier): parameters -> parameters
  " \p " -> " \p , int \i";

将具有任意名称的整数参数添加到参数列表中。这实现了与一整套 API 调用相同的效果,但它更具可读性,因为它采用您的语言(在本例中为 Java)的表面语法。

我们的DMS Software Reengineering Toolkit可以接受这样的程序转换并将其应用于多种语言,包括 Java。

You can do this with Eclipse by calling APIs that let you manipulate the ASTs.

Or you can apply program transformations to achieve your effect in way that doesn't depend on the microscopic details of the AST.

As an example you might write the following program transformation:

add_int_parameter(p:parameter_list, i: identifier): parameters -> parameters
  " \p " -> " \p , int \i";

to add an integer parameter with an arbitrary name to a parameter list. This achieves the same effect as a whole set of API calls but it is a lot more readable because it is in the surface syntax of your language (in this case, Java).

Our DMS Software Reengineering Toolkit can accept such program transformations and apply them to many languages, including Java.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文