[java] Fix #5253: Support Boolean wrapper class for BooleanGetMethodName rule (#5269)

Merge pull request #5269 from Aryant-Tripathi:5253/feature/support-boolean-wrapper-in-get-method-rule
This commit is contained in:
Andreas Dangel
2024-10-12 18:04:14 +02:00
5 changed files with 328 additions and 201 deletions

View File

@ -7828,6 +7828,24 @@
"contributions": [
"bug"
]
},
{
"login": "phansys",
"name": "Javier Spagnoletti",
"avatar_url": "https://avatars.githubusercontent.com/u/1231441?v=4",
"profile": "https://github.com/phansys",
"contributions": [
"bug"
]
},
{
"login": "Aryant-Tripathi",
"name": "Aryant Tripathi",
"avatar_url": "https://avatars.githubusercontent.com/u/60316716?v=4",
"profile": "https://github.com/Aryant-Tripathi",
"contributions": [
"code"
]
}
],
"contributorsPerLine": 7,

File diff suppressed because it is too large Load Diff

View File

@ -33,6 +33,8 @@ The old rule names still work but are deprecated.
* java
* [#4532](https://github.com/pmd/pmd/issues/4532): \[java] Rule misnomer for JUnit* rules
* [#5261](https://github.com/pmd/pmd/issues/5261): \[java] Record patterns with empty deconstructor lists lead to NPE
* java-codestyle
* [#5253](https://github.com/pmd/pmd/issues/5253): \[java] BooleanGetMethodName: False-negatives with `Boolean` wrapper
* java-errorprone
* [#5067](https://github.com/pmd/pmd/issues/5067): \[java] CloseResource: False positive for FileSystems.getDefault()
@ -51,6 +53,7 @@ The old rule names still work but are deprecated.
* [#5225](https://github.com/pmd/pmd/pull/5225): \[java] Fix #5067: CloseResource: False positive for FileSystems.getDefault() - [Lukas Gräf](https://github.com/lukasgraef) (@lukasgraef)
* [#5241](https://github.com/pmd/pmd/pull/5241): Ignore javacc code in coverage report - [Juan Martín Sotuyo Dodero](https://github.com/jsotuyod) (@jsotuyod)
* [#5264](https://github.com/pmd/pmd/pull/5264): \[java] Fix NPE with empty pattern list - [Clément Fournier](https://github.com/oowekyala) (@oowekyala)
* [#5269](https://github.com/pmd/pmd/pull/5269): \[java] Fix #5253: Support Boolean wrapper class for BooleanGetMethodName rule - [Aryant Tripathi](https://github.com/Aryant-Tripathi) (@Aryant-Tripathi)
{% endtocmaker %}

View File

@ -172,32 +172,33 @@ public class SomeJNIClass {
<rule name="BooleanGetMethodName"
language="java"
since="4.0"
message="A 'getX()' method which returns a boolean should be named 'isX()'"
message="A 'getX()' method which returns a boolean or Boolean should be named 'isX()'"
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_codestyle.html#booleangetmethodname">
<description>
Methods that return boolean results should be named as predicate statements to denote this.
I.e, 'isReady()', 'hasValues()', 'canCommit()', 'willFail()', etc. Avoid the use of the 'get'
prefix for these methods.
Methods that return boolean or Boolean results should be named as predicate statements to denote this.
I.e., 'isReady()', 'hasValues()', 'canCommit()', 'willFail()', etc. Avoid the use of the 'get' prefix for these methods.
</description>
<priority>4</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
<![CDATA[
//MethodDeclaration
[starts-with(@Name, 'get')]
[@Arity = 0 or $checkParameterizedMethods = true()]
[ PrimitiveType[@Kind = 'boolean'] and @Overridden = false() ]
[ (PrimitiveType[@Kind = 'boolean'] or ClassType[pmd-java:typeIs('java.lang.Boolean')]) and @Overridden = false() ]
]]>
</value>
</property>
<property name="checkParameterizedMethods" type="Boolean" description="Check parameterized methods" value="false"/>
</properties>
<example>
<![CDATA[
<![CDATA[
public boolean getFoo(); // bad
public Boolean getFoo(); // bad
public boolean isFoo(); // ok
public Boolean isFoo(); // ok
public boolean getFoo(boolean bar); // ok, unless checkParameterizedMethods=true
]]>
</example>

View File

@ -34,6 +34,18 @@ public class Foo {
]]></code>
</test-code>
<test-code>
<description>Should not match for boxed Boolean on multiple parameters by default (#5253)</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
public Boolean getEnabled(boolean thisIsNotABean) {
return false;
}
}
]]></code>
</test-code>
<test-code>
<description>Should not match on methods annotated with @Override</description>
<expected-problems>0</expected-problems>
@ -60,4 +72,93 @@ public class Foo {
}
]]></code>
</test-code>
<test-code>
<description>Bad name with boxed Boolean (#5253)</description>
<expected-problems>1</expected-problems>
<code><![CDATA[
public class Foo {
public Boolean getEnabled() {
return true;
}
}
]]></code>
</test-code>
<test-code>
<description>Good name with boxed Boolean (#5253)</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
public Boolean isEnabled() {
return false;
}
}
]]></code>
</test-code>
<test-code>
<description>Should not match for boxed Boolean on methods annotated with @Override (#5253)</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 for boxed Boolean on multiple parameters when checkParameterizedMethods = true (#5253)</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-code>
<description>Should match for boxed Boolean on multiple parameters when checkParameterizedMethods = true (#5253)</description>
<rule-property name="checkParameterizedMethods">true</rule-property>
<expected-problems>1</expected-problems>
<code><![CDATA[
public class Foo {
public Boolean getEnabled(boolean thisIsNotABean) {
return false;
}
}
]]></code>
</test-code>
<test-code>
<description>Custom Boolean type (#5253)</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
class Boolean { }
public class Foo {
public Boolean getEnabled() {
return null;
}
}
]]></code>
</test-code>
<test-code>
<description>Custom Boolean type with returning value (#5253)</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
class Boolean { }
public class Foo {
public Boolean getEnabled() { return false; }
}
]]></code>
</test-code>
</test-data>