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

ClassCanBeStatic: Exclude JUnit @Nested classes #3654

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
Original file line number Diff line number Diff line change
Expand Up @@ -1041,7 +1041,9 @@ public static boolean shouldKeep(Tree tree) {
* though they are.
*/
private static final ImmutableSet<String> ANNOTATIONS_CONSIDERED_KEEP =
ImmutableSet.of("org.apache.beam.sdk.transforms.DoFn.ProcessElement");
ImmutableSet.of(
"org.apache.beam.sdk.transforms.DoFn.ProcessElement",
"org.junit.jupiter.api.Nested");

private static final String USED_REFLECTIVELY = "UsedReflectively";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ public Description matchClass(ClassTree tree, VisitorState state) {
if (CanBeStaticAnalyzer.referencesOuter(tree, currentClass, state)) {
return NO_MATCH;
}
if (ASTHelpers.shouldKeep(tree)) {
return NO_MATCH;
}
if (tree.getMembers().stream().anyMatch(m -> hasAnnotation(m, REFASTER_ANNOTATION, state))) {
return NO_MATCH;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -401,4 +401,28 @@ public void refaster() {
"}")
.doTest();
}

@Test
public void junitNestedClass() {
compilationHelper
.addSourceLines(
"Nested.java",
"package org.junit.jupiter.api;",
"import java.lang.annotation.ElementType;",
"import java.lang.annotation.Retention;",
"import java.lang.annotation.RetentionPolicy;",
"import java.lang.annotation.Target;",
"@Target(ElementType.TYPE)",
"@Retention(RetentionPolicy.RUNTIME)",
"public @interface Nested {}")
.addSourceLines(
"A.java",
"import org.junit.jupiter.api.Nested;",
"public class A {",
" @Nested class Inner {",
" void f() {}",
" }",
"}")
.doTest();
}
}