4

Eclipseで次のコードを考えます。

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

Eclipse Compare API(org.eclipse.compare)をどのように使用してASTの違いを見つけますか?(そして、これはプラグインの外で行うことができますか?)

私は次の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-ただし、コードが推奨されます)を指すことができます。

4

3 に答える 3

7

GumTreeは無料で仕事をします:)

また、javascriptなどの他の言語もサポートしています。

于 2014-09-11T02:18:41.820 に答える
2

EclipseがAST差分を行わないことを考えると、おそらくOPは、空白とコメントを無視する言語構成の観点から、2つのファイル間の差異を見つけたいと考えていました。Smart Differencerツールは、言語構成(変数、式、ステートメント、ブロック、メソッドなど)に関して2つのソースファイルを比較し、これらの要素に対する抽象的な編集操作(削除、コピー、移動、名前変更)に関する違いを説明します。地域の識別子、...)

于 2009-08-12T20:50:06.190 に答える
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;
    }
}
于 2015-06-04T09:51:13.200 に答える