• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 package with a
10  * certain name.
11  */
12 final class InPackageCriterion implements Criterion {
13 
14   private final String name;
15 
InPackageCriterion(String name)16   InPackageCriterion(String name) {
17     this.name = name;
18   }
19 
20   /**
21    * {@inheritDoc}
22    */
23   @Override
getKind()24   public Kind getKind() {
25     return Kind.IN_PACKAGE;
26   }
27 
28   /** {@inheritDoc} */
29   @Override
isSatisfiedBy(TreePath path, Tree leaf)30   public boolean isSatisfiedBy(TreePath path, Tree leaf) {
31     assert path == null || path.getLeaf() == leaf;
32     return isSatisfiedBy(path);
33   }
34 
35   /** {@inheritDoc} */
36   @Override
isSatisfiedBy(TreePath path)37   public boolean isSatisfiedBy(TreePath path) {
38     if (path == null) {
39       return false;
40     }
41 
42     Criteria.dbug.debug("InPackageCriterion.isSatisfiedBy(%s); this=%s",
43         Main.pathToString(path), this.toString());
44 
45     do {
46       Tree tree = path.getLeaf();
47       if (tree.getKind() == Tree.Kind.COMPILATION_UNIT) {
48         CompilationUnitTree cu = (CompilationUnitTree)tree;
49         ExpressionTree pn = cu.getPackageName();
50         if (pn == null) {
51           return name == null || name.equals("");
52         } else {
53           String packageName = pn.toString();
54           return name != null && (name.equals(packageName));
55         }
56       }
57       path = path.getParentPath();
58     } while (path != null && path.getLeaf() != null);
59 
60     Criteria.dbug.debug("InPackageCriterion.isSatisfiedBy => false");
61     return false;
62   }
63 
64   /**
65    * {@inheritDoc}
66    */
67   @Override
toString()68   public String toString() {
69     return "in package '" + name + "'";
70   }
71 }
72