我不会在这里发布这个问题的整个源代码,因为它很长,但我会让人们开始。
您需要的所有文档都在这里: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 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());
}
}
}
}
这应该能让你们开始。
确实需要一些时间来适应这一点(就我而言需要花费很多时间)。但它确实有效,并且是我能掌握的最好方法。
祝你好运 ;)
极限编码器
Edit:
在我忘记之前,这些是我用来让它工作的导入(我花了相当多的时间来组织这些):
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 代表版本号。