Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not report NonFinalStaticField findings for fields modified in @BeforeAll methods #4251

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -55,6 +55,8 @@
public final class NonFinalStaticField extends BugChecker implements VariableTreeMatcher {
private static final ImmutableSet<String> ANNOTATIONS_TO_AVOID =
ImmutableSet.of("Captor", "Inject", "Mock", "TestParameter");
private static final ImmutableSet<String> BEFORE_ALL_METHOD_ANNOTATIONS =
ImmutableSet.of("org.junit.BeforeClass", "org.junit.jupiter.api.BeforeAll");

@Override
public Description matchVariable(VariableTree tree, VisitorState state) {
Expand Down Expand Up @@ -185,7 +187,9 @@ private boolean isMutating(Kind kind) {
public Void visitMethod(MethodTree tree, Void unused) {
boolean prev = inBeforeMethod;
try {
inBeforeMethod |= ASTHelpers.hasAnnotation(tree, "org.junit.BeforeClass", state);
inBeforeMethod |=
BEFORE_ALL_METHOD_ANNOTATIONS.stream()
.anyMatch(annotation -> ASTHelpers.hasAnnotation(tree, annotation, state));
return super.visitMethod(tree, null);
} finally {
inBeforeMethod = prev;
Expand Down
Expand Up @@ -227,4 +227,35 @@ public void beforeClass() {
"}")
.doTest();
}

@Test
public void beforeAll() {
compilationTestHelper
.addSourceLines(
"org/junit/jupiter/api/BeforeAll.java",
"package org.junit.jupiter.api;",
"",
"import java.lang.annotation.Documented;",
"import java.lang.annotation.ElementType;",
"import java.lang.annotation.Retention;",
"import java.lang.annotation.RetentionPolicy;",
"import java.lang.annotation.Target;",
"",
"@Target({ ElementType.ANNOTATION_TYPE, ElementType.METHOD })",
"@Retention(RetentionPolicy.RUNTIME)",
"@Documented",
"public @interface BeforeAll {",
"}")
.addSourceLines(
"Test.java", //
"import org.junit.jupiter.api.BeforeAll;",
"public class Test {",
" private static String foo;",
" @BeforeAll",
" public static void setup() {",
" foo = \"\";",
" }",
"}")
.doTest();
}
}