rewrite-static-analysis icon indicating copy to clipboard operation
rewrite-static-analysis copied to clipboard

`FinalClass` and `HideUtilityClassConstructor` should not apply for Spring Configuration classes

Open kthoms opened this issue 3 months ago • 2 comments

The recipes org.openrewrite.staticanalysis.FinalClass and org.openrewrite.staticanalysis.HideUtilityClassConstructor must not be applied on classes that are annotated with org.springframework.context.annotation.Configuration.

Image

What version of OpenRewrite are you using?

I am using

  • Maven/Gradle plugin v6.17.0
  • rewrite-static-analysis v2.16.0

How are you running OpenRewrite?

I am using the Maven plugin. The project can be checked out from https://github.com/operaton/operaton.git.

Remove the exclusions for FinalClass from /pom.xml and run

./mvnw -U org.openrewrite.maven:rewrite-maven-plugin:run

What is the smallest, simplest way to reproduce the problem?

@org.springframework.context.annotation.Configuration
class A {
}

What did you expect to see?

No change.

What did you see instead?

@org.springframework.context.annotation.Configuration
final class A {
}

or for HideUtilityClassConstructor

@org.springframework.context.annotation.Configuration
class A {
  private A () {}
}

Are you interested in contributing a fix to OpenRewrite?

Sorry, too busy for that ATM. We have excluded the few cases as workaround for the moment and are fine with that.

kthoms avatar Sep 01 '25 21:09 kthoms

Thanks for the report! For HideUtilityClassConstructor there is already the option to provide a style with annotation classes that should not see their default constructor hidden: https://github.com/openrewrite/rewrite/blob/03c458404073082275a752063887c188d213afbe/rewrite-java/src/main/java/org/openrewrite/java/style/HideUtilityClassConstructorStyle.java#L27-L33 https://github.com/openrewrite/rewrite-static-analysis/blob/8e44876b985c0afd190faffe8516da68952b7954/src/main/java/org/openrewrite/staticanalysis/HideUtilityClassConstructor.java#L59-L63

For FinalClass we don't yet have something similar; we could start out with not having that make any changes if any annotations are found, as to not make this Spring specific in rewrite-static-analysis.

timtebeek avatar Sep 02 '25 08:09 timtebeek

  • Added excludeAnnotations in FinalClass in https://github.com/openrewrite/rewrite-static-analysis/pull/735

e5LA avatar Sep 08 '25 17:09 e5LA