Home | History | Annotate | Download | only in find
      1 package annotator.find;
      2 
      3 import annotations.el.RelativeLocation;
      4 import annotator.scanner.MemberReferenceScanner;
      5 
      6 import com.sun.source.tree.Tree;
      7 import com.sun.source.util.TreePath;
      8 
      9 public class MemberReferenceCriterion implements Criterion {
     10   private final String methodName;
     11   private final RelativeLocation loc;
     12 
     13   public MemberReferenceCriterion(String methodName, RelativeLocation loc) {
     14     this.methodName = methodName;
     15     this.loc = loc;
     16   }
     17 
     18   /** {@inheritDoc} */
     19   @Override
     20   public boolean isSatisfiedBy(TreePath path, Tree leaf) {
     21     assert path == null || path.getLeaf() == leaf;
     22     return isSatisfiedBy(path);
     23   }
     24 
     25   /** {@inheritDoc} */
     26   @Override
     27   public boolean isSatisfiedBy(TreePath path) {
     28     if (path == null) {
     29       return false;
     30     }
     31 
     32     Tree leaf = path.getLeaf();
     33 
     34     if (leaf.getKind() == Tree.Kind.MEMBER_REFERENCE) {
     35       int indexInSource =
     36           MemberReferenceScanner.indexOfMemberReferenceTree(path, leaf);
     37       boolean b;
     38       if (loc.isBytecodeOffset()) {
     39         int indexInClass =
     40             MemberReferenceScanner.getMemberReferenceIndex(methodName,
     41                 loc.offset);
     42         b = (indexInSource == indexInClass);
     43       } else {
     44         b = (indexInSource == loc.index);
     45       }
     46       return b;
     47     } else {
     48       boolean b = this.isSatisfiedBy(path.getParentPath());
     49       return b;
     50     }
     51   }
     52 
     53   @Override
     54   public Kind getKind() {
     55     return Kind.METHOD_REFERENCE;
     56   }
     57 
     58   @Override
     59   public String toString() {
     60     return "MemberReferenceCriterion: in method: " + methodName + " location: " + loc;
     61   }
     62 }
     63