Class JavacAnnotationEqualityVisitor

All Implemented Interfaces:
TreeVisitor<Void,Tree>

public class JavacAnnotationEqualityVisitor extends DoubleJavacVisitor
Given two javac ASTs representing the same Java file that may differ in annotations, findMismatch(Tree, Tree) tests if they have the same annotations.

Two annotations are compared as AnnotationMirrors when both ASTs have been attributed by javac, and as source text otherwise; see sameAnnotation(com.sun.source.tree.AnnotationTree, com.sun.source.tree.AnnotationTree).

Known gap: a receiver parameter is compared only if both ASTs have one, because a .ajava file may add an explicit receiver parameter that the Java file omits. As a consequence, if a receiver parameter appears in only one of the two ASTs, its annotations are ignored rather than reported as a mismatch. See DoubleJavacVisitor.visitMethod(com.sun.source.tree.MethodTree, com.sun.source.tree.Tree).

This is the javac-based replacement for AnnotationEqualityVisitor.

  • Method Details

    • findMismatch

      public static @Nullable org.plumelib.util.IPair<Tree,Tree> findMismatch(Tree tree1, Tree tree2)
      Returns null if the two ASTs have matching annotations everywhere, or the first pair of corresponding nodes where annotations differ. The comparison is order-sensitive: nodes with the same annotations in a different order are considered to differ. Only the first mismatch is returned even if multiple mismatches exist.
      Parameters:
      tree1 - root of the first AST
      tree2 - root of the second AST
      Returns:
      null if annotations match everywhere, or a pair of corresponding nodes from tree1 and tree2 where annotations differ
      Throws:
      UserError - if the two ASTs differ other than in annotations; for example, if one declares a member that the other does not
    • defaultAction

      protected Void defaultAction(Tree tree1, Tree tree2)
      Description copied from class: DoubleJavacVisitor
      Default action performed on each pair of nodes from matching ASTs.

      This method is called by each visitXyz method in this class before scanning child trees. It does not itself recurse; recursion is driven by the visitXyz methods.

      Specified by:
      defaultAction in class DoubleJavacVisitor
      Parameters:
      tree1 - the first tree in the matched pair
      tree2 - the second tree in the matched pair
      Returns:
      null
    • visitAnnotationList

      protected void visitAnnotationList(Tree owner1, Tree owner2, List<? extends AnnotationTree> annotations1, List<? extends AnnotationTree> annotations2)
      Description copied from class: DoubleJavacVisitor
      Visits two corresponding annotation lists. This base implementation does nothing because annotations may legitimately differ between a Java file and its corresponding .ajava file. Subclasses may override this method to process annotations.
      Overrides:
      visitAnnotationList in class DoubleJavacVisitor
      Parameters:
      owner1 - the tree from the first AST that the annotations are attached to
      owner2 - the tree from the second AST that the annotations are attached to
      annotations1 - annotation list from the first AST
      annotations2 - annotation list from the second AST