diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index f1c6ab85a9..40f41e4d2e 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -71,6 +71,8 @@ we have measured up to 10% improvements during Type Resolution, Symbol Table ana * java-codestyle * [#1003](https://github.com/pmd/pmd/issues/1003): \[java] UnnecessaryConstructor triggered on required empty constructor (Dagger @Inject) * [#1023](https://github.com/pmd/pmd/issues/1023): \[java] False positive for useless parenthesis +* java-errorprone + * [#816](https://github.com/pmd/pmd/issues/816): \[java] SingleMethodSingleton false positives with inner classes * java-performance * [#586](https://github.com/pmd/pmd/issues/586): \[java] AvoidUsingShortType erroneously triggered on overrides of 3rd party methods @@ -90,4 +92,5 @@ we have measured up to 10% improvements during Type Resolution, Symbol Table ana * [#1012](https://github.com/pmd/pmd/pull/1012): \[java] JUnitAssertionsShouldIncludeMessage - False positive with assertEquals and JUnit5 - [BBG](https://github.com/djydewang) * [#1024](https://github.com/pmd/pmd/pull/1024): \[java]Issue 558: Properlogger for enums - [Utku Cuhadaroglu](https://github.com/utkuc) * [#1041](https://github.com/pmd/pmd/pull/1041): \[java] Make BasicProjectMemoizer thread safe. - [bergander](https://github.com/bergander) +* [#1044](https://github.com/pmd/pmd/pull/1044): \[java] Fix for issue #816 - [Akshat Bahety](https://github.com/akshatbahety) diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/SingleMethodSingletonRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/SingleMethodSingletonRule.java index ead66d667a..8336ba8c0d 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/SingleMethodSingletonRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/SingleMethodSingletonRule.java @@ -4,35 +4,49 @@ package net.sourceforge.pmd.lang.java.rule.errorprone; -import java.util.HashSet; -import java.util.Set; -import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit; +import java.util.List; + + + +import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration; import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule; +/** + * Returns Checks if the singleton rule is used properly. + */ public class SingleMethodSingletonRule extends AbstractJavaRule { - private Set methodset = new HashSet(); + /** + * Checks for getInstance method usage in the same class. + * @param node of ASTCLass + * @param data of Object + * @return Object + * + */ - @Override - public Object visit(ASTCompilationUnit node, Object data) { - methodset.clear(); - return super.visit(node, data); - } - @Override - public Object visit(ASTMethodDeclaration node, Object data) { - if (node.getResultType().isVoid()) { - return super.visit(node, data); - } + public Object visit(ASTClassOrInterfaceDeclaration node, Object data) { - if ("getInstance".equals(node.getMethodName())) { - if (!methodset.add(node.getMethodName())) { - addViolation(data, node); + + List methods = node.findDescendantsOfType(ASTMethodDeclaration.class); // Find the name of methods in it + + int count = 0; + for (ASTMethodDeclaration method : methods) { + + if (method.getName().equals("getInstance")) { + count++; + if (count > 1) { + addViolation(data, node); + break; + } } + } + return super.visit(node, data); + } } diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/SingleMethodSingleton.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/SingleMethodSingleton.xml index a04d3e2800..ea2cc5b43d 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/SingleMethodSingleton.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/SingleMethodSingleton.xml @@ -48,4 +48,27 @@ private static Singleton instance = null; ]]> - \ No newline at end of file + + + + 0 + + + + + + + + +