2009-06-10 12 views
4

は、Eclipseで次のコードを考える:どのようにEclipseを使用してくださいEclipseの抽象構文木のDiff

import org.eclipse.jdt.core.dom.AST; 
import org.eclipse.jdt.core.dom.ASTParser; 
import org.eclipse.jdt.core.dom.CompilationUnit; 

public class Question { 
    public static void main(String[] args) { 
     String source = "class Bob {}"; 
     ASTParser parser = ASTParser.newParser(AST.JLS3); 
     parser.setSource(source.toCharArray()); 
     CompilationUnit result = (CompilationUnit) parser.createAST(null); 

     String source2 = "class Bob {public void MyMethod(){}}"; 
     ASTParser parser2 = ASTParser.newParser(AST.JLS3); 
     parser2.setSource(source2.toCharArray()); 
     CompilationUnit result2 = (CompilationUnit) parser2.createAST(null); 
    } 
} 

をAST差を見つけるために、API(org.eclipse.compare)を比較? (そしてこれは、プラグインの外で行うことができますか?)私は、次のAPI

http://kickjava.com/src/org/eclipse/compare/structuremergeviewer/Differencer.java.htm http://kickjava.com/src/org/eclipse/jdt/internal/ui/compare/JavaStructureCreator.java.htm http://kickjava.com/src/org/eclipse/compare/CompareUI.java.htm

で探しています

誰もがサンプルコード(またはAPIを指すことができます - しかし、コードが望ましい)。

答えて

2

EclipseがAST差分を行わないとすれば、おそらくOPは空白とコメントを無視して言語構造の点で2つのファイル間の相違点を探したかったでしょう。私たちのSmart Differencer toolは、言語構造(変数、式、文、ブロック、メソッドなど)に関して2つのソースファイルを比較し、これらの要素に対する抽象的な編集操作の違いを説明します(削除、コピー、移動、領域は、...)

+0

オープンソースソリューションはありますか? –

+0

Apparantly not http://stackoverflow.com/questions/3829453/is-there-any-free-ast-diff-tool –

5

GumTreeは無料で、仕事をしていません:)

また、JavaScriptのような他の言語をサポートしています。

1

実際には、ASTNodeのプロパティを使用して、等価性のチェックは簡単です。その後、違いをどうやって得たいかはあなた次第です。同等性テストのコードサンプルを確認してください:

public class ASTCompare { 

    @SuppressWarnings("unchecked") 
    static boolean equals(ASTNode left, ASTNode right) { 
     // if both are null, they are equal, but if only one, they aren't 
     if (left == null && right == null) { 
      return true; 
     } else if (left == null || right == null) { 
      return false; 
     } 
     // if node types are the same we can assume that they will have the same 
     // properties 
     if (left.getNodeType() != right.getNodeType()) { 
      return false; 
     } 
     List<StructuralPropertyDescriptor> props = left 
       .structuralPropertiesForType(); 
     for (StructuralPropertyDescriptor property : props) { 
      Object leftVal = left.getStructuralProperty(property); 
      Object rightVal = right.getStructuralProperty(property); 
      if (property.isSimpleProperty()) { 
       // check for simple properties (primitive types, Strings, ...) 
       // with normal equality 
       if (!leftVal.equals(rightVal)) { 
        return false; 
       } 
      } else if (property.isChildProperty()) { 
       // recursively call this function on child nodes 
       if (!equals((ASTNode) leftVal, (ASTNode) rightVal)) { 
        return false; 
       } 
      } else if (property.isChildListProperty()) { 
       Iterator<ASTNode> leftValIt = ((Iterable<ASTNode>) leftVal) 
         .iterator(); 
       Iterator<ASTNode> rightValIt = ((Iterable<ASTNode>) rightVal) 
         .iterator(); 
       while (leftValIt.hasNext() && rightValIt.hasNext()) { 
        // recursively call this function on child nodes 
        if (!equals(leftValIt.next(), rightValIt.next())) { 
         return false; 
        } 
       } 
       // one of the value lists have additional elements 
       if (leftValIt.hasNext() || rightValIt.hasNext()) { 
        return false; 
       } 
      } 
     } 
     return true; 
    } 
} 
関連する問題