Merge branch 'pr-2409'

[java] ClassNamingConventions suggests to add Util for class containing only static constants, fixes #1164 #2409
This commit is contained in:
Andreas Dangel
2020-04-13 12:06:01 +02:00
3 changed files with 19 additions and 3 deletions

View File

@ -51,6 +51,7 @@ Note that XPath 1.0 support, the default XPath version, is deprecated since PMD
* java
* [#2378](https://github.com/pmd/pmd/issues/2378): \[java] AbstractJUnitRule has bad performance on large code bases
* java-codestyle
* [#1164](https://github.com/pmd/pmd/issues/1164): \[java] ClassNamingConventions suggests to add Util for class containing only static constants
* [#1723](https://github.com/pmd/pmd/issues/1723): \[java] UseDiamondOperator false-positive inside lambda
* java-design
* [#2390](https://github.com/pmd/pmd/issues/2390): \[java] AbstractClassWithoutAnyMethod: missing violation for nested classes
@ -136,6 +137,7 @@ implementations, and their corresponding Parser if it exists (in the same packag
* [#2397](https://github.com/pmd/pmd/pull/2397): \[apex] fixed WITH SECURITY_ENFORCED regex to recognise line break characters - [Kieran Black](https://github.com/kieranlblack)
* [#2401](https://github.com/pmd/pmd/pull/2401): \[doc] Update DoNotUseThreads rule documentation - [Saikat Sengupta](https://github.com/s4ik4t)
* [#2403](https://github.com/pmd/pmd/pull/2403): \[java] #2402 fix false-positives on Primitive Streams - [Bernd Farka](https://github.com/BerndFarkaDyna)
* [#2409](https://github.com/pmd/pmd/pull/2409): \[java] ClassNamingConventions suggests to add Util for class containing only static constants, fixes #1164 - [Binu R J](https://github.com/binu-r)
* [#2411](https://github.com/pmd/pmd/pull/2411): \[java] Fix UseAssertEqualsInsteadOfAssertTrue Example - [Moritz Scheve](https://github.com/Blightbuster)
{% endtocmaker %}

View File

@ -30,7 +30,7 @@ public class ClassNamingConventionsRule extends AbstractNamingConventionRule<AST
private final PropertyDescriptor<Pattern> interfaceRegex = defaultProp("interface").build();
private final PropertyDescriptor<Pattern> enumerationRegex = defaultProp("enum").build();
private final PropertyDescriptor<Pattern> annotationRegex = defaultProp("annotation").build();
private final PropertyDescriptor<Pattern> utilityClassRegex = defaultProp("utility class").defaultValue("[A-Z][a-zA-Z0-9]+(Utils?|Helper)").build();
private final PropertyDescriptor<Pattern> utilityClassRegex = defaultProp("utility class").defaultValue("[A-Z][a-zA-Z0-9]+(Utils?|Helper|Constants)").build();
public ClassNamingConventionsRule() {

View File

@ -53,7 +53,7 @@
<description>Utility class convention</description>
<expected-problems>1</expected-problems>
<expected-messages>
<message>The utility class name 'Foo' doesn't match '[A-Z][a-zA-Z0-9]+(Utils?|Helper)'</message>
<message>The utility class name 'Foo' doesn't match '[A-Z][a-zA-Z0-9]+(Utils?|Helper|Constants)'</message>
</expected-messages>
<code><![CDATA[
public class Foo {
@ -144,7 +144,7 @@
<description>Class with only static members except constructors should be a utility class</description>
<expected-problems>1</expected-problems>
<expected-messages>
<message>The utility class name 'Foo' doesn't match '[A-Z][a-zA-Z0-9]+(Utils?|Helper)'</message>
<message>The utility class name 'Foo' doesn't match '[A-Z][a-zA-Z0-9]+(Utils?|Helper|Constants)'</message>
</expected-messages>
<code><![CDATA[
public class Foo {
@ -255,4 +255,18 @@
]]></code>
</test-code>
<test-code>
<description>Utility class can have name constants</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class MyConstants {
public static final String FOOO = "FOOO";
private MyConstants() {
// whitelisted
}
}
]]></code>
</test-code>
</test-data>