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

[java] UnnecessaryConstructor vs. AtLeastOneConstructor #4914

Open
StevanWhite opened this issue Apr 3, 2024 · 0 comments
Open

[java] UnnecessaryConstructor vs. AtLeastOneConstructor #4914

StevanWhite opened this issue Apr 3, 2024 · 0 comments
Labels
a:bug PMD crashes or fails to analyse a file.

Comments

@StevanWhite
Copy link

Affects PMD Version:

7.0.0

Description:

Situation: a class whose elements are properly instantiated with a no-argument constructor.

If the code is left without a constructor, the compiler will provide one, but PMD complains:
Each class should have at least one constructor

If a constructor is provided in the code, PMD complains:
Avoid unnecessary constructors - the compiler will generate these for you

Sure, the programmer can insert @SuppressWarnings into (otherwise very simple) code,
but this really shouldn't be necessary. Sure, the programmer can disable either warning globally...
but in fact, both warnings are good to have.

But as it is, it leaves the programmer thinking: "Make up your mind!". And it wastes time.

I'm not sure what measure would be best to take.
Perhaps a property for the UnnecessaryConstructor rule,
"allowSingleConstructor", or something like that, would help.

Exception Stacktrace:

# Copy-paste the stack trace here

Code Sample demonstrating the issue:

/** An overly-simple example */
class ExampleClass {
        // If this constructor is present, PMD complains.
        // If this constructor is removed, PMD complains.
        ExampleClass(){
        }
}

The situation arises in many practical cases. For instance, the documentation for
javax.annotation.processing.Processor
explicitly states
"Each implementation of a Processor must provide a public no-argument constructor to be used by tools to instantiate the processor."

Steps to reproduce:

Please provide detailed steps for how we can reproduce the bug.

  1. ... Run PMD on a file named ExampleClass.java containing the above code
  2. ...Then delete the constructor, and try it again.

Running PMD through: [CLI | Ant | Maven | Gradle | Designer | Other]

Running PMD through CLI

@StevanWhite StevanWhite added the a:bug PMD crashes or fails to analyse a file. label Apr 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a:bug PMD crashes or fails to analyse a file.
Projects
None yet
Development

No branches or pull requests

1 participant