resources.report.rules.pmd.AvoidProtectedMethodInFinalClassNotExtending.html Maven / Gradle / Ivy
AvoidProtectedMethodInFinalClassNotExtending
AvoidProtectedMethodInFinalClassNotExtending
Do not use protected methods in most final classes since they cannot be subclassed. This should only be allowed in final classes that extend other classes with protected methods (whose visibility cannot be reduced). Clarify your intent by using private or package access modifiers instead.
//ClassOrInterfaceDeclaration[@Final='true' and not(ExtendsList)]
/ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration
/MethodDeclaration[@Protected='true'][MethodDeclarator/@Image != 'finalize']
Example(s):
public final class Foo {
private int bar() {}
protected int baz() {} // Foo cannot be subclassed, and doesn't extend anything, so is baz() really private or package visible?
}