【问题标题】:Eclipse Abstract Syntax Tree DiffEclipse 抽象语法树差异
【发布时间】:2009-06-10 10:51:07
【问题描述】:

在 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 比较 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 - 但首选代码)。

【问题讨论】:

    标签: java eclipse diff abstract-syntax-tree compilationunit


    【解决方案1】:

    GumTree 完成这项工作,免费 :)

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

    【讨论】:

      【解决方案2】:

      鉴于 Eclipse 不进行 AST 差异,也许 OP 想要在忽略空格和 cmets 的语言结构方面找到两个文件之间的差异。我们的Smart Differencer tool 比较了两个源文件的语言结构(变量、表达式、语句、块、方法……),并描述了对这些元素的抽象编辑操作(删除、复制、移动、重命名)的差异地区标识符,...)

      【讨论】:

      【解决方案3】:

      实际上,使用 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;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多