1 package annotator.find; 2 3 import annotator.Main; 4 5 import com.sun.source.tree.*; 6 import com.sun.source.util.TreePath; 7 8 /** 9 * Represents the criterion that a program element is in a method with a 10 * certain name. 11 */ 12 final class PackageCriterion implements Criterion { 13 14 private final String name; 15 PackageCriterion(String name)16 PackageCriterion(String name) { 17 this.name = name; 18 } 19 20 /** {@inheritDoc} */ 21 @Override getKind()22 public Kind getKind() { 23 return Kind.PACKAGE; 24 } 25 26 /** {@inheritDoc} */ 27 @Override isSatisfiedBy(TreePath path, Tree tree)28 public boolean isSatisfiedBy(TreePath path, Tree tree) { 29 assert path == null || path.getLeaf() == tree; 30 return isSatisfiedBy(path); 31 } 32 33 /** {@inheritDoc} */ 34 @Override isSatisfiedBy(TreePath path)35 public boolean isSatisfiedBy(TreePath path) { 36 Tree tree = path.getLeaf(); 37 Criteria.dbug.debug("PackageCriterion.isSatisfiedBy(%s, %s); this=%s%n", 38 Main.pathToString(path), tree, this.toString()); 39 40 if (tree.getKind() == Tree.Kind.COMPILATION_UNIT) { 41 CompilationUnitTree cu = (CompilationUnitTree)tree; 42 if (cu.getSourceFile().getName().endsWith("package-info.java")) { 43 ExpressionTree pn = cu.getPackageName(); 44 assert ((pn instanceof IdentifierTree) 45 || (pn instanceof MemberSelectTree)); 46 if (this.name.equals(pn.toString())) { 47 return true; 48 } 49 } 50 } 51 Criteria.dbug.debug("PackageCriterion.isSatisfiedBy => false%n"); 52 return false; 53 } 54 55 /** 56 * {@inheritDoc} 57 */ 58 @Override toString()59 public String toString() { 60 return "package '" + name + "'"; 61 } 62 } 63