我不會在這裏發佈整個源代碼到這個問題,因爲它很長,但我會讓人們開始。
所有你需要的文檔是在這裏: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表示版本號。