1
0
forked from phoedos/pmd

Merge branch 'pr-1044'

This commit is contained in:
Juan Martín Sotuyo Dodero
2018-04-23 02:16:18 -03:00
3 changed files with 58 additions and 18 deletions
docs/pages
pmd-java/src
main/java/net/sourceforge/pmd/lang/java/rule/errorprone
test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml

@ -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)

@ -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<String> methodset = new HashSet<String>();
/**
* 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<ASTMethodDeclaration> 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);
}
}

@ -48,4 +48,27 @@ private static Singleton instance = null;
]]>
</code>
</test-code>
</test-data>
<test-code>
<description><![CDATA[
OK! Has two different getInstance() in different classes
]]></description>
<expected-problems>0</expected-problems>
<code>
<![CDATA[
final class Siblings {
static class Inner1 {
static Inner1 getInstance() { return null; }
}
static class Inner2 {
static Inner2 getInstance() { return null; }
}
}
]]>
</code>
</test-code>
</test-data>