diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index 7e967c8806..1e4e3039c3 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -30,6 +30,13 @@ to analyze JavaScript code. Note that PMD core still only requires Java 7. ``` +* The new Java rule {% rule "java/design/MutableStaticState" %} finds non-private static fields + that are not final. These fields break encapsulation since these fields can be modified from anywhere + within the program. You can try out this rule like so: +```xml + +``` + #### Modified rules * The Java rule {% rule "java/errorprone/CompareObjectsWithEquals" %} has now a new property @@ -93,6 +100,7 @@ to analyze JavaScript code. Note that PMD core still only requires Java 7. * [#3275](https://github.com/pmd/pmd/pull/3275): \[java] UnnecessaryLocalBeforeReturn: false negatives with lambda and anon class * java-design * [#2780](https://github.com/pmd/pmd/issues/2780): \[java] DataClass example from documentation results in false-negative + * [#2987](https://github.com/pmd/pmd/issues/2987): \[java] New Rule: Public and protected static fields must be final * java-errorprone * [#3110](https://github.com/pmd/pmd/issues/3110): \[java] Enhance CompareObjectsWithEquals with list of exceptions * [#3112](https://github.com/pmd/pmd/issues/3112): \[java] Deprecate rule CloneThrowsCloneNotSupportedException @@ -108,5 +116,6 @@ to analyze JavaScript code. Note that PMD core still only requires Java 7. ### External Contributions * [#3272](https://github.com/pmd/pmd/pull/3272): \[apex] correction for ApexUnitTestMethodShouldHaveIsTestAnnotation false positives - [William Brockhus](https://github.com/YodaDaCoda) +* [#3246](https://github.com/pmd/pmd/pull/3246): \[java] New Rule: MutableStaticState - [Vsevolod Zholobov](https://github.com/vszholobov) {% endtocmaker %} diff --git a/pmd-core/src/main/resources/rulesets/releases/6350.xml b/pmd-core/src/main/resources/rulesets/releases/6350.xml new file mode 100644 index 0000000000..269ee93117 --- /dev/null +++ b/pmd-core/src/main/resources/rulesets/releases/6350.xml @@ -0,0 +1,13 @@ + + + + +This ruleset contains links to rules that are new in PMD v6.35.0 + + + + + diff --git a/pmd-java/src/main/resources/category/java/design.xml b/pmd-java/src/main/resources/category/java/design.xml index 9637117720..a4dfdc31a8 100644 --- a/pmd-java/src/main/resources/category/java/design.xml +++ b/pmd-java/src/main/resources/category/java/design.xml @@ -1731,6 +1731,44 @@ public class MaybeAUtility { public static void foo() {} public static void bar() {} } +]]> + + + + + +Non-private static fields should be made constants (or immutable references) by +declaring them final. + +Non-private non-final static fields break encapsulation and can lead to hard to find +bugs, since these fields can be modified from anywhere within the program. +Callers can trivially access and modify non-private non-final static fields. Neither +accesses nor modifications can be guarded against, and newly set values cannot +be validated. + +If you are using this rule, then you don't need this +rule {% rule java/errorprone/AssignmentToNonFinalStatic %}. + + 3 + + + + + + + + + + diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/design/MutableStaticStateTest.java b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/design/MutableStaticStateTest.java new file mode 100644 index 0000000000..27ba41ea2f --- /dev/null +++ b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/design/MutableStaticStateTest.java @@ -0,0 +1,11 @@ +/** + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ + +package net.sourceforge.pmd.lang.java.rule.design; + +import net.sourceforge.pmd.testframework.PmdRuleTst; + +public class MutableStaticStateTest extends PmdRuleTst { + // no additional unit tests +} diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/design/xml/MutableStaticState.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/design/xml/MutableStaticState.xml new file mode 100644 index 0000000000..7c2a9c6ec9 --- /dev/null +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/design/xml/MutableStaticState.xml @@ -0,0 +1,53 @@ + + + + Public fields + 1 + 2 + + java 11 + + + + Default fields + 1 + 2 + + + + + Protected fields + 1 + 2 + + + + + Private fields + 0 + + + + \ No newline at end of file