Home | History | Annotate | Download | only in find
      1 package annotator.find;
      2 
      3 import annotator.Main;
      4 import annotator.scanner.CommonScanner;
      5 
      6 import com.sun.source.tree.Tree;
      7 import com.sun.source.util.TreePath;
      8 
      9 public class ReturnTypeCriterion implements Criterion {
     10 
     11   private final String methodName;
     12   private final Criterion inClassCriterion;
     13   private final Criterion sigMethodCriterion;
     14 
     15   public ReturnTypeCriterion(String className, String methodName) {
     16     this.methodName = methodName;
     17     this.inClassCriterion = Criteria.inClass(className, false);
     18     this.sigMethodCriterion = methodName.isEmpty() ? null
     19         : Criteria.isSigMethod(methodName);
     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) { return false; }
     33 
     34     Criteria.dbug.debug("ReturnTypeCriterion.isSatisfiedBy(%s); this=%n",
     35         Main.pathToString(path), this.toString());
     36 
     37     do {
     38       if (path.getLeaf().getKind() == Tree.Kind.METHOD) {
     39         if (sigMethodCriterion == null
     40             || sigMethodCriterion.isSatisfiedBy(path)) {
     41           // Method and return type verified; now check class.
     42           path = path.getParentPath();
     43           while (path != null && path.getLeaf() != null) {
     44             if (CommonScanner.hasClassKind(path.getLeaf())) {
     45               if (!inClassCriterion.isSatisfiedBy(path)) { break; }
     46               Criteria.dbug.debug("ReturnTypeCriterion.isSatisfiedBy => true%n");
     47               return true;
     48             }
     49             path = path.getParentPath();
     50           }
     51         }
     52         break;
     53       }
     54       path = path.getParentPath();
     55     } while (path != null && path.getLeaf() != null);
     56 
     57     Criteria.dbug.debug("ReturnTypeCriterion.isSatisfiedBy => false%n");
     58     return false;
     59   }
     60 
     61   @Override
     62   public Kind getKind() {
     63     return Kind.RETURN_TYPE;
     64   }
     65 
     66   @Override
     67   public String toString() {
     68     return "ReturnTypeCriterion for method: " + methodName;
     69   }
     70 }
     71