From 812dc0aaef9e7c81e616122d783f56595ba041ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20=C5=9Eener?= Date: Fri, 12 Jan 2024 15:46:47 -0800 Subject: [PATCH] Do not report `NonFinalStaticField` findings for fields modified in `@BeforeAll` methods Analogous to https://github.com/google/error-prone/pull/4215. Resolves https://github.com/google/error-prone/issues/4239. Fixes #4251 FUTURE_COPYBARA_INTEGRATE_REVIEW=https://github.com/google/error-prone/pull/4251 from DataDog:halil.sener/4239-non-final-static-field-junit-5-before-all 8a109a86455c46f8b2b7afdce4039121b2394ac4 PiperOrigin-RevId: 597978105 --- .../bugpatterns/NonFinalStaticField.java | 7 ++++- .../bugpatterns/NonFinalStaticFieldTest.java | 31 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/NonFinalStaticField.java b/core/src/main/java/com/google/errorprone/bugpatterns/NonFinalStaticField.java index 78622b104d0..e99e919a0ad 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/NonFinalStaticField.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/NonFinalStaticField.java @@ -56,6 +56,9 @@ public final class NonFinalStaticField extends BugChecker implements VariableTre private static final ImmutableSet ANNOTATIONS_TO_AVOID = ImmutableSet.of("Captor", "Inject", "Mock", "TestParameter"); + private static final ImmutableSet BEFORE_ALL_METHOD_ANNOTATIONS = + ImmutableSet.of("org.junit.BeforeClass", "org.junit.jupiter.api.BeforeAll"); + @Override public Description matchVariable(VariableTree tree, VisitorState state) { var symbol = getSymbol(tree); @@ -185,7 +188,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; diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/NonFinalStaticFieldTest.java b/core/src/test/java/com/google/errorprone/bugpatterns/NonFinalStaticFieldTest.java index 6e350f629a6..ef1675eeaf7 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/NonFinalStaticFieldTest.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/NonFinalStaticFieldTest.java @@ -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(); + } }