Skip to content

Commit

Permalink
Automatic code cleanup.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 474860734
  • Loading branch information
cushon authored and Error Prone Team committed Sep 16, 2022
1 parent 0bd03dd commit ceaf641
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,12 @@
import com.sun.source.tree.LiteralTree;
import com.sun.source.tree.NewClassTree;
import com.sun.source.tree.Tree;
import com.sun.tools.javac.code.Source;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Symtab;
import com.sun.tools.javac.code.Type;
import com.sun.tools.javac.code.Types;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.JCTree.JCMethodInvocation;
import com.sun.tools.javac.util.Context;
import javax.annotation.Nullable;

/** A {@link BugChecker}; see the associated {@link BugPattern} annotation for details. */
Expand Down Expand Up @@ -128,7 +126,6 @@ private static Fix buildFix(NewClassTree tree, VisitorState state) {
if (HASH_CODE.matches(parent, state)) {
// e.g. new Integer($A).hashCode() -> Integer.hashCode($A)
SuggestedFix.Builder fix = SuggestedFix.builder();
String replacement;

String optionalCast = "";
String optionalSuffix = "";
Expand All @@ -145,14 +142,7 @@ private static Fix buildFix(NewClassTree tree, VisitorState state) {
break;
}

// In JDK8, there are primitive static methods on each type (Long.hashCode($long)). However,
// in Java 7, those don't exist, so we suggest Guava
if (shouldUseGuavaHashCode(state.context)) {
fix.addImport("com.google.common.primitives." + typeName + "s");
replacement = String.format("%ss.hashCode(", typeName);
} else {
replacement = String.format("%s.hashCode(", typeName);
}
String replacement = String.format("%s.hashCode(", typeName);
return fix.replace(
parent.getStartPosition(), arg.getStartPosition(), replacement + optionalCast)
.replace(state.getEndPosition(arg), state.getEndPosition(parent), optionalSuffix + ")")
Expand Down Expand Up @@ -218,10 +208,6 @@ private static Fix buildFix(NewClassTree tree, VisitorState state) {
.build();
}

private static boolean shouldUseGuavaHashCode(Context context) {
return Source.instance(context).compareTo(Source.lookup("1.7")) <= 0; // 7 or below
}

private static String maybeCast(VisitorState state, Type type, Type argType) {
if (doubleAndFloatStatus(state, type, argType)
== DoubleAndFloatStatus.PRIMITIVE_DOUBLE_INTO_FLOAT) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package com.google.errorprone.bugpatterns;

import com.google.errorprone.CompilationTestHelper;
import java.util.Arrays;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -229,22 +228,6 @@ public void testHashCode() {
.doTest();
}

@Test
public void hashCodeInJava7() {
compilationHelper
.setArgs(Arrays.asList("-source", "7", "-target", "7"))
.addSourceLines(
"Test.java",
"public abstract class Test {",
" abstract int g(Integer x);",
" int f(long z) {",
" // BUG: Diagnostic contains: return Longs.hashCode(z);",
" return new Long(z).hashCode();",
" }",
"}")
.doTest();
}

public static class Super {}

public static class Inner extends Super {}
Expand Down

0 comments on commit ceaf641

Please sign in to comment.