Support wrapper class in BooleanGetMethodName rule (#5253)

- Updated XPath rule to include both  primitive and  wrapper class:
                  (PrimitiveType[@Kind = 'boolean'] or ClassType[pmd-java:typeIs('java.lang.Boolean')])
                - Added test cases to ensure that methods returning  are also flagged correctly.
                - Ensured the rule enforces consistent method naming for both primitive and wrapper types.
This commit is contained in:
Aryant Tripathi 2024-10-10 20:44:06 +05:30
parent 54dfabea9b
commit 8b2af2db8a
2 changed files with 61 additions and 1 deletions

View File

@ -187,7 +187,7 @@ public class SomeJNIClass {
//MethodDeclaration
[starts-with(@Name, 'get')]
[@Arity = 0 or $checkParameterizedMethods = true()]
[ (PrimitiveType[@Kind = 'boolean'] or ReferenceType[@Type = 'Boolean']) and @Overridden = false() ]
[ (PrimitiveType[@Kind = 'boolean'] or ClassType[pmd-java:typeIs('java.lang.Boolean')]) and @Overridden = false() ]
]]>
</value>
</property>

View File

@ -34,6 +34,16 @@ public class Foo {
]]></code>
</test-code>
<test-code>
<description>Should not match on multiple parameters by default</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
public Boolean getEnabled(boolean thisIsNotABean);
}
]]></code>
</test-code>
<test-code>
<description>Should not match on methods annotated with @Override</description>
<expected-problems>0</expected-problems>
@ -60,4 +70,54 @@ public class Foo {
}
]]></code>
</test-code>
<test-code>
<description>Bad name</description>
<expected-problems>1</expected-problems>
<code><![CDATA[
public class Foo {
public Boolean getEnabled();
}
]]></code>
</test-code>
<test-code>
<description>Good name</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
public Boolean isEnabled();
}
]]></code>
</test-code>
<test-code>
<description>Should not match on methods annotated with @Override</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo implements Toggleable {
@Override
public Boolean getEnabled() {
return true;
}
}
interface Toggleable {
Boolean getEnabled(); // NOPMD
}
]]></code>
</test-code>
<test-code>
<description>Should match on multiple parameters when checkParameterizedMethods = true</description>
<rule-property name="checkParameterizedMethods">true</rule-property>
<expected-problems>1</expected-problems>
<code><![CDATA[
public class Foo {
public Boolean getEnabled(boolean thisIsNotABean);
}
]]></code>
</test-code>
</test-data>