Pmd: [java] CommentDefaultAccessModifierRule shows incorrect message

Created on 7 Nov 2018  路  6Comments  路  Source: pmd/pmd

Affects PMD Version:
6.9.0

Rule:
codestyle/CommentDefaultAccessmodifier

Description:

When reporting on a constructor, the error message is:

To avoid mistakes add a comment at the beginning of the null constructor if you want a default access modifier

It shouldn't say null, but the name of the constructor:

To avoid mistakes add a comment at the beginning of the Foo constructor if you want a default access modifier

Code Sample demonstrating the issue:

public class Foo {
    Foo() { }
}

Running PMD through: Any

bug good first issue pr

Most helpful comment

Hi,

Can i take up this issue?

All 6 comments

Hi,

Can i take up this issue?

@keerthikorivi of course you can! All PRs are welcomed

Hi @jsotuyod,

I just wanted to confirm my understanding of the issue, the issue is that when we create a class say, Foo, as shown below with a default constructor and use pmd 6.9.0 in the build then it fails with the error message: To avoid mistakes add a comment at the beginning of the null constructor if you want a default access modifier
Instead, it should specify the class name instead of null

public class Foo {
Foo() { }
}

Please let me know if that's correct?

@keerthikorivi exactly.

@jsotuyod I ran the code locally and saw the value of decl.getImage() is null.

@Override
public Object visit(final ASTClassOrInterfaceDeclaration decl, final Object data) {
// check for nested classes
if (decl.isNested() && shouldReport(decl)) {
addViolationWithMessage(data, decl, String.format(MESSAGE, decl.getImage(), "nested class"));
}
return super.visit(decl, data);
}

Going through the code I was not able to figure out where does this get set actually. Can you please point me to that code snippet. This should help me debug further.

@keerthikorivi the problem lies within the grammar itself, not setting the image for the node to the <IDENTIFIER>:

https://github.com/pmd/pmd/blob/1cfc477e4877696b6370ebd772fa7ef19a872647/pmd-java/etc/grammar/Java.jjt#L1852-L1857

You can see how this is done for other nodes such as:

https://github.com/pmd/pmd/blob/1cfc477e4877696b6370ebd772fa7ef19a872647/pmd-java/etc/grammar/Java.jjt#L1723

Was this page helpful?
0 / 5 - 0 ratings