Skip to content

Commit

Permalink
Don't flag redundant override if annotations for parameters aren't same.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 413869517
  • Loading branch information
sumitbhagwani authored and Error Prone Team committed Dec 3, 2021
1 parent 86ee549 commit aa8f00b
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import com.sun.tools.javac.api.JavacTrees;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Symbol.MethodSymbol;
import com.sun.tools.javac.code.Symbol.VarSymbol;
import java.util.Objects;
import java.util.Optional;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -108,12 +109,18 @@ public Description matchMethod(MethodTree tree, VisitorState state) {
.isEmpty()) {
return NO_MATCH;
}
// we're matching parameter by parameter.
for (int i = 0; i < tree.getParameters().size(); ++i) {
if (!(methodInvocationTree.getArguments().get(i) instanceof IdentifierTree)) {
return NO_MATCH;
}
if (!getSymbol(tree.getParameters().get(i))
.equals(getSymbol(methodInvocationTree.getArguments().get(i)))) {
VarSymbol varSymbol = getSymbol(tree.getParameters().get(i));
if (!varSymbol.equals(getSymbol(methodInvocationTree.getArguments().get(i)))) {
return NO_MATCH;
}
ImmutableSet<Symbol> paramAnnotations = getAnnotations(varSymbol);
ImmutableSet<Symbol> superParamAnnotations = getAnnotations(superMethod.params.get(i));
if (!superParamAnnotations.equals(paramAnnotations)) {
return NO_MATCH;
}
}
Expand Down Expand Up @@ -150,7 +157,7 @@ public MethodInvocationTree visitMethodInvocation(
null);
}

private static ImmutableSet<Symbol> getAnnotations(MethodSymbol symbol) {
private static ImmutableSet<Symbol> getAnnotations(Symbol symbol) {
return symbol.getRawAttributes().stream().map(a -> a.type.tsym).collect(toImmutableSet());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -235,4 +235,54 @@ public void protectedOverrideInSamePackage() {
"}")
.doTest();
}

@Test
public void paramAnnotationAddedInOverride() {
testHelper
.addSourceLines(
"DemoAnnotation.java", //
"package foo;",
"@interface DemoAnnotation {}")
.addSourceLines(
"A.java", //
"package foo;",
"class A {",
" protected void swap(int a) {}",
"}")
.addSourceLines(
"B.java",
"package foo;",
"class B extends A {",
" @Override",
" protected void swap(@DemoAnnotation int a) {",
" super.swap(a);",
" }",
"}")
.doTest();
}

@Test
public void paramAnnotationOmittedInOverride() {
testHelper
.addSourceLines(
"DemoAnnotation.java", //
"package foo;",
"@interface DemoAnnotation {}")
.addSourceLines(
"A.java", //
"package foo;",
"class A {",
" protected void swap(@DemoAnnotation int a) {}",
"}")
.addSourceLines(
"B.java",
"package foo;",
"class B extends A {",
" @Override",
" protected void swap(int a) {",
" super.swap(a);",
" }",
"}")
.doTest();
}
}

0 comments on commit aa8f00b

Please sign in to comment.