A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/pmd/pmd/issues/1729 below:

[java] JavaRuleViolation loses information in `className` field when class has package-private access level · Issue #1729 · pmd/pmd · GitHub

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


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4