如何在Eclipse以外的項目中使用java Eclipse抽象語法樹? (即不是eclipse插件)如何在Eclipse以外的項目中使用Eclipse Eclipse抽象語法樹? (即不是eclipse插件)
我見過的所有Eclipse AST示例都是針對eclipse插件的。有沒有一種方法(例如)使用eclipse AST作爲非eclipse項目的項目。
如何在Eclipse以外的項目中使用java Eclipse抽象語法樹? (即不是eclipse插件)如何在Eclipse以外的項目中使用Eclipse Eclipse抽象語法樹? (即不是eclipse插件)
我見過的所有Eclipse AST示例都是針對eclipse插件的。有沒有一種方法(例如)使用eclipse AST作爲非eclipse項目的項目。
下面是我用來做這個給定一個Java 1.5文件的代碼。我對此非常感興趣,今天花在瀏覽周圍,試着讓下面的代碼正常工作。
public void processJavaFile(File file) {
String source = FileUtils.readFileToString(file);
Document document = new Document(source);
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setSource(document.get().toCharArray());
CompilationUnit unit = (CompilationUnit)parser.createAST(null);
unit.recordModifications();
// to get the imports from the file
List<ImportDeclaration> imports = unit.imports();
for (ImportDeclaration i : imports) {
System.out.println(i.getName().getFullyQualifiedName());
}
// to create a new import
AST ast = unit.getAST();
ImportDeclaration id = ast.newImportDeclaration();
String classToImport = "path.to.some.class";
id.setName(ast.newName(classToImport.split("\\.")));
unit.imports().add(id); // add import declaration at end
// to save the changed file
TextEdit edits = unit.rewrite(document, null);
edits.apply(document);
FileUtils.writeStringToFile(file, document.get());
// to iterate through methods
List<AbstractTypeDeclaration> types = unit.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("name: " + method.getName().getFullyQualifiedName());
}
}
}
}
}
這需要以下庫:
commons-io-1.4.jar
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
根據此old article,您應該能夠獨立於應用程序上下文(不管是否插入eclipse插件)來調用AST解析器。
ASTParser parser = ASTParser.newParser(AST.JLS2);
parser.setSource("".toCharArray());
CompilationUnit unit = (CompilationUnit) parser.createAST(null);
unit.recordModifications();
AST ast = unit.getAST();
alt text http://www.ibm.com/developerworks/opensource/library/os-ast/astexplorer.gif
從這個bug entry:
ASTParser在3.0可以在另一個獨立的程序中使用而無需實際運行Eclipse創建Eclipse的 的AST。由於文件說:
char[] source = ...;
ASTParser parser = ASTParser.newParser(AST.JLS2); // handles JLS2 (J2SE 1.4)
parser.setSource(source);
CompilationUnit result = (CompilationUnit) parser.createAST(null);
因此this thread試圖解析一個很短的java源:
import org.eclipse.jdt.core.dom.*;
import org.eclipse.jface.text.Document;
import org.eclipse.text.edits.TextEdit;
public class Test{
public static void main(String[] args){
Test t= new Test();
t.runtest();
}
void runtest(){
Document doc = new Document("import java.util.List;\nclass X {}\n");
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setResolveBindings(true);
parser.setSource(doc.get().toCharArray());
CompilationUnit cu = (CompilationUnit) parser.createAST(null);
cu.recordModifications();
AST ast = cu.getAST();
ImportDeclaration id = ast.newImportDeclaration();
id.setName(ast.newName(new String[] {"java", "util", "Set"}));
cu.imports().add(id); // add import declaration at end
TextEdit edits = cu.rewrite(doc, null);
}
}
也就是說這麼老,你不能得到它的工作了 – hawkeye 2011-01-14 23:47:58
您的岩石 - 我重複這一點,並把它放在github上的位置:HTTPS: //github.com/juliangamble/ASTTest – hawkeye 2011-01-14 23:47:30