2009-06-08 18 views
22

Làm thế nào tôi có thể sử dụng Java Syntax Syntax Tree trong một dự án ngoài Eclipse? (nghĩa là không phải là một plugin eclipse)Làm thế nào tôi có thể sử dụng cây cú pháp trừu tượng Eclipse của Java trong một dự án bên ngoài Eclipse? (tức là không phải là một plugin eclipse)

Tất cả các ví dụ AST Eclipse mà tôi đã thấy là dành cho các plugin eclipse. Có cách nào (ví dụ như một ví dụ) của một dự án sử dụng AST nhật thực cho một dự án không thực.

Trả lời

26

Dưới đây là đoạn code tôi sử dụng để làm điều này được đưa ra một Java 1.5 tập tin. Tôi rất mới với điều này và dành ngày hôm nay duyệt xung quanh, và cố gắng những điều trên để có được mã dưới đây làm việc.

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()); 
       } 
      } 
     } 
    } 
} 

Điều này đòi hỏi các thư viện sau:

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 
+2

bạn đá - Tôi đã nhân đôi này và đặt nó trên github ở đây: https: //github.com/juliangamble/ASTTest – hawkeye

+0

@hawkeye: tuyệt vời. +1 cho giải pháp cập nhật hơn nhiều. – VonC

+0

làm thế nào tôi có thể nhận được các lọ cần thiết khác ngoài commons-io? –

4

Theo điều này old article, bạn sẽ có thể gọi trình phân tích cú pháp AST độc lập với ngữ cảnh ứng dụng của bạn (plugin eclipse hay không).

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


Từ này bug entry:

ASTParser trong 3.0 có thể được sử dụng trong một chương trình độc lập để tạo Eclipse ASTs mà không thực sự chạy Eclipse. Như các tài liệu nói:

char[] source = ...; 
    ASTParser parser = ASTParser.newParser(AST.JLS2); // handles JLS2 (J2SE 1.4) 
    parser.setSource(source); 
    CompilationUnit result = (CompilationUnit) parser.createAST(null); 

Do đó this thread nỗ lực để phân tích một nguồn java rất ngắn:

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); 
} 

} 
+0

Đó là quá cũ bạn không thể có được nó để làm việc nữa – hawkeye

Các vấn đề liên quan