Пример #1
0
 @Override
 public Description matchMethodInvocation(
     MethodInvocationTree methodInvocationTree, VisitorState state) {
   if (methodInvocationTree.getArguments().isEmpty()) {
     return Description.NO_MATCH;
   }
   if (!TRUTH_SUBJECT_CALL.matches(methodInvocationTree, state)) {
     return Description.NO_MATCH;
   }
   ExpressionTree rec = ASTHelpers.getReceiver(methodInvocationTree);
   if (rec == null) {
     return Description.NO_MATCH;
   }
   if (!ASSERT_THAT.matches(rec, state)) {
     return Description.NO_MATCH;
   }
   ExpressionTree expr = getOnlyElement(((MethodInvocationTree) rec).getArguments());
   if (expr == null) {
     return Description.NO_MATCH;
   }
   // check that argument of assertThat is a constant
   if (ASTHelpers.constValue(expr) == null) {
     return Description.NO_MATCH;
   }
   // check that expectation isn't a constant
   ExpressionTree expectation = getOnlyElement(methodInvocationTree.getArguments());
   if (ASTHelpers.constValue(expectation) != null) {
     return Description.NO_MATCH;
   }
   SuggestedFix fix = SuggestedFix.swap(expr, expectation);
   return buildDescription(methodInvocationTree).addFix(fix).build();
 }
Пример #2
0
  @Override
  public Description matchClass(ClassTree classTree, VisitorState state) {

    TypeSymbol symbol = ASTHelpers.getSymbol(classTree);
    if (symbol.getKind() != ElementKind.CLASS) {
      return Description.NO_MATCH;
    }

    MethodTree equals = null;
    for (Tree member : classTree.getMembers()) {
      if (!(member instanceof MethodTree)) {
        continue;
      }
      MethodTree methodTree = (MethodTree) member;
      if (EQUALS_MATCHER.matches(methodTree, state)) {
        equals = methodTree;
      }
    }
    if (equals == null) {
      return Description.NO_MATCH;
    }

    MethodSymbol hashCodeSym =
        ASTHelpers.resolveExistingMethod(
            state,
            symbol,
            state.getName("hashCode"),
            ImmutableList.<Type>of(),
            ImmutableList.<Type>of());

    if (hashCodeSym.owner.equals(state.getSymtab().objectType.tsym)) {
      return describeMatch(equals);
    }
    return Description.NO_MATCH;
  }
Пример #3
0
 private boolean anyCatchBlockMatches(TryTree tree, VisitorState state, Matcher<Tree> matcher) {
   for (CatchTree catchTree : tree.getCatches()) {
     if (matcher.matches(catchTree.getBlock(), state)) {
       return true;
     }
   }
   return false;
 }
Пример #4
0
 private boolean isInapplicableExceptionType(TryTree tree, VisitorState state) {
   for (CatchTree catchTree : tree.getCatches()) {
     if (INAPPLICABLE_EXCEPTION.matches(catchTree.getParameter(), state)) {
       return true;
     }
   }
   return false;
 }
Пример #5
0
  /**
   * Suggests replacing with Arrays.equals(a, b). Also adds the necessary import statement for
   * java.util.Arrays.
   */
  @Override
  public Description matchMethodInvocation(MethodInvocationTree t, VisitorState state) {
    String arg1;
    String arg2;
    if (instanceEqualsMatcher.matches(t, state)) {
      arg1 = ((JCFieldAccess) t.getMethodSelect()).getExpression().toString();
      arg2 = t.getArguments().get(0).toString();
    } else if (staticEqualsMatcher.matches(t, state)) {
      arg1 = t.getArguments().get(0).toString();
      arg2 = t.getArguments().get(1).toString();
    } else {
      return NO_MATCH;
    }

    Fix fix =
        SuggestedFix.builder()
            .replace(t, "Arrays.equals(" + arg1 + ", " + arg2 + ")")
            .addImport("java.util.Arrays")
            .build();
    return describeMatch(t, fix);
  }
Пример #6
0
 @Override
 public boolean matches(MethodInvocationTree methodInvocationTree, VisitorState state) {
   return methodSelectMatcher.matches(methodInvocationTree, state)
       && matches(methodInvocationTree.getArguments());
 }
Пример #7
0
 private boolean hasThrowOrFail(TryTree tree, VisitorState state) {
   return THROW_OR_FAIL_IN_BLOCK.matches(tree, state);
 }
Пример #8
0
 private boolean isInInapplicableMethod(TryTree tree, VisitorState state) {
   return NON_TEST_METHOD.matches(tree, state);
 }
Пример #9
0
 private boolean returnsInTryCatchOrAfter(TryTree tree, VisitorState state) {
   return RETURN_IN_BLOCK.matches(tree, state) || RETURN_AFTER.matches(tree, state);
 }
Пример #10
0
 private boolean hasContinue(TryTree tree, VisitorState state) {
   return CONTINUE_IN_BLOCK.matches(tree, state);
 }
Пример #11
0
 private boolean hasWhileTrue(TryTree tree, VisitorState state) {
   return WHILE_TRUE_IN_BLOCK.matches(tree, state);
 }