[java] Improve TooManyMethods (#3994)

This commit is contained in:
Andreas Dangel committed 2022-07-07 10:07:35 +02:00
1 parent 5fa2a6b976
commit 8f3c7ee71d
2 files changed
+26 -36

No files matched your search

@@ -1678,39 +1678,14 @@ complexity and find a way to have more fine grained objects.
<property name="version" value="2.0"/>
<property name="xpath">
<value>
<!-- FIXME: Refine XPath to discard 'get' and 'set' methods with Block no more than 3 lines,
something like this:
not (
(
starts-with(@Name,'get')
or
starts-with(@Name,'set')
or
starts-with(@Name,'is')
)
and (
(
(../Block/attribute::endLine)
-
(../Block/attribute::beginLine)
) <= 3
)
)
This will avoid discarding 'real' methods...
-->
<![CDATA[
//ClassOrInterfaceDeclaration/ClassOrInterfaceBody
[
count(./ClassOrInterfaceBodyDeclaration/MethodDeclaration[
not (
starts-with(@Name,'get')
or
starts-with(@Name,'set')
or
starts-with(@Name,'is')
and (
count(../Block/BlockStatement)<=1)
(starts-with(@Name,'get') or starts-with(@Name,'set') or starts-with(@Name,'is'))
and
count(Block/BlockStatement) <= 1
)
]) > $maxmethods
]
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<test-data
xmlns="http://pmd.sourceforge.net/rule-tests"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/rule-tests http://pmd.sourceforge.net/rule-tests_1_0_0.xsd">
xmlns="http://pmd.sourceforge.net/rule-tests"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/rule-tests http://pmd.sourceforge.net/rule-tests_1_0_0.xsd">
<test-code>
<description>Less than 10 methods.</description>
@@ -114,10 +114,7 @@ public class OuterClass {
]]></code>
</test-code>
<test-code>
<description>#3729 TooManyMethods ignores "real" methods which are named like getters or setters</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
<code-fragment id="code_22_real_methods"><![CDATA[
public class Foo {
public void setMethod1(){int a = 1;a++;}
public void setMethod2(){int a = 1;a++;}
@@ -144,7 +141,25 @@ public class Foo {
public void method20(){}
public void method21(){}
public void method22(){}
private String field;
public String getField() { return field; } // real getter
public void setField(String field) { this.field = field; } // real setter
}
]]></code>
]]>
</code-fragment>
<test-code>
<description>#3729 TooManyMethods ignores "real" methods which are named like getters or setters - default</description>
<expected-problems>1</expected-problems>
<expected-linenumbers>1</expected-linenumbers>
<code-ref id="code_22_real_methods"/>
</test-code>
<test-code>
<description>#3729 TooManyMethods ignores "real" methods which are named like getters or setters - max 22 methods</description>
<rule-property name="maxmethods">22</rule-property>
<expected-problems>0</expected-problems>
<code-ref id="code_22_real_methods"/>
</test-code>
</test-data>