使用 Eclipse AST [英] Using the Eclipse AST
问题描述
我最近需要修改一些 Java 代码(添加方法、更改某些字段的签名和删除方法),我认为所有这些都可以通过使用 Eclipse SDK 的 AST 来完成.
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?
非常感谢,
极限编码器
我现在开始更多地研究 JCodeModel,我认为这可能更容易使用,但我不知道是否可以将现有文档加载到其中?
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 ;)
再次感谢.
推荐答案
我不会在这里发布这个问题的整个源代码,因为它很长,但我会让人们开始.
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;
class X
{
public void deleteme()
{
}
}
");
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());
}
}
}
}
这应该会让你开始.
这确实需要一些时间来适应(在我的情况下需要很多时间).但它确实有效,而且是我能掌握的最佳方法.
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.
祝你好运;)
极限编码器
在我忘记之前,这些是我用来让它工作的导入(我花了很多时间来组织这些):
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
其中 xxxx 代表版本号.
Where xxxx represents a version number.
这篇关于使用 Eclipse AST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!