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] JavaRuleViolation loses information in className field when class has package-private access level #1729

Closed
KroArtem opened this issue Mar 19, 2019 · 3 comments
Labels
a:bug PMD crashes or fails to analyse a file.
Milestone

Comments

@KroArtem
Copy link
Contributor

Affects PMD Version: 6.12

Rule: e.g UnusedUmportsRule

Description:
UnusedImportsRule warns about unused imports. When it founds an unused import, it adds it to a Set<ImportWrapper> imports and then adds violation. JavaRuleViolation has method setClassNameFrom, which (I believe) has the root of the problem.

Code Sample demonstrating the issue:

Here is this method that has the problem. If the class has default (package-private) access level, qualifiedName and className both remain null.

    private void setClassNameFrom(JavaNode node) {
        String qualifiedName = null;
        for (AbstractAnyTypeDeclaration parent : node.getParentsOfType(AbstractAnyTypeDeclaration.class)) {
            String clsName = parent.getScope().getEnclosingScope(ClassScope.class).getClassName();
            if (qualifiedName == null) {
                qualifiedName = clsName;
            } else {
                qualifiedName = clsName + '$' + qualifiedName;
            }
        }

        if (qualifiedName == null) {
            Set<ClassNameDeclaration> classes = node.getScope().getEnclosingScope(SourceFileScope.class)
                    .getClassDeclarations().keySet();
            for (ClassNameDeclaration c : classes) {
                // find the first public class/enum declaration
                if (c.getAccessNodeParent() instanceof AccessNode) {
                    if (((AccessNode) c.getAccessNodeParent()).isPublic()) {
                        qualifiedName = c.getImage();
                        break;
                    }
                }
            }
        }

        if (qualifiedName != null) {
            className = qualifiedName;
        }

Running PMD through: Other

@jsotuyod jsotuyod added the a:bug PMD crashes or fails to analyse a file. label Mar 31, 2019
@jsotuyod jsotuyod added this to the 6.14.0 milestone Mar 31, 2019
jsotuyod added a commit to Monits/pmd that referenced this issue Mar 31, 2019
@jsotuyod
Copy link
Member

@KroArtem thanks for the report! You are absolutely right about this, and you analysis was spot on.

I did however struggle a little understanding the issue at first. Having a code snippet + the generated report would have been a great aid.

@KroArtem
Copy link
Contributor Author

@jsotuyod , hello! Actually I was more or less ready to provide more info if only anybody asked for it. We're using PMD not as a maven goal but as a test that checks only changed files comparing to master. That's why I didn't have any report.

Next time I'd be glad to ask for more info and probably fix something by myself, though I wasn't sure where is the active developers' place (slack, irc or whatever). Now I see you're using sourceforge

@jsotuyod
Copy link
Member

@KroArtem don't worry, I'm looking forward for any contributions from you! We are on fitter. Check the Readme for a link.

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

2 participants