1 package annotator.find; 2 3 import annotations.el.RelativeLocation; 4 import annotator.scanner.LambdaScanner; 5 6 import com.sun.source.tree.LambdaExpressionTree; 7 import com.sun.source.tree.Tree; 8 import com.sun.source.util.TreePath; 9 10 public class LambdaCriterion implements Criterion { 11 private final String methodName; 12 private final RelativeLocation loc; 13 LambdaCriterion(String methodName, RelativeLocation loc)14 public LambdaCriterion(String methodName, RelativeLocation loc) { 15 this.methodName = methodName; 16 this.loc = loc; 17 } 18 19 /** {@inheritDoc} */ 20 @Override isSatisfiedBy(TreePath path, Tree leaf)21 public boolean isSatisfiedBy(TreePath path, Tree leaf) { 22 assert path == null || path.getLeaf() == leaf; 23 return isSatisfiedBy(path); 24 } 25 26 /** {@inheritDoc} */ 27 @Override isSatisfiedBy(TreePath path)28 public boolean isSatisfiedBy(TreePath path) { 29 if (path == null) { 30 Criteria.dbug.debug("return null"); 31 return false; 32 } 33 34 Tree leaf = path.getLeaf(); 35 36 Criteria.dbug.debug("%n%s%n", this.toString()); 37 Criteria.dbug.debug("LambdaCriterion.isSatisfiedBy: %s%n", leaf); 38 Criteria.dbug.debug("leaf: %s%n", leaf); 39 Criteria.dbug.debug("kind: %s%n", leaf.getKind()); 40 Criteria.dbug.debug("class: %s%n", leaf.getClass()); 41 42 TreePath parentPath = path.getParentPath(); 43 if (parentPath == null) { 44 Criteria.dbug.debug("return: parent path null%n"); 45 return false; 46 } 47 48 Tree parent = parentPath.getLeaf(); 49 if (parent == null) { 50 Criteria.dbug.debug("return: parent null%n"); 51 return false; 52 } 53 54 if (parent.getKind() == Tree.Kind.LAMBDA_EXPRESSION) { 55 // LambdaExpressionTree lambdaTree = (LambdaExpressionTree) parent; 56 57 int indexInSource = LambdaScanner.indexOfLambdaExpressionTree(path, parent); 58 Criteria.dbug.debug("return source: %d%n", indexInSource); 59 boolean b; 60 if (loc.isBytecodeOffset()) { 61 int indexInClass = LambdaScanner.getMethodLambdaExpressionIndex(methodName, loc.offset); 62 Criteria.dbug.debug("return class: %d%n", indexInClass); 63 b = (indexInSource == indexInClass); 64 } else { 65 b = (indexInSource == loc.index); 66 Criteria.dbug.debug("return loc.index: %d%n", loc.index); 67 } 68 Criteria.dbug.debug("return new: %b%n", b); 69 return b; 70 } else { 71 boolean b = this.isSatisfiedBy(path.getParentPath()); 72 Criteria.dbug.debug("return parent: %b%n", b); 73 return b; 74 } 75 } 76 77 @Override getKind()78 public Kind getKind() { 79 return Kind.LAMBDA_EXPRESSION; 80 } 81 82 @Override toString()83 public String toString() { 84 return "LambdaCriterion: at location: " + loc; 85 } 86 } 87