From d885267e6c0fdb4c6d98e20caa0ca2b24613b7b9 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Thu, 16 Apr 2015 20:12:22 +0200 Subject: [PATCH] Update changelog, add license headers, add externalInfoUrl for new rules --- .../src/main/resources/rulesets/releases/540.xml | 15 +++++++++++++++ .../rule/design/SingleMethodSingletonRule.java | 5 ++++- .../SingletonClassReturningNewInstanceRule.java | 3 +++ .../src/main/resources/rulesets/java/design.xml | 4 ++-- .../lang/java/rule/design/DesignRulesTest.java | 4 ++-- src/site/markdown/overview/changelog.md | 7 +++++++ 6 files changed, 33 insertions(+), 5 deletions(-) create mode 100644 pmd-core/src/main/resources/rulesets/releases/540.xml diff --git a/pmd-core/src/main/resources/rulesets/releases/540.xml b/pmd-core/src/main/resources/rulesets/releases/540.xml new file mode 100644 index 0000000000..689785f0f8 --- /dev/null +++ b/pmd-core/src/main/resources/rulesets/releases/540.xml @@ -0,0 +1,15 @@ + + + + +This ruleset contains links to rules that are new in PMD v5.4.0 + + + + + + + diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/SingleMethodSingletonRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/SingleMethodSingletonRule.java index 0ab91a1d8c..ae60c8b6a6 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/SingleMethodSingletonRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/SingleMethodSingletonRule.java @@ -1,3 +1,6 @@ +/** + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ package net.sourceforge.pmd.lang.java.rule.design; @@ -23,7 +26,7 @@ public class SingleMethodSingletonRule extends AbstractJavaRule { private static Map fieldDecls = new HashMap(); private static Set returnset = new HashSet(); - boolean violation=false; + private boolean violation = false; private static Set methodset = new HashSet(); @Override public Object visit(ASTFieldDeclaration node, Object data) { diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/SingletonClassReturningNewInstanceRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/SingletonClassReturningNewInstanceRule.java index 3097ab5c60..fbde614876 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/SingletonClassReturningNewInstanceRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/SingletonClassReturningNewInstanceRule.java @@ -1,3 +1,6 @@ +/** + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ package net.sourceforge.pmd.lang.java.rule.design; import java.util.List; diff --git a/pmd-java/src/main/resources/rulesets/java/design.xml b/pmd-java/src/main/resources/rulesets/java/design.xml index d8b16e9b71..59a9aec14c 100644 --- a/pmd-java/src/main/resources/rulesets/java/design.xml +++ b/pmd-java/src/main/resources/rulesets/java/design.xml @@ -1299,7 +1299,7 @@ public static Foo getFoo() { @@ -1329,7 +1329,7 @@ public static Singleton getInstance(Object obj){ diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/design/DesignRulesTest.java b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/design/DesignRulesTest.java index 743dddae0c..119f833a73 100644 --- a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/design/DesignRulesTest.java +++ b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/design/DesignRulesTest.java @@ -46,8 +46,6 @@ public class DesignRulesTest extends SimpleAggregatorTst { addRule(RULESET, "NonCaseLabelInSwitchStatement"); addRule(RULESET, "NonStaticInitializer"); addRule(RULESET, "NonThreadSafeSingleton"); - addRule(RULESET,"SingleMethodSingleton"); - addRule(RULESET, "SingletonClassReturningNewInstance"); addRule(RULESET, "OptimizableToArrayCall"); // addRule(RULESET, "PositionalIteratorRule"); This rule does not yet // exist @@ -59,6 +57,8 @@ public class DesignRulesTest extends SimpleAggregatorTst { addRule(RULESET, "SimplifyBooleanExpressions"); addRule(RULESET, "SimplifyBooleanReturns"); addRule(RULESET, "SimplifyConditional"); + addRule(RULESET, "SingleMethodSingleton"); + addRule(RULESET, "SingletonClassReturningNewInstance"); addRule(RULESET, "SingularField"); addRule(RULESET, "SwitchDensity"); addRule(RULESET, "SwitchStmtsShouldHaveDefault"); diff --git a/src/site/markdown/overview/changelog.md b/src/site/markdown/overview/changelog.md index 27c3467e78..451fc906f1 100644 --- a/src/site/markdown/overview/changelog.md +++ b/src/site/markdown/overview/changelog.md @@ -8,8 +8,15 @@ **New/Modified/Deprecated Rules:** +* New Rule: rulesets/java/design.xml/SingleMethodSingletonRule: Verifies that there is only one method called + "getInstance". If there are more methods that return the singleton, than it can easily happen, that these + are not the same instances - and thus no singleton. +* New Rule: rulesets/java/design.xml/SingletonClassReturningNewInstance: Verifies that the method called + "getInstance" returns a cached instance not always a fresh, new instance. + **Pull Requests:** +* [#21](https://github.com/adangel/pmd/pull/21): Added PMD Rules for Singleton pattern violations. * [#53](https://github.com/pmd/pmd/pull/53): Fix some NullPointerExceptions **Bugfixes:**