• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package annotator.find;
2 
3 import javax.lang.model.element.Modifier;
4 
5 import annotator.Main;
6 
7 import com.sun.source.tree.*;
8 import com.sun.source.util.TreePath;
9 
10 /**
11  * Represents the criterion that a program element is in a method with a
12  * certain name.
13  */
14 final class InMethodCriterion implements Criterion {
15 
16   public final String name;
17   private final IsSigMethodCriterion sigMethodCriterion;
18 
InMethodCriterion(String name)19   InMethodCriterion(String name) {
20     this.name = name;
21     sigMethodCriterion = new IsSigMethodCriterion(name);
22   }
23 
24   /**
25    * {@inheritDoc}
26    */
27   @Override
getKind()28   public Kind getKind() {
29     return Kind.IN_METHOD;
30   }
31 
32   /** {@inheritDoc} */
33   @Override
isSatisfiedBy(TreePath path, Tree leaf)34   public boolean isSatisfiedBy(TreePath path, Tree leaf) {
35     assert path == null || path.getLeaf() == leaf;
36     return isSatisfiedBy(path);
37   }
38 
39   /** {@inheritDoc} */
40   @Override
isSatisfiedBy(TreePath path)41   public boolean isSatisfiedBy(TreePath path) {
42     Criteria.dbug.debug("InMethodCriterion.isSatisfiedBy(%s); this=%s%n",
43         Main.pathToString(path), this.toString());
44     boolean staticDecl = false;
45     boolean result = false;
46 
47     do {
48       if (path.getLeaf().getKind() == Tree.Kind.METHOD) {
49         boolean b = sigMethodCriterion.isSatisfiedBy(path);
50         Criteria.dbug.debug("%s%n", "InMethodCriterion.isSatisfiedBy => b");
51         return b;
52       }
53       if (path.getLeaf().getKind() == Tree.Kind.VARIABLE) {
54         ModifiersTree mods = ((VariableTree) path.getLeaf()).getModifiers();
55         staticDecl = mods.getFlags().contains(Modifier.STATIC);
56       }
57       path = path.getParentPath();
58     } while (path != null && path.getLeaf() != null);
59 
60     result = (staticDecl ? "<clinit>()V" : "<init>()V").equals(name);
61 
62     Criteria.dbug.debug("InMethodCriterion.isSatisfiedBy => %s%n", result);
63     return result;
64   }
65 
66   /**
67    * {@inheritDoc}
68    */
69   @Override
toString()70   public String toString() {
71     return "in method '" + name + "'";
72   }
73 }
74