Skip to content

Commit

Permalink
MissingOverride: ignore generated code.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 344778138
  • Loading branch information
graememorgan authored and Error Prone Team committed Nov 30, 2020
1 parent aeafd79 commit d41b86f
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ public Description matchMethod(MethodTree tree, VisitorState state) {
if (override == null) {
return Description.NO_MATCH;
}
if (!ASTHelpers.getGeneratedBy(state).isEmpty()) {
return Description.NO_MATCH;
}
if (ASTHelpers.hasAnnotation(override, Deprecated.class, state)) {
// to allow deprecated methods to be removed non-atomically, we permit overrides of
// @Deprecated to skip the annotation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.google.common.collect.ImmutableList;
import com.google.errorprone.CompilationTestHelper;
import com.google.errorprone.util.RuntimeVersion;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
Expand All @@ -32,7 +33,11 @@ public class MissingOverrideTest {
@Test
public void simple() {
compilationHelper
.addSourceLines("Super.java", "public class Super {", " void f() {}", "}")
.addSourceLines(
"Super.java", //
"public class Super {",
" void f() {}",
"}")
.addSourceLines(
"Test.java",
"public class Test extends Super {",
Expand All @@ -45,7 +50,11 @@ public void simple() {
@Test
public void abstractMethod() {
compilationHelper
.addSourceLines("Super.java", "public abstract class Super {", " abstract void f();", "}")
.addSourceLines(
"Super.java", //
"public abstract class Super {",
" abstract void f();",
"}")
.addSourceLines(
"Test.java",
"public class Test extends Super {",
Expand All @@ -55,10 +64,29 @@ public void abstractMethod() {
.doTest();
}

@Test
public void generatedMethod() {
compilationHelper
.addSourceLines(
"Test.java", //
(RuntimeVersion.isAtLeast9()
? "import javax.annotation.processing.Generated;"
: "import javax.annotation.Generated;"),
"@Generated(\"foo\")",
"public abstract class Test {",
" public abstract int hashCode();",
"}")
.doTest();
}

@Test
public void interfaceMethod() {
compilationHelper
.addSourceLines("Super.java", "interface Super {", " void f();", "}")
.addSourceLines(
"Super.java", //
"interface Super {",
" void f();",
"}")
.addSourceLines(
"Test.java",
"public class Test implements Super {",
Expand All @@ -71,25 +99,43 @@ public void interfaceMethod() {
@Test
public void bothStatic() {
compilationHelper
.addSourceLines("Super.java", "public class Super {", " static void f() {}", "}")
.addSourceLines(
"Test.java", "public class Test extends Super {", " static public void f() {}", "}")
"Super.java", //
"public class Super {",
" static void f() {}",
"}")
.addSourceLines(
"Test.java", //
"public class Test extends Super {",
" static public void f() {}",
"}")
.doTest();
}

@Test
public void deprecatedMethod() {
compilationHelper
.addSourceLines("Super.java", "public class Super {", " @Deprecated void f() {}", "}")
.addSourceLines(
"Test.java", "public class Test extends Super {", " public void f() {}", "}")
"Super.java", //
"public class Super {",
" @Deprecated void f() {}",
"}")
.addSourceLines(
"Test.java", //
"public class Test extends Super {",
" public void f() {}",
"}")
.doTest();
}

@Test
public void interfaceOverride() {
compilationHelper
.addSourceLines("Super.java", "interface Super {", " void f();", "}")
.addSourceLines(
"Super.java", //
"interface Super {",
" void f();",
"}")
.addSourceLines(
"Test.java",
"public interface Test extends Super {",
Expand All @@ -103,8 +149,16 @@ public void interfaceOverride() {
public void ignoreInterfaceOverride() {
compilationHelper
.setArgs(ImmutableList.of("-XepOpt:MissingOverride:IgnoreInterfaceOverrides=true"))
.addSourceLines("Super.java", "interface Super {", " void f();", "}")
.addSourceLines("Test.java", "public interface Test extends Super {", " void f();", "}")
.addSourceLines(
"Super.java", //
"interface Super {",
" void f();",
"}")
.addSourceLines(
"Test.java", //
"public interface Test extends Super {",
" void f();",
"}")
.doTest();
}
}

0 comments on commit d41b86f

Please sign in to comment.