1package annotator.find;
2
3import annotations.el.RelativeLocation;
4import annotator.scanner.CastScanner;
5
6import com.sun.source.tree.Tree;
7import com.sun.source.util.TreePath;
8
9/**
10 * Criterion for being a specific type cast expression.
11 */
12public class CastCriterion implements Criterion {
13
14  private final String methodName;
15  private final RelativeLocation loc;
16
17  public CastCriterion(String methodName, RelativeLocation loc) {
18    this.methodName = methodName.substring(0, methodName.lastIndexOf(")") + 1);
19    this.loc = loc;
20  }
21
22  /** {@inheritDoc} */
23  @Override
24  public boolean isSatisfiedBy(TreePath path, Tree leaf) {
25    assert path == null || path.getLeaf() == leaf;
26    return isSatisfiedBy(path);
27  }
28
29  /** {@inheritDoc} */
30  @Override
31  public boolean isSatisfiedBy(TreePath path) {
32    if (path == null) {
33      return false;
34    }
35
36    Tree leaf = path.getLeaf();
37
38    if (leaf.getKind() == Tree.Kind.TYPE_CAST) {
39      int indexInSource = CastScanner.indexOfCastTree(path, leaf);
40      boolean b;
41      if (loc.isBytecodeOffset()) {
42        int indexInClass = CastScanner.getMethodCastIndex(methodName, loc.offset);
43        b = (indexInSource == indexInClass);
44      } else {
45        b = (indexInSource == loc.index);
46      }
47      return b;
48
49    } else {
50      boolean b = this.isSatisfiedBy(path.getParentPath());
51      return b;
52    }
53  }
54
55  public RelativeLocation getLocation() { return loc; }
56
57  @Override
58  public Kind getKind() {
59    return Kind.CAST;
60  }
61
62  @Override
63  public String toString() {
64    return "CastCriterion: in method: " + methodName + " location: " + loc;
65  }
66}
67