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

PrivateConstructorForUtilityClass flags @SpringBootApplication #4834

Open
commonquail opened this issue Feb 21, 2025 · 0 comments · May be fixed by #4849
Open

PrivateConstructorForUtilityClass flags @SpringBootApplication #4834

commonquail opened this issue Feb 21, 2025 · 0 comments · May be fixed by #4849

Comments

@commonquail
Copy link

A typical Spring Boot application will tend to have a class that looks like

@SpringBootApplication
public class App {
    public static void main(final String[] args) {
        SpringApplication.run(App.class, args);
    }
}

where delightful Spring magic relies on implicit extensibility to run the application. Crucially, it does not work with a private constructor or when the class is final.

For the code above, in v2.36.0, PrivateConstructorForUtilityClass produces this false positive violation:

$ ./mvnw clean test-compile -DepFlags='-XepDisableAllChecks -Xep:PrivateConstructorForUtilityClass:ERROR'
[INFO] Scanning for projects...
...
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /.../src/main/java/example/App.java:[7,8] [PrivateConstructorForUtilityClass] Classes which are not intended to be instantiated should be made non-instantiable with a private constructor. This includes utility classes (classes with only static members), and the main class.
    (see https://errorprone.info/bugpattern/PrivateConstructorForUtilityClass)
  Did you mean 'public final class App {'?

With an explicit public constructor, PrivateConstructorForUtilityClass shuts up and Spring Boot still works. However, I find that people tend to get confused by explicit public no-op constructors, and some linters flag them as redundant (incorrectly, IMO).

Although users can easily work around this false positive it would be nice if PrivateConstructorForUtilityClass could learn to recognize the @SpringBootApplication annotation.

commonquail added a commit to commonquail/error-prone that referenced this issue Feb 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant