如何在Eclipse之外的项目中使用java Eclipse抽象语法树?(即不是eclipse插件)
如何在Eclipse之外的项目中使用java Eclipse抽象语法树?(即不是eclipse插件) 我看到的所有Eclipse AST示例都是针对Eclipse插件的。是否有一种方法(例如)可以将EclipseAST用于非eclipse项目 根据这一点,您应该能够独立于应用程序上下文(eclipse插件与否)调用AST解析器如何在Eclipse之外的项目中使用java Eclipse抽象语法树?(即不是eclipse插件),java,eclipse,abstract-syntax-tree,Java,Eclipse,Abstract Syntax Tree,如何在Eclipse之外的项目中使用java Eclipse抽象语法树?(即不是eclipse插件) 我看到的所有Eclipse AST示例都是针对Eclipse插件的。是否有一种方法(例如)可以将EclipseAST用于非eclipse项目 根据这一点,您应该能够独立于应用程序上下文(eclipse插件与否)调用AST解析器 (来源:) 由此: 3.0中的ASTParser可以在另一个独立程序中用于创建Eclipse 没有实际运行Eclipse的ASTs。正如文件所说: char[]
(来源:)
由此: 3.0中的ASTParser可以在另一个独立程序中用于创建Eclipse 没有实际运行Eclipse的ASTs。正如文件所说:
char[] source = ...;
ASTParser parser = ASTParser.newParser(AST.JLS2); // handles JLS2 (J2SE 1.4)
parser.setSource(source);
CompilationUnit result = (CompilationUnit) parser.createAST(null);
因此,尝试解析非常短的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);
}
}
下面是我在给定Java1.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());
}
}
}
}
}
你太棒了-我复制了这个,并把它放在github上:我怎样才能得到所需的JAR而不是commons io?可以在eclipse插件文件夹中找到,建议在
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