首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Eclipse抽象语法树差异

Eclipse抽象语法树差异
EN

Stack Overflow用户
提问于 2009-06-10 10:51:07
回答 3查看 5K关注 0票数 4

给定Eclipse中的以下代码:

代码语言:javascript
复制
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 -但代码是首选)。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2009-08-12 20:50:06

鉴于Eclipse不执行AST差异,OP可能想要找出两个文件在忽略空格和注释的语言构造方面的差异。我们的Smart Differencer tool根据语言构造(变量、表达式、语句、块、方法等)比较两个源文件并描述了这些元素在抽象编辑操作方面的差异(删除、复制、移动、重命名区域中的标识符,...)

票数 2
EN

Stack Overflow用户

发布于 2014-09-11 10:18:42

GumTree免费做这项工作:)

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

票数 6
EN

Stack Overflow用户

发布于 2015-06-04 17:51:13

实际上,使用ASTNode的属性检查相等性很简单。在此之后,这取决于您,您希望如何获得差异。检查代码示例以进行相等性测试:

代码语言:javascript
复制
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;
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/974855

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档