1package annotator.find;
2
3import com.sun.source.tree.Tree;
4import com.sun.source.util.TreePath;
5
6public class FieldCriterion implements Criterion {
7
8  public final String varName;
9  public final boolean isDeclaration;
10  public final Criterion varCriterion;
11  public final Criterion notInMethodCriterion;
12
13  public FieldCriterion(String varName) {
14    this(varName, false);
15  }
16
17  public FieldCriterion(String varName, boolean isDeclaration) {
18    this.varName = varName;
19    this.isDeclaration = isDeclaration;
20    this.varCriterion = Criteria.is(Tree.Kind.VARIABLE, varName);
21    this.notInMethodCriterion = Criteria.notInMethod();
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    if (path == null || (isDeclaration
35            && path.getLeaf().getKind() != Tree.Kind.VARIABLE)) {
36      return false;
37    }
38
39    if (varCriterion.isSatisfiedBy(path) &&
40        notInMethodCriterion.isSatisfiedBy(path)) {
41      return true;
42    } else {
43      return this.isSatisfiedBy(path.getParentPath());
44    }
45  }
46
47  @Override
48  public Kind getKind() {
49    return Kind.FIELD;
50  }
51
52  @Override
53  public String toString() {
54    return "FieldCriterion: " + varName;
55  }
56}
57