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