Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import static com.google.errorprone.matchers.Matchers.instanceMethod;
import static com.google.errorprone.matchers.Matchers.staticMethod;
import static com.google.errorprone.predicates.TypePredicates.isArray;
import static com.google.errorprone.util.ASTHelpers.getType;

import com.google.common.base.Preconditions;
import com.google.errorprone.BugPattern;
Expand All @@ -33,7 +34,6 @@
import com.google.errorprone.matchers.Description;
import com.google.errorprone.matchers.Matcher;
import com.google.errorprone.matchers.Matchers;
import com.google.errorprone.util.ASTHelpers;
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.MethodInvocationTree;
import com.sun.tools.javac.code.Type;
Expand Down Expand Up @@ -81,22 +81,17 @@ public class ArrayHashCode extends BugChecker implements MethodInvocationTreeMat
*/
@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
SuggestedFix.Builder fix = null;
var fix = SuggestedFix.builder();
Types types = state.getTypes();
if (jdk7HashCodeMethodMatcher.matches(tree, state)) {
// java.util.Objects#hashCode takes a single argument, so rewrite the whole method call
// to use Arrays.hashCode/deepHashCode instead.
fix =
SuggestedFix.builder()
.replace(tree, rewriteArrayArgument(tree.getArguments().get(0), state));
fix.replace(tree, rewriteArrayArgument(tree.getArguments().get(0), state));
} else if (instanceHashCodeMethodMatcher.matches(tree, state)) {
// Rewrite call to instance hashCode method to use Arrays.hashCode/deepHashCode instead.
fix =
SuggestedFix.builder()
.replace(
tree,
rewriteArrayArgument(
((JCFieldAccess) tree.getMethodSelect()).getExpression(), state));
fix.replace(
tree,
rewriteArrayArgument(((JCFieldAccess) tree.getMethodSelect()).getExpression(), state));
} else if (varargsHashCodeMethodMatcher.matches(tree, state)) {
// Varargs hash code methods, java.util.Objects#hash and
// com.google.common.base.Objects#hashCode
Expand All @@ -105,22 +100,21 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState
// Types like Object[], String[], etc. are not an error because they don't get boxed
// in this single-argument varargs call.
ExpressionTree arg = tree.getArguments().get(0);
Type elemType = types.elemtype(ASTHelpers.getType(arg));
Type elemType = types.elemtype(getType(arg));
if (elemType.isPrimitive() || types.isArray(elemType)) {
fix = SuggestedFix.builder().replace(tree, rewriteArrayArgument(arg, state));
fix.replace(tree, rewriteArrayArgument(arg, state));
}
} else {
// If more than one argument, wrap each argument in a call to Arrays#hashCode/deepHashCode.
fix = SuggestedFix.builder();
for (ExpressionTree arg : tree.getArguments()) {
if (types.isArray(ASTHelpers.getType(arg))) {
if (types.isArray(getType(arg))) {
fix.replace(arg, rewriteArrayArgument(arg, state));
}
}
}
}

if (fix != null) {
if (!fix.isEmpty()) {
fix.addImport("java.util.Arrays");
return describeMatch(tree, fix.build());
}
Expand All @@ -135,7 +129,7 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState
*/
private static String rewriteArrayArgument(ExpressionTree arg, VisitorState state) {
Types types = state.getTypes();
Type argType = ASTHelpers.getType(arg);
Type argType = getType(arg);
Preconditions.checkState(types.isArray(argType), "arg must be of array type");
if (types.isArray(types.elemtype(argType))) {
return "Arrays.deepHashCode(" + state.getSourceForNode(arg) + ")";
Expand Down
Loading