package annotator.find;

import com.sun.source.tree.ArrayTypeTree;
import com.sun.source.tree.IdentifierTree;
import com.sun.source.tree.ParameterizedTypeTree;
import com.sun.source.tree.PrimitiveTypeTree;
import com.sun.source.tree.Tree;
import com.sun.source.tree.VariableTree;
import com.sun.source.util.TreePath;

public class FieldCriterion implements Criterion {

  public String varName;
  public Criterion varCriterion;
  public Criterion notInMethodCriterion;

  public FieldCriterion(String varName) {
    this.varName = varName;
    this.varCriterion = Criteria.is(Tree.Kind.VARIABLE, varName);
    this.notInMethodCriterion = Criteria.notInMethod();
  }

  /** {@inheritDoc} */
  @Override
  public boolean isSatisfiedBy(TreePath path, Tree leaf) {
    assert path == null || path.getLeaf() == leaf;
    return isSatisfiedBy(path);
  }

  /** {@inheritDoc} */
  @Override
  public boolean isSatisfiedBy(TreePath path) {
    if (path == null) {
      return false;
    }

    Tree leaf = path.getLeaf();
    boolean var = leaf instanceof ParameterizedTypeTree
    || leaf instanceof IdentifierTree
    || leaf instanceof ArrayTypeTree
    || leaf instanceof PrimitiveTypeTree;

    if (leaf.getKind() == Tree.Kind.VARIABLE) {
      VariableTree vt = (VariableTree) leaf;
      String s = vt.getName().toString();
      boolean b =  varName.equals(vt.getName().toString()) &&
        notInMethodCriterion.isSatisfiedBy(path);
      // System.out.println("At (leaf.getKind() == Tree.Kind.VARIABLE)");
      // Why is the below commented out?  This branch is frequently taken.
//      return b;
    }

    if (varCriterion.isSatisfiedBy(path) &&
        notInMethodCriterion.isSatisfiedBy(path)) {
      return true;
    } else {
      return this.isSatisfiedBy(path.getParentPath());
    }
  }

  public Kind getKind() {
    return Kind.FIELD;
  }

  public String toString() {
    return "FieldCriterion: " + varName;
  }
}

@@ -49,230 +49,8 @@
 ree.
-ArrayTypeTree;%0Aimport com.sun.source.tree.IdentifierTree;%0Aimport com.sun.source.tree.ParameterizedTypeTree;%0Aimport com.sun.source.tree.PrimitiveTypeTree;%0Aimport com.sun.source.tree.Tree;%0Aimport com.sun.source.tree.Variable
 Tree
@@ -758,635 +758,8 @@
  %7D%0A%0A
-    Tree leaf = path.getLeaf();%0A    boolean var = leaf instanceof ParameterizedTypeTree%0A    %7C%7C leaf instanceof IdentifierTree%0A    %7C%7C leaf instanceof ArrayTypeTree%0A    %7C%7C leaf instanceof PrimitiveTypeTree;%0A%0A    if (leaf.getKind() == Tree.Kind.VARIABLE) %7B%0A      VariableTree vt = (VariableTree) leaf;%0A      String s = vt.getName().toString();%0A      boolean b =  varName.equals(vt.getName().toString()) &&%0A        notInMethodCriterion.isSatisfiedBy(path);%0A      // System.out.println(%22At (leaf.getKind() == Tree.Kind.VARIABLE)%22);%0A      // Why is the below commented out?  This branch is frequently taken.%0A//      return b;%0A    %7D%0A%0A
     

Remove a lot of dead, confusing code.