Skip to content

Commit

Permalink
For NamedLikeContextualKeyword error prone check, don't alert on gene…
Browse files Browse the repository at this point in the history
…rated code from an @autovalue or @AutoOneOf class.

PiperOrigin-RevId: 549712144
  • Loading branch information
java-team-github-bot authored and Error Prone Team committed Jul 20, 2023
1 parent bd32be4 commit dad7174
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import com.sun.source.util.TreePath;
import com.sun.tools.javac.code.Symbol.ClassSymbol;
import com.sun.tools.javac.code.Symbol.MethodSymbol;
import java.util.Collections;
import javax.inject.Inject;

/**
Expand Down Expand Up @@ -81,6 +82,10 @@ public final class NamedLikeContextualKeyword extends BugChecker
"yield");
private static final Matcher<MethodTree> DISALLOWED_METHOD_NAME_MATCHER =
allOf(not(methodIsConstructor()), methodIsNamed("yield"));
private static final ImmutableSet<String> AUTO_PROCESSORS =
ImmutableSet.of(
"com.google.auto.value.processor.AutoValueProcessor",
"com.google.auto.value.processor.AutoOneOfProcessor");

private final boolean enableMethodNames;
private final boolean enableClassNames;
Expand All @@ -100,6 +105,12 @@ public Description matchMethod(MethodTree tree, VisitorState state) {
}

MethodSymbol methodSymbol = ASTHelpers.getSymbol(tree);

// Don't alert if an @Auto... class (safe since reference always qualified).
if (isInGeneratedAutoCode(state)) {
return NO_MATCH;
}

// Don't alert if method is an override (this includes interfaces)
if (!streamSuperMethods(methodSymbol, state.getTypes()).findAny().isPresent()
&& DISALLOWED_METHOD_NAME_MATCHER.matches(tree, state)) {
Expand Down Expand Up @@ -153,4 +164,8 @@ private static String getQualifier(
}
}
}

private static boolean isInGeneratedAutoCode(VisitorState state) {
return !Collections.disjoint(ASTHelpers.getGeneratedBy(state), AUTO_PROCESSORS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,67 @@ public void staticMethodName_error() {
.doTest();
}

@Test
public void autoOneOfMethodName_noError() {
helper
.addSourceLines(
"Test.java",
"import javax.annotation.processing.Generated;",
"@Generated(\"com.google.auto.value.processor.AutoOneOfProcessor\")",
"class Test {",
" static Throwable foo;",
" public Test() {",
" }",
" ",
" public static void yield() { ",
" foo = new NullPointerException(\"uh oh\");",
" }",
"}")
.setArgs("-XepOpt:NamedLikeContextualKeyword:EnableMethodNames")
.doTest();
}

@Test
public void autoValueMethodName_noError() {
helper
.addSourceLines(
"Test.java",
"import javax.annotation.processing.Generated;",
"@Generated(\"com.google.auto.value.processor.AutoValueProcessor\")",
"class Test {",
" static Throwable foo;",
" public Test() {",
" }",
" ",
" public static void yield() { ",
" foo = new NullPointerException(\"uh oh\");",
" }",
"}")
.setArgs("-XepOpt:NamedLikeContextualKeyword:EnableMethodNames")
.doTest();
}

@Test
public void generatedButNotAuto_error() {
helper
.addSourceLines(
"Test.java",
"import javax.annotation.processing.Generated;",
"@Generated(\"com.google.foo.Bar\")",
"class Test {",
" static Throwable foo;",
" public Test() {",
" }",
" ",
" // BUG: Diagnostic contains: [NamedLikeContextualKeyword]",
" public static void yield() { ",
" foo = new NullPointerException(\"uh oh\");",
" }",
"}")
.setArgs("-XepOpt:NamedLikeContextualKeyword:EnableMethodNames")
.doTest();
}

@Test
public void className_error() {
helper
Expand Down

0 comments on commit dad7174

Please sign in to comment.