1 package annotator.find; 2 3 import com.sun.source.tree.Tree; 4 import com.sun.source.util.TreePath; 5 6 /** 7 * Represents the criterion that a program element is enclosed (directly or 8 * indirect) by a program element of a certain type. 9 */ 10 final class EnclosedByCriterion implements Criterion { 11 12 private final Tree.Kind kind; 13 14 EnclosedByCriterion(Tree.Kind kind) { 15 this.kind = kind; 16 } 17 18 /** {@inheritDoc} */ 19 @Override 20 public Kind getKind() { 21 return Kind.ENCLOSED_BY; 22 } 23 24 /** {@inheritDoc} */ 25 @Override 26 public boolean isSatisfiedBy(TreePath path, Tree leaf) { 27 assert path == null || path.getLeaf() == leaf; 28 return isSatisfiedBy(path); 29 } 30 31 /** {@inheritDoc} */ 32 @Override 33 public boolean isSatisfiedBy(TreePath path) { 34 35 if (path == null) { 36 return false; 37 } 38 39 for (Tree tree : path) { 40 if (tree.getKind() == kind) { 41 return true; 42 } 43 } 44 return false; 45 } 46 47 /** 48 * {@inheritDoc} 49 */ 50 @Override 51 public String toString() { 52 return "enclosed by '" + kind + "'"; 53 } 54 } 55