whitespaces

This commit is contained in:
Andreas Dangel
2015-04-16 20:13:53 +02:00
parent d885267e6c
commit a9cf5d0b73
2 changed files with 135 additions and 155 deletions

View File

@ -3,22 +3,16 @@
*/ */
package net.sourceforge.pmd.lang.java.rule.design; package net.sourceforge.pmd.lang.java.rule.design;
import java.util.HashSet;
import java.util.List;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Set; 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.ASTCompilationUnit;
import net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTName; 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.ASTPrimaryPrefix;
import net.sourceforge.pmd.lang.java.ast.ASTReturnStatement;
import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId; import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule; import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
@ -28,13 +22,14 @@ public class SingleMethodSingletonRule extends AbstractJavaRule {
private static Set<ASTFieldDeclaration> returnset = new HashSet<ASTFieldDeclaration>(); private static Set<ASTFieldDeclaration> returnset = new HashSet<ASTFieldDeclaration>();
private boolean violation = false; private boolean violation = false;
private static Set<String> methodset = new HashSet<String>(); private static Set<String> methodset = new HashSet<String>();
@Override @Override
public Object visit(ASTFieldDeclaration node, Object data) { public Object visit(ASTFieldDeclaration node, Object data) {
if (node.isStatic() && node.isPrivate()) { if (node.isStatic() && node.isPrivate()) {
ASTVariableDeclaratorId varDeclaratorId=node.getFirstDescendantOfType(ASTVariableDeclaratorId.class); ASTVariableDeclaratorId varDeclaratorId = node.getFirstDescendantOfType(ASTVariableDeclaratorId.class);
if(varDeclaratorId!=null){ if (varDeclaratorId != null) {
String varName=varDeclaratorId.getImage(); String varName = varDeclaratorId.getImage();
fieldDecls.put(varName, node); fieldDecls.put(varName, node);
} }
} }
@ -43,37 +38,37 @@ public class SingleMethodSingletonRule extends AbstractJavaRule {
} }
@Override @Override
public Object visit(ASTCompilationUnit node, Object data){ public Object visit(ASTCompilationUnit node, Object data) {
violation=false; violation = false;
fieldDecls.clear(); fieldDecls.clear();
returnset.clear(); returnset.clear();
methodset.clear(); methodset.clear();
return super.visit(node,data); return super.visit(node, data);
} }
@Override @Override
public Object visit(ASTMethodDeclaration node, Object data) { public Object visit(ASTMethodDeclaration node, Object data) {
violation=false; violation = false;
if (node.getResultType().isVoid()) { if (node.getResultType().isVoid()) {
return super.visit(node, data); return super.visit(node, data);
} }
if ("getInstance".equals(node.getMethodName())) { if ("getInstance".equals(node.getMethodName())) {
if(!methodset.add(node.getMethodName())){ if (!methodset.add(node.getMethodName())) {
violation=true; violation = true;
} }
} }
if(violation){ if (violation) {
addViolation(data, node); addViolation(data, node);
} }
return super.visit(node, data); return super.visit(node, data);
} }
private String getNameFromPrimaryPrefix(ASTPrimaryPrefix pp) { private String getNameFromPrimaryPrefix(ASTPrimaryPrefix pp) {
if ((pp.jjtGetNumChildren() == 1) if ((pp.jjtGetNumChildren() == 1) && (pp.jjtGetChild(0) instanceof ASTName)) {
&& (pp.jjtGetChild(0) instanceof ASTName)) {
return ((ASTName) pp.jjtGetChild(0)).getImage(); return ((ASTName) pp.jjtGetChild(0)).getImage();
} }
return null; return null;

View File

@ -30,15 +30,13 @@ public class SingletonClassReturningNewInstanceRule extends AbstractJavaRule {
} }
if ("getInstance".equals(node.getMethodName())) { if ("getInstance".equals(node.getMethodName())) {
List<ASTReturnStatement> rsl = node List<ASTReturnStatement> rsl = node.findDescendantsOfType(ASTReturnStatement.class);
.findDescendantsOfType(ASTReturnStatement.class);
if (rsl.isEmpty()) { if (rsl.isEmpty()) {
return super.visit(node, data); return super.visit(node, data);
} else { } else {
for(ASTReturnStatement rs : rsl){ for (ASTReturnStatement rs : rsl) {
List<ASTPrimaryExpression> pel = rs List<ASTPrimaryExpression> pel = rs.findDescendantsOfType(ASTPrimaryExpression.class);
.findDescendantsOfType(ASTPrimaryExpression.class);
ASTPrimaryExpression ape = pel.get(0); ASTPrimaryExpression ape = pel.get(0);
if (ape.getFirstDescendantOfType(ASTAllocationExpression.class) != null) { if (ape.getFirstDescendantOfType(ASTAllocationExpression.class) != null) {
violation = true; violation = true;
@ -56,32 +54,22 @@ public class SingletonClassReturningNewInstanceRule extends AbstractJavaRule {
* *
* Singleton m_instance=null; * Singleton m_instance=null;
* *
* if ( m_instance == null ) { * if ( m_instance == null ) { synchronized(Singleton.class) {
* synchronized(Singleton.class) { * if(m_instance == null) { m_instance = new Singleton(); } } }
* if(m_instance == null) { * return m_instance; } }
* m_instance = new Singleton();
* }
* }
* }
* return m_instance;
* }
* }
*
*/ */
List<ASTBlockStatement> ASTBlockStatements = node List<ASTBlockStatement> ASTBlockStatements = node.findDescendantsOfType(ASTBlockStatement.class);
.findDescendantsOfType(ASTBlockStatement.class);
returnVariableName = getReturnVariableName(node); returnVariableName = getReturnVariableName(node);
if (ASTBlockStatements.size() != 0) { if (ASTBlockStatements.size() != 0) {
for (ASTBlockStatement blockStatement : ASTBlockStatements) { for (ASTBlockStatement blockStatement : ASTBlockStatements) {
if (blockStatement if (blockStatement.hasDescendantOfType(ASTLocalVariableDeclaration.class)) {
.hasDescendantOfType(ASTLocalVariableDeclaration.class)) { List<ASTLocalVariableDeclaration> lVarList = blockStatement
List<ASTLocalVariableDeclaration> lVarList=blockStatement.findDescendantsOfType(ASTLocalVariableDeclaration.class); .findDescendantsOfType(ASTLocalVariableDeclaration.class);
if(!lVarList.isEmpty()){ if (!lVarList.isEmpty()) {
for(ASTLocalVariableDeclaration localVar : lVarList){ for (ASTLocalVariableDeclaration localVar : lVarList) {
localVarName = localVar.getVariableName(); localVarName = localVar.getVariableName();
if (returnVariableName != null if (returnVariableName != null && returnVariableName.equals(localVarName)) {
&& returnVariableName.equals(localVarName)) {
violation = true; violation = true;
break; break;
} }
@ -99,11 +87,9 @@ public class SingletonClassReturningNewInstanceRule extends AbstractJavaRule {
private String getReturnVariableName(ASTMethodDeclaration node) { private String getReturnVariableName(ASTMethodDeclaration node) {
List<ASTReturnStatement> rsl = node List<ASTReturnStatement> rsl = node.findDescendantsOfType(ASTReturnStatement.class);
.findDescendantsOfType(ASTReturnStatement.class);
ASTReturnStatement rs = rsl.get(0); ASTReturnStatement rs = rsl.get(0);
List<ASTPrimaryExpression> pel = rs List<ASTPrimaryExpression> pel = rs.findDescendantsOfType(ASTPrimaryExpression.class);
.findDescendantsOfType(ASTPrimaryExpression.class);
ASTPrimaryExpression ape = pel.get(0); ASTPrimaryExpression ape = pel.get(0);
Node lastChild = ape.jjtGetChild(0); Node lastChild = ape.jjtGetChild(0);
String returnVariableName = null; String returnVariableName = null;
@ -119,8 +105,7 @@ public class SingletonClassReturningNewInstanceRule extends AbstractJavaRule {
} }
private String getNameFromPrimaryPrefix(ASTPrimaryPrefix pp) { private String getNameFromPrimaryPrefix(ASTPrimaryPrefix pp) {
if ((pp.jjtGetNumChildren() == 1) if ((pp.jjtGetNumChildren() == 1) && (pp.jjtGetChild(0) instanceof ASTName)) {
&& (pp.jjtGetChild(0) instanceof ASTName)) {
return ((ASTName) pp.jjtGetChild(0)).getImage(); return ((ASTName) pp.jjtGetChild(0)).getImage();
} }
return null; return null;