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] ClassWithOnlyPrivateConstructorsShouldBeFinal can't detect inner class #2536

Closed
wuchiuwong opened this issue May 26, 2020 · 1 comment · Fixed by #3404
Closed

[java] ClassWithOnlyPrivateConstructorsShouldBeFinal can't detect inner class #2536

wuchiuwong opened this issue May 26, 2020 · 1 comment · Fixed by #3404
Labels
a:false-negative PMD doesn't flag a problematic piece of code
Projects
Milestone

Comments

@wuchiuwong
Copy link

wuchiuwong commented May 26, 2020

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

@wuchiuwong wuchiuwong added the a:bug PMD crashes or fails to analyse a file. label May 26, 2020
@oowekyala oowekyala added a:false-negative PMD doesn't flag a problematic piece of code and removed a:bug PMD crashes or fails to analyse a file. labels Oct 26, 2020
@oowekyala oowekyala changed the title [java]ClassWithOnlyPrivateConstructorsShouldBeFinal can't detect inner class with only private constructor [java]ClassWithOnlyPrivateConstructorsShouldBeFinal can't detect inner class Jul 12, 2021
@oowekyala oowekyala changed the title [java]ClassWithOnlyPrivateConstructorsShouldBeFinal can't detect inner class [java] ClassWithOnlyPrivateConstructorsShouldBeFinal can't detect inner class Jul 12, 2021
@oowekyala oowekyala added this to Done in PMD 7 Jul 12, 2021
oowekyala added a commit to oowekyala/pmd that referenced this issue Jul 12, 2021
@adangel adangel linked a pull request Aug 19, 2021 that will close this issue
4 tasks
@adangel adangel added this to the 7.0.0 milestone Jan 7, 2023
@adangel adangel mentioned this issue Jan 23, 2023
55 tasks
@jsotuyod
Copy link
Member

This is fixed in PMD 7.0.0-rc1. It won't be backported to PMD 6.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a:false-negative PMD doesn't flag a problematic piece of code
Projects
No open projects
PMD 7
  
Done
Development

Successfully merging a pull request may close this issue.

4 participants