diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index 0ea1034c15..10f9ada18c 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -26,6 +26,15 @@ for parsing JavaScript code, requires at least Java 8. Therefore we decided to u module to Java 8 as well. This means that from now on, a Java 8 or later runtime is required in order to analyze JavaScript code. Note that PMD core still only requires Java 7. +#### New rules + +* The new Java rule {% rule "java/bestpractices/JUnit5TestShouldBePackagePrivate" %} + enforces the convention that JUnit 5 tests should have minimal visibility. + You can try out this rule like so: +```xml + +``` + #### Modified rules * The Java rule {% rule "java/errorprone/CompareObjectsWithEquals" %} has now a new property @@ -35,6 +44,31 @@ to analyze JavaScript code. Note that PMD core still only requires Java 7. Additionally comparisons against constants are allowed now. This makes the rule less noisy when two constants are compared. Constants are identified by looking for an all-caps identifier. +#### Deprecated rules + +* The java rule {% rule "java/codestyle/DefaultPackage" %} has been deprecated in favor of + {% rule "java/codestyle/CommentDefaultAccessModifier" %}. + + The rule "DefaultPackage" assumes that any usage of package-access is accidental, + and by doing so, prohibits using a really fundamental and useful feature of the language. + + To satisfy the rule, you have to make the member public even if it doesn't need to, or make it protected, + which muddies your intent even more if you don't intend the class to be extended, and may be at odds with + other rules like {% rule "java/codestyle/AvoidProtectedFieldInFinalClass" %}. + + The rule {% rule "java/codestyle/CommentDefaultAccessModifier" %} should be used instead. + It flags the same thing, but has an escape hatch. + +* The Java rule {% rule "java/errorprone/CloneThrowsCloneNotSupportedException" %} has been deprecated without + replacement. + + The rule has no real value as `CloneNotSupportedException` is a + checked exception and therefore you need to deal with it while implementing the `clone()` method. You either + need to declare the exception or catch it. If you catch it, then subclasses can't throw it themselves explicitly. + However, `Object.clone()` will still throw this exception if the `Cloneable` interface is not implemented. + + Note, this rule has also been removed from the Quickstart Ruleset (`rulesets/java/quickstart.xml`). + ### Fixed Issues * apex @@ -56,6 +90,7 @@ to analyze JavaScript code. Note that PMD core still only requires Java 7. * [#3254](https://github.com/pmd/pmd/issues/3254): \[java] AvoidReassigningParameters reports violations on wrong line numbers * java-codestyle * [#2655](https://github.com/pmd/pmd/issues/2655): \[java] UnnecessaryImport false positive for on-demand imports + * [#3206](https://github.com/pmd/pmd/issues/3206): \[java] Deprecate rule DefaultPackage * [#3262](https://github.com/pmd/pmd/pull/3262): \[java] FieldDeclarationsShouldBeAtStartOfClass: false negative with anon classes * [#3265](https://github.com/pmd/pmd/pull/3265): \[java] MethodArgumentCouldBeFinal: false negatives with interfaces and inner classes * [#3266](https://github.com/pmd/pmd/pull/3266): \[java] LocalVariableCouldBeFinal: false negatives with interfaces, anon classes @@ -65,6 +100,7 @@ to analyze JavaScript code. Note that PMD core still only requires Java 7. * [#2780](https://github.com/pmd/pmd/issues/2780): \[java] DataClass example from documentation results in false-negative * 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 * [#3205](https://github.com/pmd/pmd/issues/3205): \[java] Make CompareObjectWithEquals allow comparing against constants * [#3248](https://github.com/pmd/pmd/issues/3248): \[java] Documentation is wrong for SingletonClassReturningNewInstance rule * [#3249](https://github.com/pmd/pmd/pull/3249): \[java] AvoidFieldNameMatchingTypeName: False negative with interfaces diff --git a/pmd-java/src/main/resources/category/java/bestpractices.xml b/pmd-java/src/main/resources/category/java/bestpractices.xml index 0d98e13d88..e99122c522 100644 --- a/pmd-java/src/main/resources/category/java/bestpractices.xml +++ b/pmd-java/src/main/resources/category/java/bestpractices.xml @@ -797,6 +797,71 @@ public class MyTest { + + + 3 + + + + + + + + + + + + + Use explicit scoping instead of accidental usage of default package private level. The rule allows methods and fields annotated with Guava's @VisibleForTesting and JUnit 5's annotations. + +This rule is deprecated since PMD 6.35.0. It assumes that any usage of package-access is accidental, +and by doing so, prohibits using a really fundamental and useful feature of the language. + +To satisfy the rule, you have to make the member public even if it doesn't need to, or make it protected, +which muddies your intent even more if you don't intend the class to be extended, and may be at odds with +other rules like {% rule "java/codestyle/AvoidProtectedFieldInFinalClass" %}. + +The rule {% rule "java/codestyle/CommentDefaultAccessModifier" %} should be used instead. This rule flags +the same thing, but has an escape hatch. 3 diff --git a/pmd-java/src/main/resources/category/java/errorprone.xml b/pmd-java/src/main/resources/category/java/errorprone.xml index 36f4ce0941..e32f56e8ed 100644 --- a/pmd-java/src/main/resources/category/java/errorprone.xml +++ b/pmd-java/src/main/resources/category/java/errorprone.xml @@ -998,6 +998,7 @@ public class Foo implements Cloneable { The method clone() should throw a CloneNotSupportedException. + +This rule is deprecated since PMD 6.35.0 without replacement. The rule has no real value as +`CloneNotSupportedException` is a checked exception and therefore you need to deal with it while +implementing the `clone()` method. You either need to declare the exception or catch it. If you catch it, +then subclasses can't throw it themselves explicitly. However, `Object.clone()` will still throw this +exception if the `Cloneable` interface is not implemented. 3 diff --git a/pmd-java/src/main/resources/rulesets/java/quickstart.xml b/pmd-java/src/main/resources/rulesets/java/quickstart.xml index 359f78215e..f2e03f6183 100644 --- a/pmd-java/src/main/resources/rulesets/java/quickstart.xml +++ b/pmd-java/src/main/resources/rulesets/java/quickstart.xml @@ -27,6 +27,7 @@ + @@ -89,7 +90,7 @@ - + @@ -203,7 +204,7 @@ - + diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/bestpractices/JUnit5TestShouldBePackagePrivateTest.java b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/bestpractices/JUnit5TestShouldBePackagePrivateTest.java new file mode 100644 index 0000000000..5d56479c58 --- /dev/null +++ b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/bestpractices/JUnit5TestShouldBePackagePrivateTest.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.bestpractices; + +import net.sourceforge.pmd.testframework.PmdRuleTst; + +public class JUnit5TestShouldBePackagePrivateTest extends PmdRuleTst { + // no additional unit tests +} diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/bestpractices/xml/JUnit5TestShouldBePackagePrivate.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/bestpractices/xml/JUnit5TestShouldBePackagePrivate.xml new file mode 100644 index 0000000000..9fdea7c27d --- /dev/null +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/bestpractices/xml/JUnit5TestShouldBePackagePrivate.xml @@ -0,0 +1,132 @@ + + + + + Public modifier is not necessary on a test method nor on the class + 2 + 3,5 + + + + + Package private modifiers are what is required + 0 + + + + + Private method modifiers should be reported by rule 'JUnit5TestNoPrivateModifier' and ignored by rule 'JUnit5TestShouldBePackagePrivate' + 0 + + + + + Private modifiers for inner classes should be reported by rule 'JUnit5TestNoPrivateModifier' and ignored by rule 'JUnit5TestShouldBePackagePrivate' + 0 + + + + + Public modifier is allowed on an abstract test classes + 0 + + + + + Public modifier is allowed on test interfaces + 0 + + + + + Public and protected modifiers on all JUnit5 test types should be rejected + 5 + 8,10,13,19,23 + + + + + Public JUnit4 tests are not flagged + 0 + + +