1 package annotator.find; 2 3 import annotator.scanner.CommonScanner; 4 5 import com.sun.source.tree.*; 6 import com.sun.source.util.TreePath; 7 8 /** 9 * Represents the criterion that a program element has a particular type and 10 * name. 11 */ 12 final class IsCriterion implements Criterion { 13 14 private final Tree.Kind kind; 15 private final String name; 16 IsCriterion(Tree.Kind kind, String name)17 IsCriterion(Tree.Kind kind, String name) { 18 this.kind = kind; 19 this.name = name; 20 } 21 22 /** 23 * {@inheritDoc} 24 */ 25 @Override getKind()26 public Kind getKind() { 27 return Kind.HAS_KIND; 28 } 29 30 /** {@inheritDoc} */ 31 @Override isSatisfiedBy(TreePath path, Tree leaf)32 public boolean isSatisfiedBy(TreePath path, Tree leaf) { 33 assert path == null || path.getLeaf() == leaf; 34 return isSatisfiedBy(path); 35 } 36 37 /** {@inheritDoc} */ 38 @Override isSatisfiedBy(TreePath path)39 public boolean isSatisfiedBy(TreePath path) { 40 if (path == null) { 41 return false; 42 } 43 Tree tree = path.getLeaf(); 44 if (CommonScanner.hasClassKind(tree)) { 45 return InClassCriterion.isSatisfiedBy(path, name, /*exactMatch=*/ true); 46 } 47 if (tree.getKind() != kind) { 48 return false; 49 } 50 switch (tree.getKind()) { 51 case VARIABLE: 52 String varName = ((VariableTree)tree).getName().toString(); 53 return varName.equals(name); 54 case METHOD: 55 String methodName = ((MethodTree)tree).getName().toString(); 56 return methodName.equals(name); 57 // case CLASS: 58 // return InClassCriterion.isSatisfiedBy(path, name, /*exactMatch=*/ true); 59 default: 60 throw new Error("unknown tree kind " + kind); 61 } 62 } 63 64 /** 65 * {@inheritDoc} 66 */ 67 @Override toString()68 public String toString() { 69 return "is " + kind.toString().toLowerCase() + " '" + name + "'"; 70 } 71 72 } 73