Affects PMD Version:
6.22.0
Rule:
ClassWithOnlyPrivateConstructorsShouldBeFinal
Description:
ClassWithOnlyPrivateConstructorsShouldBeFinal can't detect inner class with only private constructor
This rule is implemented through xpath search:
TypeDeclaration[count(../TypeDeclaration) = 1]/ClassOrInterfaceDeclaration
[@Final = 'false']
[count(./ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration/ConstructorDeclaration[@Private = 'true']) >= 1 ]
[count(./ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration/ConstructorDeclaration[(@Public = 'true') or (@Protected = 'true') or (@PackagePrivate = 'true')]) = 0 ]
[not(.//ClassOrInterfaceDeclaration)]
while the AST analyse result of inner class in code sample is:
<TypeDeclaration FindBoundary='false' Image='' SingleLine='false'>
<ClassOrInterfaceDeclaration Abstract='false' BinaryName='' Default='false' Final='false' FindBoundary='false' Image='Main209' Interface='false' Local='false' Modifiers='1' Native='false' Nested='false' PackagePrivate='false' Private='false' Protected='false' Public='true' SimpleName='Main209' SingleLine='false' Static='false' Strictfp='false' Synchronized='false' Transient='false' TypeKind='CLASS' Volatile='false'>
<ClassOrInterfaceBody AnonymousInnerClass='false' EnumChild='false' FindBoundary='false' Image='' SingleLine='false'>
<ClassOrInterfaceBodyDeclaration AnonymousInnerClass='false' EnumChild='false' FindBoundary='false' Image='' Kind='CLASS' SingleLine='false'>
<ClassOrInterfaceDeclaration Abstract='false' BinaryName='' Default='false' Final='false' FindBoundary='true' Image='InputBits' Interface='false' Local='false' Modifiers='20' Native='false' Nested='true' PackagePrivate='false' Private='true' Protected='false' Public='false' SimpleName='InputBits' SingleLine='false' Static='true' Strictfp='false' Synchronized='false' Transient='false' TypeKind='CLASS' Volatile='false'>
AST chain to class InputBits
is TypeDeclaration->ClassOrInterfaceDeclaration->ClassOrInterfaceBody->ClassOrInterfaceBodyDeclaration->ClassOrInterfaceDeclaration
Code Sample demonstrating the issue:
public class Main209 {
......
private static class InputBits {
private InputBits(int number) {
}
......
Expected outcome:
false-negative
Running PMD through:
CLI
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