Search in sources :

Example 1 with MEMBER_SELECT

use of com.sun.source.tree.Tree.Kind.MEMBER_SELECT in project error-prone by google.

the class SelfAssignment method describeForAssignment.

/**
 * We expect that the lhs is a field and the rhs is an identifier, specifically a parameter to the
 * method. We base our suggested fixes on this expectation.
 *
 * <p>Case 1: If lhs is a field and rhs is an identifier, find a method parameter of the same type
 * and similar name and suggest it as the rhs. (Guess that they have misspelled the identifier.)
 *
 * <p>Case 2: If lhs is a field and rhs is not an identifier, find a method parameter of the same
 * type and similar name and suggest it as the rhs.
 *
 * <p>Case 3: If lhs is not a field and rhs is an identifier, find a class field of the same type
 * and similar name and suggest it as the lhs.
 *
 * <p>Case 4: Otherwise suggest deleting the assignment.
 */
public Description describeForAssignment(AssignmentTree assignmentTree, VisitorState state) {
    // the statement that is the parent of the self-assignment expression
    Tree parent = state.getPath().getParentPath().getLeaf();
    // default fix is to delete assignment
    Fix fix = SuggestedFix.delete(parent);
    ExpressionTree lhs = assignmentTree.getVariable();
    ExpressionTree rhs = assignmentTree.getExpression();
    // if this is a method invocation, they must be calling checkNotNull()
    if (assignmentTree.getExpression().getKind() == METHOD_INVOCATION) {
        // change the default fix to be "checkNotNull(x)" instead of "x = checkNotNull(x)"
        fix = SuggestedFix.replace(assignmentTree, rhs.toString());
        // new rhs is first argument to checkNotNull()
        rhs = stripNullCheck(rhs, state);
    }
    ImmutableList<Fix> exploratoryFieldFixes = ImmutableList.of();
    if (lhs.getKind() == MEMBER_SELECT) {
        // find a method parameter of the same type and similar name and suggest it
        // as the rhs
        // rhs should be either identifier or field access
        Preconditions.checkState(rhs.getKind() == IDENTIFIER || rhs.getKind() == MEMBER_SELECT);
        Type rhsType = ASTHelpers.getType(rhs);
        exploratoryFieldFixes = ReplacementVariableFinder.fixesByReplacingExpressionWithMethodParameter(rhs, varDecl -> ASTHelpers.isSameType(rhsType, varDecl.type, state), state);
    } else if (rhs.getKind() == IDENTIFIER) {
        // find a field of the same type and similar name and suggest it as the lhs
        // lhs should be identifier
        Preconditions.checkState(lhs.getKind() == IDENTIFIER);
        Type lhsType = ASTHelpers.getType(lhs);
        exploratoryFieldFixes = ReplacementVariableFinder.fixesByReplacingExpressionWithLocallyDeclaredField(lhs, var -> !Flags.isStatic(var.sym) && (var.sym.flags() & Flags.FINAL) == 0 && ASTHelpers.isSameType(lhsType, var.type, state), state);
    }
    if (exploratoryFieldFixes.isEmpty()) {
        return describeMatch(assignmentTree, fix);
    }
    return buildDescription(assignmentTree).addAllFixes(exploratoryFieldFixes).build();
}
Also used : Matchers.anyOf(com.google.errorprone.matchers.Matchers.anyOf) VariableTree(com.sun.source.tree.VariableTree) IDENTIFIER(com.sun.source.tree.Tree.Kind.IDENTIFIER) VisitorState(com.google.errorprone.VisitorState) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) AssignmentTree(com.sun.source.tree.AssignmentTree) ImmutableList(com.google.common.collect.ImmutableList) BugPattern(com.google.errorprone.BugPattern) JDK(com.google.errorprone.BugPattern.Category.JDK) Matcher(com.google.errorprone.matchers.Matcher) Fix(com.google.errorprone.fixes.Fix) Tree(com.sun.source.tree.Tree) Matchers.staticMethod(com.google.errorprone.matchers.Matchers.staticMethod) AssignmentTreeMatcher(com.google.errorprone.bugpatterns.BugChecker.AssignmentTreeMatcher) ERROR(com.google.errorprone.BugPattern.SeverityLevel.ERROR) MEMBER_SELECT(com.sun.source.tree.Tree.Kind.MEMBER_SELECT) ExpressionTree(com.sun.source.tree.ExpressionTree) METHOD_INVOCATION(com.sun.source.tree.Tree.Kind.METHOD_INVOCATION) Symbol(com.sun.tools.javac.code.Symbol) MemberSelectTree(com.sun.source.tree.MemberSelectTree) CLASS(com.sun.source.tree.Tree.Kind.CLASS) VariableTreeMatcher(com.google.errorprone.bugpatterns.BugChecker.VariableTreeMatcher) STATIC(javax.lang.model.element.Modifier.STATIC) Description(com.google.errorprone.matchers.Description) Preconditions(com.google.common.base.Preconditions) ProvidesFix(com.google.errorprone.BugPattern.ProvidesFix) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) Flags(com.sun.tools.javac.code.Flags) ASTHelpers(com.google.errorprone.util.ASTHelpers) Type(com.sun.tools.javac.code.Type) Type(com.sun.tools.javac.code.Type) Fix(com.google.errorprone.fixes.Fix) ProvidesFix(com.google.errorprone.BugPattern.ProvidesFix) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) VariableTree(com.sun.source.tree.VariableTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) AssignmentTree(com.sun.source.tree.AssignmentTree) Tree(com.sun.source.tree.Tree) ExpressionTree(com.sun.source.tree.ExpressionTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) ExpressionTree(com.sun.source.tree.ExpressionTree)

Aggregations

Preconditions (com.google.common.base.Preconditions)1 ImmutableList (com.google.common.collect.ImmutableList)1 BugPattern (com.google.errorprone.BugPattern)1 JDK (com.google.errorprone.BugPattern.Category.JDK)1 ProvidesFix (com.google.errorprone.BugPattern.ProvidesFix)1 ERROR (com.google.errorprone.BugPattern.SeverityLevel.ERROR)1 VisitorState (com.google.errorprone.VisitorState)1 AssignmentTreeMatcher (com.google.errorprone.bugpatterns.BugChecker.AssignmentTreeMatcher)1 VariableTreeMatcher (com.google.errorprone.bugpatterns.BugChecker.VariableTreeMatcher)1 Fix (com.google.errorprone.fixes.Fix)1 SuggestedFix (com.google.errorprone.fixes.SuggestedFix)1 Description (com.google.errorprone.matchers.Description)1 Matcher (com.google.errorprone.matchers.Matcher)1 Matchers.anyOf (com.google.errorprone.matchers.Matchers.anyOf)1 Matchers.staticMethod (com.google.errorprone.matchers.Matchers.staticMethod)1 ASTHelpers (com.google.errorprone.util.ASTHelpers)1 AssignmentTree (com.sun.source.tree.AssignmentTree)1 ExpressionTree (com.sun.source.tree.ExpressionTree)1 MemberSelectTree (com.sun.source.tree.MemberSelectTree)1 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)1