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

Don't report NonFinalStaticField findings for fields modified in @BeforeClass methods #4215

Merged
merged 1 commit into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,16 @@
import com.google.errorprone.bugpatterns.BugChecker.VariableTreeMatcher;
import com.google.errorprone.fixes.SuggestedFix;
import com.google.errorprone.matchers.Description;
import com.google.errorprone.util.ASTHelpers;
import com.sun.source.tree.AssignmentTree;
import com.sun.source.tree.CompoundAssignmentTree;
import com.sun.source.tree.MethodTree;
import com.sun.source.tree.Tree.Kind;
import com.sun.source.tree.UnaryTree;
import com.sun.source.tree.VariableTree;
import com.sun.source.util.TreeScanner;
import com.sun.tools.javac.code.Symbol.VarSymbol;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.lang.model.element.Modifier;

/** A BugPattern; see the summary. */
Expand All @@ -71,7 +72,11 @@ public Description matchVariable(VariableTree tree, VisitorState state) {
.anyMatch(anno -> hasDirectAnnotationWithSimpleName(tree, anno))) {
return NO_MATCH;
}
if (!canBeRemoved(symbol, state) || isEverMutatedInSameCompilationUnit(symbol, state)) {
IsMutated everMutatedInSameCompilationUnit = isEverMutatedInSameCompilationUnit(symbol, state);
if (everMutatedInSameCompilationUnit == IsMutated.IN_BEFORE_METHOD) {
return NO_MATCH;
}
if (!canBeRemoved(symbol, state) || everMutatedInSameCompilationUnit == IsMutated.TRUE) {
return describeMatch(tree);
}
return describeMatch(
Expand Down Expand Up @@ -117,45 +122,77 @@ private static String getDefaultInitializer(VariableTree tree, VisitorState stat
return "null";
}

private static boolean isEverMutatedInSameCompilationUnit(VarSymbol symbol, VisitorState state) {
AtomicBoolean seen = new AtomicBoolean(false);
new TreeScanner<Void, Void>() {
@Override
public Void visitAssignment(AssignmentTree tree, Void unused) {
if (Objects.equals(getSymbol(tree.getVariable()), symbol)) {
seen.set(true);
}
return super.visitAssignment(tree, null);
}

@Override
public Void visitCompoundAssignment(CompoundAssignmentTree tree, Void unused) {
if (Objects.equals(getSymbol(tree.getVariable()), symbol)) {
seen.set(true);
}
return super.visitCompoundAssignment(tree, null);
}

@Override
public Void visitUnary(UnaryTree tree, Void unused) {
if (Objects.equals(getSymbol(tree.getExpression()), symbol) && isMutating(tree.getKind())) {
seen.set(true);
}
return super.visitUnary(tree, null);
}

private boolean isMutating(Kind kind) {
switch (kind) {
case POSTFIX_DECREMENT:
case POSTFIX_INCREMENT:
case PREFIX_DECREMENT:
case PREFIX_INCREMENT:
return true;
default:
return false;
}
}
}.scan(state.getPath().getCompilationUnit(), null);
return seen.get();
enum IsMutated {
TRUE,
FALSE,
IN_BEFORE_METHOD
}

private static IsMutated isEverMutatedInSameCompilationUnit(
VarSymbol symbol, VisitorState state) {
var scanner =
new TreeScanner<Void, Void>() {
IsMutated isMutated = IsMutated.FALSE;

boolean inBeforeMethod = false;

@Override
public Void visitAssignment(AssignmentTree tree, Void unused) {
if (Objects.equals(getSymbol(tree.getVariable()), symbol)) {
isMutated();
}
return super.visitAssignment(tree, null);
}

@Override
public Void visitCompoundAssignment(CompoundAssignmentTree tree, Void unused) {
if (Objects.equals(getSymbol(tree.getVariable()), symbol)) {
isMutated();
}
return super.visitCompoundAssignment(tree, null);
}

@Override
public Void visitUnary(UnaryTree tree, Void unused) {
if (Objects.equals(getSymbol(tree.getExpression()), symbol)
&& isMutating(tree.getKind())) {
isMutated();
}
return super.visitUnary(tree, null);
}

private void isMutated() {
if (inBeforeMethod) {
isMutated = IsMutated.IN_BEFORE_METHOD;
} else if (isMutated.equals(IsMutated.FALSE)) {
isMutated = IsMutated.TRUE;
}
}

private boolean isMutating(Kind kind) {
switch (kind) {
case POSTFIX_DECREMENT:
case POSTFIX_INCREMENT:
case PREFIX_DECREMENT:
case PREFIX_INCREMENT:
return true;
default:
return false;
}
}

@Override
public Void visitMethod(MethodTree tree, Void unused) {
boolean prev = inBeforeMethod;
try {
inBeforeMethod |= ASTHelpers.hasAnnotation(tree, "org.junit.BeforeClass", state);
return super.visitMethod(tree, null);
} finally {
inBeforeMethod = prev;
}
}
};
scanner.scan(state.getPath().getCompilationUnit(), null);
return scanner.isMutated;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -211,4 +211,20 @@ public void volatileRemoved() {
"}")
.doTest();
}

@Test
public void beforeClass() {
compilationTestHelper
.addSourceLines(
"Test.java", //
"import org.junit.BeforeClass;",
"public class Test {",
" private static String foo;",
" @BeforeClass",
" public static void setup() {",
" foo = \"\";",
" }",
"}")
.doTest();
}
}
Loading