@Override
 public boolean matches(TryTree tryTree, VisitorState state) {
   return ASTHelpers.findEnclosingNode(state.getPath(), DoWhileLoopTree.class) != null
       || ASTHelpers.findEnclosingNode(state.getPath(), EnhancedForLoopTree.class) != null
       || ASTHelpers.findEnclosingNode(state.getPath(), WhileLoopTree.class) != null
       || ASTHelpers.findEnclosingNode(state.getPath(), ForLoopTree.class) != null;
 }
  /**
   * Fixes the error by assigning the result of the call to the receiver reference, or deleting the
   * method call.
   */
  public Description describe(MethodInvocationTree methodInvocationTree, VisitorState state) {
    // Find the root of the field access chain, i.e. a.intern().trim() ==> a.
    ExpressionTree identifierExpr = ASTHelpers.getRootAssignable(methodInvocationTree);
    String identifierStr = null;
    Type identifierType = null;
    if (identifierExpr != null) {
      identifierStr = identifierExpr.toString();
      if (identifierExpr instanceof JCIdent) {
        identifierType = ((JCIdent) identifierExpr).sym.type;
      } else if (identifierExpr instanceof JCFieldAccess) {
        identifierType = ((JCFieldAccess) identifierExpr).sym.type;
      } else {
        throw new IllegalStateException("Expected a JCIdent or a JCFieldAccess");
      }
    }

    Type returnType =
        ASTHelpers.getReturnType(((JCMethodInvocation) methodInvocationTree).getMethodSelect());

    Fix fix;
    if (identifierStr != null
        && !"this".equals(identifierStr)
        && returnType != null
        && state.getTypes().isAssignable(returnType, identifierType)) {
      // Fix by assigning the assigning the result of the call to the root receiver reference.
      fix = SuggestedFix.prefixWith(methodInvocationTree, identifierStr + " = ");
    } else {
      // Unclear what the programmer intended.  Delete since we don't know what else to do.
      Tree parent = state.getPath().getParentPath().getLeaf();
      fix = SuggestedFix.delete(parent);
    }
    return describeMatch(methodInvocationTree, fix);
  }
 @Override
 protected boolean matchArgument(ExpressionTree tree, VisitorState state) {
   Type type = ASTHelpers.getType(tree);
   if (!type.isReference()) {
     return false;
   }
   ClassTree classTree = ASTHelpers.findEnclosingNode(state.getPath(), ClassTree.class);
   if (classTree == null) {
     return false;
   }
   Type classType = ASTHelpers.getType(classTree);
   if (classType == null) {
     return false;
   }
   if (inEqualsOrCompareTo(classType, type, state)) {
     return false;
   }
   if (ASTHelpers.isSubtype(type, state.getSymtab().enumSym.type, state)) {
     return false;
   }
   if (ASTHelpers.isSubtype(type, state.getSymtab().classType, state)) {
     return false;
   }
   if (!implementsEquals(type, state)) {
     return false;
   }
   return true;
 }
    @Override
    public boolean matches(TryTree tryTree, VisitorState state) {
      MethodTree enclosingMethodTree =
          ASTHelpers.findEnclosingNode(state.getPath(), MethodTree.class);

      Name name = enclosingMethodTree.getName();
      return JUnitMatchers.looksLikeJUnit3SetUp.matches(enclosingMethodTree, state)
          || JUnitMatchers.looksLikeJUnit3TearDown.matches(enclosingMethodTree, state)
          || name.contentEquals("main")
          // TODO(schmitt): Move to JUnitMatchers?
          || name.contentEquals("suite")
          || Matchers.hasAnnotation(JUNIT_BEFORE_ANNOTATION).matches(enclosingMethodTree, state)
          || Matchers.hasAnnotation(JUNIT_AFTER_ANNOTATION).matches(enclosingMethodTree, state);
    }
 private boolean inEqualsOrCompareTo(Type classType, Type type, VisitorState state) {
   MethodTree methodTree = ASTHelpers.findEnclosingNode(state.getPath(), MethodTree.class);
   if (methodTree == null) {
     return false;
   }
   MethodSymbol sym = ASTHelpers.getSymbol(methodTree);
   if (sym == null || sym.isStatic()) {
     return false;
   }
   Symbol compareTo = getOnlyMember(state, state.getSymtab().comparableType, "compareTo");
   Symbol equals = getOnlyMember(state, state.getSymtab().objectType, "equals");
   if (!sym.overrides(compareTo, classType.tsym, state.getTypes(), false)
       && !sym.overrides(equals, classType.tsym, state.getTypes(), false)) {
     return false;
   }
   if (!ASTHelpers.isSameType(type, classType, state)) {
     return false;
   }
   return true;
 }
Exemple #6
0
 /**
  * Returns the {@link Nullness} for an expression as determined by the nullness dataflow analysis.
  */
 public static Nullness getNullnessValue(
     ExpressionTree expr, VisitorState state, NullnessAnalysis nullnessAnalysis) {
   TreePath pathToExpr = new TreePath(state.getPath(), expr);
   return nullnessAnalysis.getNullness(pathToExpr, state.context);
 }