Skip to content
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 @@ -35,7 +35,7 @@

public class FinalClassVisitor extends JavaIsoVisitor<ExecutionContext> {

private static final AnnotationMatcher CONFIGURATION_ANNOTATION = new AnnotationMatcher("@org.springframework.context.annotation.Configuration");
private static final AnnotationMatcher CONFIGURATION_ANNOTATION = new AnnotationMatcher("@org.springframework.context.annotation.Configuration", true);

Tree visitRoot;

Expand Down Expand Up @@ -75,7 +75,7 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDeclarat
return cd;
}

// Spring @Configuration classes are proxied at runtime and must not be final
// Spring @Configuration classes (including meta-annotated ones like @TestConfiguration / @SpringBootApplication) are proxied at runtime and must not be final
if (cd.getLeadingAnnotations().stream().anyMatch(a -> CONFIGURATION_ANNOTATION.matches(a))) {
return cd;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, P
* Until then, however, we'll keep this private and unexposed.
*/
private final class UtilityClassMatcher {
private final AnnotationMatcher configurationAnnotation = new AnnotationMatcher("@org.springframework.context.annotation.Configuration");
private final AnnotationMatcher configurationAnnotation = new AnnotationMatcher("@org.springframework.context.annotation.Configuration", true);
private final Collection<AnnotationMatcher> ignorableAnnotations;

private UtilityClassMatcher(Collection<String> ignorableAnnotations) {
Expand Down
34 changes: 34 additions & 0 deletions src/test/java/org/openrewrite/staticanalysis/FinalClassTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,40 @@ private MyConfig() {}
);
}

@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/729")
@Test
void doNotFinalizeSpringTestConfigurationClass() {
rewriteRun(
//language=java
java(
"""
package org.springframework.context.annotation;
public @interface Configuration {}
"""
),
//language=java
java(
"""
package org.springframework.boot.test.context;
import org.springframework.context.annotation.Configuration;
@Configuration
public @interface TestConfiguration {}
"""
),
//language=java
java(
"""
import org.springframework.boot.test.context.TestConfiguration;

@TestConfiguration
class MyTestConfig {
private MyTestConfig() {}
}
"""
)
);
}

@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/372")
@Test
void doNotFinalizeClassWithNestedStaticFinalSubclass() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,42 @@ public static String someBean() {
);
}

@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/729")
@Test
void doNotChangeSpringTestConfigurationClass() {
rewriteRun(
//language=java
java(
"""
package org.springframework.context.annotation;
public @interface Configuration {}
"""
),
//language=java
java(
"""
package org.springframework.boot.test.context;
import org.springframework.context.annotation.Configuration;
@Configuration
public @interface TestConfiguration {}
"""
),
//language=java
java(
"""
import org.springframework.boot.test.context.TestConfiguration;

@TestConfiguration
class MyTestConfig {
public static String someBean() {
return "bean";
}
}
"""
)
);
}

private static Consumer<RecipeSpec> hideUtilityClassConstructor(String... ignoreIfAnnotatedBy) {
return spec -> spec.parser(JavaParser.fromJavaVersion().styles(
singletonList(
Expand Down
Loading