Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Eclipse抽象语法树差异_Java_Eclipse_Diff_Abstract Syntax Tree_Compilationunit - Fatal编程技术网

Java Eclipse抽象语法树差异

Java Eclipse抽象语法树差异,java,eclipse,diff,abstract-syntax-tree,compilationunit,Java,Eclipse,Diff,Abstract Syntax Tree,Compilationunit,给定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

给定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);
    }
}
如何使用EclipseCompareAPI(org.Eclipse.Compare)查找AST差异?(这可以在插件之外完成吗?)

我正在看下面的API


任何人都可以指向示例代码(或API,但代码是首选)。

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

免费执行此项工作:)


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

实际上,使用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;
    }
}
公共类{
@抑制警告(“未选中”)
静态布尔等于(ASTNode left,ASTNode right){
//如果两者都为空,则它们相等,但如果只有一个,则它们不相等
if(left==null&&right==null){
返回true;
}else if(左==null | |右==null){
返回false;
}
//如果节点类型相同,我们可以假设它们具有相同的
//性质
if(left.getNodeType()!=right.getNodeType()){
返回false;
}
列表道具=左
.StructurePropertiesForType();
for(StructuralPropertyDescriptor属性:props){
Object leftVal=left.getStructuralProperty(属性);
Object rightVal=right.getStructuralProperty(属性);
if(property.isSimpleProperty()){
//检查简单属性(基本类型、字符串等)
//正常相等
如果(!leftVal.等于(rightVal)){
返回false;
}
}else if(property.isChildProperty()){
//在子节点上递归调用此函数
如果(!等于((ASTNode)leftVal,(ASTNode)rightVal)){
返回false;
}
}else if(property.isChildListProperty()){
迭代器leftValIt=((Iterable)leftVal)
.iterator();
迭代器rightValIt=((Iterable)rightVal)
.iterator();
while(leftValIt.hasNext()&&righvalit.hasNext()){
//在子节点上递归调用此函数
如果(!等于(leftValIt.next(),rightValIt.next()){
返回false;
}
}
//其中一个值列表包含其他元素
if(leftValIt.hasNext()| | righvalit.hasNext()){
返回false;
}
}
}
返回true;
}
}

有开源解决方案吗?显然没有