Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Document Empty Constructor In java by PMD

I am using PMD plugin in my Java Project.

When I run the PMD it shows the warning as "Document Empty Constructor ".

My code is as follows...

public class ExceptionHandlerImpl implements ExceptionHandler {

    private static final Logger log = Logger
            .getLogger(ExceptionHandlerImpl.class);

    /**
     * Default Constructor
     */
    public ExceptionHandlerImpl()
    {
        super();
    }

On the above constructor code it is showing "Document Empty Constructor".

How do I resolve this and why this is occurring?

like image 554
sam Avatar asked Jul 09 '26 05:07

sam


2 Answers

In your case the UncommentedEmptyConstructor rule is triggered.

It finds places where non-private constructor does not contain any statements or it contains just super() and there is no comment inside. The Javadoc before is not relevant for this rule.

By providing comments in empty constructors it is easier to distinguish between intentional (for example to be able to provide Javadoc) and unintentional empty constructors (someone forgot to write the implementation or this constructor can be just removed).

This rule expects from you something like that:

class MyConstructorIsNeededHere { 
  /** 
   * Creates instance of {@link Foo}.
   */ 
  MyConstructorIsNeededHere() { 
    // The explicit constructor is here, so that it is possible to provide Javadoc. 
  } 
} 

and warns you in the following case:

class OhNoICanBeRemoved { 
  OhNoICanBeRemoved() { 
    super();
  } 
} 
like image 56
Michal Kordas Avatar answered Jul 11 '26 20:07

Michal Kordas


You get the warning because the constructor you wrote does essentially the same as the implicit default constructor that Java would create. Hence you can just remove your constructor. If, however, there is any particular reason why you still want to keep it, you should document the reason in a comment. That is what PMD tells you.

like image 35
Nils Göde Avatar answered Jul 11 '26 21:07

Nils Göde



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!