Skip to content

Commit ba8f9a2

Browse files
java-team-github-botError Prone Team
authored andcommitted
Do not update getters that override methods from a superclass.
The change would be incorrect as the modified method is no longer matching the signature of the method in the superclass. PiperOrigin-RevId: 660049434
1 parent a706e8d commit ba8f9a2

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

core/src/main/java/com/google/errorprone/bugpatterns/AutoValueBoxedValues.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static com.google.errorprone.BugPattern.SeverityLevel.WARNING;
2121
import static com.google.errorprone.matchers.Description.NO_MATCH;
2222
import static com.google.errorprone.matchers.Matchers.hasModifier;
23+
import static com.google.errorprone.util.ASTHelpers.findSuperMethods;
2324
import static com.google.errorprone.util.ASTHelpers.getSymbol;
2425
import static com.google.errorprone.util.ASTHelpers.getType;
2526
import static com.google.errorprone.util.ASTHelpers.hasAnnotation;
@@ -121,6 +122,7 @@ private Getter maybeFixGetter(MethodTree method, VisitorState state) {
121122
Type type = getType(method.getReturnType());
122123
if (!isSuppressed(method, state)
123124
&& !hasNullableAnnotation(method)
125+
&& !isOverride(method, state)
124126
&& isBoxedPrimitive(state, type)) {
125127
suggestRemoveUnnecessaryBoxing(method.getReturnType(), state, type, getter.fix());
126128
}
@@ -211,6 +213,11 @@ private static boolean hasNullableAnnotation(Tree tree) {
211213
return NullnessAnnotations.fromAnnotationsOn(getSymbol(tree)).orElse(null) == Nullness.NULLABLE;
212214
}
213215

216+
/** Returns true if the method overrides another method. */
217+
private static boolean isOverride(MethodTree methodTree, VisitorState state) {
218+
return !findSuperMethods(getSymbol(methodTree), state.getTypes()).isEmpty();
219+
}
220+
214221
/** Returns the primitive type corresponding to a boxed type. */
215222
private static Type unbox(VisitorState state, Type type) {
216223
return state.getTypes().unboxedType(type);

core/src/test/java/com/google/errorprone/bugpatterns/AutoValueBoxedValuesTest.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,64 @@ public void unnecessaryBoxedTypes_suppressWarnings() {
425425
.doTest();
426426
}
427427

428+
@Test
429+
public void unnecessaryBoxedTypes_overrides() {
430+
refactoringHelper
431+
.addInputLines(
432+
"in/Test.java",
433+
mergeLines(
434+
lines(
435+
"import com.google.auto.value.AutoValue;",
436+
"class Test {",
437+
" interface SuperClass {",
438+
" Long superClassLongId();",
439+
" }",
440+
" @AutoValue",
441+
" static abstract class BaseClass implements SuperClass {",
442+
" public abstract Long longId();",
443+
" @Override",
444+
" public abstract Long superClassLongId();"),
445+
linesWithoutBuilder(
446+
" static BaseClass create(Long longId, Long superClassLongId) {",
447+
" return new AutoValue_Test_BaseClass(longId, superClassLongId);",
448+
" }"),
449+
linesWithBuilder(
450+
" @AutoValue.Builder",
451+
" abstract static class Builder {",
452+
" abstract Builder setLongId(Long value);",
453+
" abstract Builder setSuperClassLongId(Long value);",
454+
" abstract BaseClass build();",
455+
" }"),
456+
lines(" }", "}")))
457+
.addOutputLines(
458+
"out/Test.java",
459+
mergeLines(
460+
lines(
461+
"import com.google.auto.value.AutoValue;",
462+
"class Test {",
463+
" interface SuperClass {",
464+
" Long superClassLongId();",
465+
" }",
466+
" @AutoValue",
467+
" static abstract class BaseClass implements SuperClass {",
468+
" public abstract long longId();",
469+
" @Override",
470+
" public abstract Long superClassLongId();"),
471+
linesWithoutBuilder(
472+
" static BaseClass create(long longId, Long superClassLongId) {",
473+
" return new AutoValue_Test_BaseClass(longId, superClassLongId);",
474+
" }"),
475+
linesWithBuilder(
476+
" @AutoValue.Builder",
477+
" abstract static class Builder {",
478+
" abstract Builder setLongId(long value);",
479+
" abstract Builder setSuperClassLongId(Long value);",
480+
" abstract BaseClass build();",
481+
" }"),
482+
lines(" }", "}")))
483+
.doTest();
484+
}
485+
428486
@Test
429487
public void nullableGettersWithNonNullableSetters_noChange() {
430488
if (!withBuilder) {

0 commit comments

Comments
 (0)