使用 Eclipse AST
我最近需要修改一些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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不会在这里发布这个问题的整个源代码,因为它很长,但我会让人们开始。
您需要的所有文档都在这里: 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
这将从您传递的源代码中为您创建一个编译单元现在
这是一个简单的函数,它打印出您所传递的类定义中的所有方法:
这应该可以让您开始。
确实需要一些时间来适应这一点(就我而言需要花费很多时间)。但它确实有效,并且是我能掌握的最好方法。
祝你好运;)
ExtremeCoder
编辑:
在我忘记之前,这些是我用来让它工作的导入(我花了相当多的时间来组织这些):
其中 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
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:
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):
Where xxxx represents a version number.
您可以使用 Eclipse 通过调用可操作 AST 的 API 来完成此操作。
或者,您可以应用程序转换来以不依赖于 AST 微观细节的方式实现您的效果。
例如,您可以编写以下程序转换:
将具有任意名称的整数参数添加到参数列表中。这实现了与一整套 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:
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.