pmd / pmd

An extensible multilanguage static code analyzer.

Home Page:https://pmd.github.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[java] ClassNamingConventions uses wrong pattern for interfaces

zman0900 opened this issue · comments

Affects PMD Version: 7.0.0

Description:

When the ClassNamingConventions rule is configured with a custom naming pattern for abstract classes, it reports all interfaces as in violation of that rule.

Dumping AST for an interface shows that they all seem to be also marked as abstract.

<ClassDeclaration Abstract='true' Annotation='false' Anonymous='false' BinaryName='sample.Example' CanonicalName='sample.Example' EffectiveVisibility='public' Enum='false' Final='false' Image='' Interface='true' Local='false' Nested='false' PackageName='sample' Record='false' RegularClass='false' RegularInterface='true' SimpleName='Example' Static='false' TopLevel='true' Visibility='public'>

Code Sample demonstrating the issue:

package sample;

public interface Example {
}
<rule ref="category/java/codestyle.xml/ClassNamingConventions">
    <properties>
        <property name="abstractClassPattern" value="Abstract[a-zA-Z0-9]+" />
    </properties>
</rule>

Steps to reproduce:

  1. Configure rules file as above with custom pattern for abstract classes.
  2. Have an interface that does not meet that pattern.
  3. Get violation like: PMD Failure: sample.Example:3 Rule:ClassNamingConventions Priority:1 The interface name 'Example' doesn't match 'Abstract[a-zA-Z0-9]+'.

Running PMD through: Maven

This is weird… the rule explicitly checks for interfaces beforehand…

} else if (node.isInterface()) {
checkMatches(node, interfaceRegex, data);
} else {
// at this point, node must be a class and cannot be an interface anymore
if (node.isAbstract()) {
checkMatches(node, abstractClassRegex, data);

Oh, apparently it is wrong in 7.0.0 release but was already fixed on master: fb496b9. Looks like this is a duplicate of #4881.