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 ae60c8b6a6..058378effd 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 @@ -3,79 +3,74 @@ */ package net.sourceforge.pmd.lang.java.rule.design; - -import java.util.HashSet; -import java.util.List; import java.util.HashMap; +import java.util.HashSet; import java.util.Map; import java.util.Set; -import net.sourceforge.pmd.lang.ast.Node; -import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBodyDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit; import net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTName; -import net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression; import net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix; -import net.sourceforge.pmd.lang.java.ast.ASTReturnStatement; import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId; import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule; public class SingleMethodSingletonRule extends AbstractJavaRule { - private static Map fieldDecls = new HashMap(); - private static Set returnset = new HashSet(); - private boolean violation = false; - private static Set methodset = new HashSet(); - @Override - public Object visit(ASTFieldDeclaration node, Object data) { - - if (node.isStatic() && node.isPrivate()) { - ASTVariableDeclaratorId varDeclaratorId=node.getFirstDescendantOfType(ASTVariableDeclaratorId.class); - if(varDeclaratorId!=null){ - String varName=varDeclaratorId.getImage(); - fieldDecls.put(varName, node); - } - } - - return super.visit(node, data); - } + private static Map fieldDecls = new HashMap(); + private static Set returnset = new HashSet(); + private boolean violation = false; + private static Set methodset = new HashSet(); - @Override - public Object visit(ASTCompilationUnit node, Object data){ - violation=false; - fieldDecls.clear(); - returnset.clear(); - methodset.clear(); - return super.visit(node,data); - } - @Override - public Object visit(ASTMethodDeclaration node, Object data) { + @Override + public Object visit(ASTFieldDeclaration node, Object data) { - violation=false; - if (node.getResultType().isVoid()) { - return super.visit(node, data); - } + if (node.isStatic() && node.isPrivate()) { + ASTVariableDeclaratorId varDeclaratorId = node.getFirstDescendantOfType(ASTVariableDeclaratorId.class); + if (varDeclaratorId != null) { + String varName = varDeclaratorId.getImage(); + fieldDecls.put(varName, node); + } + } - if ("getInstance".equals(node.getMethodName())) { + return super.visit(node, data); + } - if(!methodset.add(node.getMethodName())){ - violation=true; - } - } - - if(violation){ - addViolation(data, node); - } - return super.visit(node, data); - } + @Override + public Object visit(ASTCompilationUnit node, Object data) { + violation = false; + fieldDecls.clear(); + returnset.clear(); + methodset.clear(); + return super.visit(node, data); + } - private String getNameFromPrimaryPrefix(ASTPrimaryPrefix pp) { - if ((pp.jjtGetNumChildren() == 1) - && (pp.jjtGetChild(0) instanceof ASTName)) { - return ((ASTName) pp.jjtGetChild(0)).getImage(); - } - return null; - } + @Override + public Object visit(ASTMethodDeclaration node, Object data) { + + violation = false; + if (node.getResultType().isVoid()) { + return super.visit(node, data); + } + + if ("getInstance".equals(node.getMethodName())) { + + if (!methodset.add(node.getMethodName())) { + violation = true; + } + } + + if (violation) { + addViolation(data, node); + } + return super.visit(node, data); + } + + private String getNameFromPrimaryPrefix(ASTPrimaryPrefix pp) { + if ((pp.jjtGetNumChildren() == 1) && (pp.jjtGetChild(0) instanceof ASTName)) { + return ((ASTName) pp.jjtGetChild(0)).getImage(); + } + return null; + } } 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 fbde614876..72bca1bba2 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 @@ -17,112 +17,97 @@ import net.sourceforge.pmd.lang.java.ast.ASTReturnStatement; import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule; public class SingletonClassReturningNewInstanceRule extends AbstractJavaRule { - - @Override - public Object visit(ASTMethodDeclaration node, Object data) { - boolean violation = false; - String localVarName = null; - String returnVariableName = null; + @Override + public Object visit(ASTMethodDeclaration node, Object data) { - if (node.getResultType().isVoid()) { - return super.visit(node, data); - } + boolean violation = false; + String localVarName = null; + String returnVariableName = null; - if ("getInstance".equals(node.getMethodName())) { - List rsl = node - .findDescendantsOfType(ASTReturnStatement.class); - if (rsl.isEmpty()) { - return super.visit(node, data); - } else { - for(ASTReturnStatement rs : rsl){ - - List pel = rs - .findDescendantsOfType(ASTPrimaryExpression.class); - ASTPrimaryExpression ape = pel.get(0); - if (ape.getFirstDescendantOfType(ASTAllocationExpression.class) != null) { - violation = true; - break; - } - } - } + if (node.getResultType().isVoid()) { + return super.visit(node, data); + } - /* - * public class Singleton { - * - * private static Singleton m_instance=null; - * - * public static Singleton getInstance() { - * - * Singleton m_instance=null; - * - * if ( m_instance == null ) { - * synchronized(Singleton.class) { - * if(m_instance == null) { - * m_instance = new Singleton(); - * } - * } - * } - * return m_instance; - * } - * } - * - */ + if ("getInstance".equals(node.getMethodName())) { + List rsl = node.findDescendantsOfType(ASTReturnStatement.class); + if (rsl.isEmpty()) { + return super.visit(node, data); + } else { + for (ASTReturnStatement rs : rsl) { - List ASTBlockStatements = node - .findDescendantsOfType(ASTBlockStatement.class); - returnVariableName = getReturnVariableName(node); - if (ASTBlockStatements.size() != 0) { - for (ASTBlockStatement blockStatement : ASTBlockStatements) { - if (blockStatement - .hasDescendantOfType(ASTLocalVariableDeclaration.class)) { - List lVarList=blockStatement.findDescendantsOfType(ASTLocalVariableDeclaration.class); - if(!lVarList.isEmpty()){ - for(ASTLocalVariableDeclaration localVar : lVarList){ - localVarName = localVar.getVariableName(); - if (returnVariableName != null - && returnVariableName.equals(localVarName)) { - violation = true; - break; - } - } - } - } - } - } - } - if (violation) { - addViolation(data, node); - } - return super.visit(node, data); - } + List pel = rs.findDescendantsOfType(ASTPrimaryExpression.class); + ASTPrimaryExpression ape = pel.get(0); + if (ape.getFirstDescendantOfType(ASTAllocationExpression.class) != null) { + violation = true; + break; + } + } + } - private String getReturnVariableName(ASTMethodDeclaration node) { + /* + * public class Singleton { + * + * private static Singleton m_instance=null; + * + * public static Singleton getInstance() { + * + * Singleton m_instance=null; + * + * if ( m_instance == null ) { synchronized(Singleton.class) { + * if(m_instance == null) { m_instance = new Singleton(); } } } + * return m_instance; } } + */ - List rsl = node - .findDescendantsOfType(ASTReturnStatement.class); - ASTReturnStatement rs = rsl.get(0); - List pel = rs - .findDescendantsOfType(ASTPrimaryExpression.class); - ASTPrimaryExpression ape = pel.get(0); - Node lastChild = ape.jjtGetChild(0); - String returnVariableName = null; - if (lastChild instanceof ASTPrimaryPrefix) { - returnVariableName = getNameFromPrimaryPrefix((ASTPrimaryPrefix) lastChild); - } - /* - * if(lastChild instanceof ASTPrimarySuffix){ returnVariableName = - * getNameFromPrimarySuffix((ASTPrimarySuffix) lastChild); } - */ - return returnVariableName; + List ASTBlockStatements = node.findDescendantsOfType(ASTBlockStatement.class); + returnVariableName = getReturnVariableName(node); + if (ASTBlockStatements.size() != 0) { + for (ASTBlockStatement blockStatement : ASTBlockStatements) { + if (blockStatement.hasDescendantOfType(ASTLocalVariableDeclaration.class)) { + List lVarList = blockStatement + .findDescendantsOfType(ASTLocalVariableDeclaration.class); + if (!lVarList.isEmpty()) { + for (ASTLocalVariableDeclaration localVar : lVarList) { + localVarName = localVar.getVariableName(); + if (returnVariableName != null && returnVariableName.equals(localVarName)) { + violation = true; + break; + } + } + } + } + } + } + } + if (violation) { + addViolation(data, node); + } + return super.visit(node, data); + } - } + private String getReturnVariableName(ASTMethodDeclaration node) { - private String getNameFromPrimaryPrefix(ASTPrimaryPrefix pp) { - if ((pp.jjtGetNumChildren() == 1) - && (pp.jjtGetChild(0) instanceof ASTName)) { - return ((ASTName) pp.jjtGetChild(0)).getImage(); - } - return null; - } + List rsl = node.findDescendantsOfType(ASTReturnStatement.class); + ASTReturnStatement rs = rsl.get(0); + List pel = rs.findDescendantsOfType(ASTPrimaryExpression.class); + ASTPrimaryExpression ape = pel.get(0); + Node lastChild = ape.jjtGetChild(0); + String returnVariableName = null; + if (lastChild instanceof ASTPrimaryPrefix) { + returnVariableName = getNameFromPrimaryPrefix((ASTPrimaryPrefix) lastChild); + } + /* + * if(lastChild instanceof ASTPrimarySuffix){ returnVariableName = + * getNameFromPrimarySuffix((ASTPrimarySuffix) lastChild); } + */ + return returnVariableName; + + } + + private String getNameFromPrimaryPrefix(ASTPrimaryPrefix pp) { + if ((pp.jjtGetNumChildren() == 1) && (pp.jjtGetChild(0) instanceof ASTName)) { + return ((ASTName) pp.jjtGetChild(0)).getImage(); + } + return null; + } } \ No newline at end of file