Eclipse抽象语法树差异

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 - 但是更倾向于代码)。

3个回答

7

GumTree 免费提供此项服务 :)

它还支持其他编程语言,例如javascript。


2
实际上,检查相等性很简单,使用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;
    }
}

2
鉴于Eclipse不支持AST差异比较,也许OP想找到两个文件在语言结构方面的差异,忽略空格和注释。我们的Smart Differencer工具会按照语言结构(变量、表达式、语句、块、方法等)比较两个源文件,并描述这些元素之间的抽象编辑操作的差异(删除、复制、移动、重命名区域中的标识符等)。

有没有开源的解决方案? - khatchad
显然不是。http://stackoverflow.com/questions/3829453/is-there-any-free-ast-diff-tool - Ira Baxter

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接