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;
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;
@ -28,6 +22,7 @@ public class SingleMethodSingletonRule extends AbstractJavaRule {
private static Set<ASTFieldDeclaration> returnset = new HashSet<ASTFieldDeclaration>();
private boolean violation = false;
private static Set<String> methodset = new HashSet<String>();
@Override
public Object visit(ASTFieldDeclaration node, Object data) {
@ -50,6 +45,7 @@ public class SingleMethodSingletonRule extends AbstractJavaRule {
methodset.clear();
return super.visit(node, data);
}
@Override
public Object visit(ASTMethodDeclaration node, Object data) {
@ -72,8 +68,7 @@ public class SingleMethodSingletonRule extends AbstractJavaRule {
}
private String getNameFromPrimaryPrefix(ASTPrimaryPrefix pp) {
if ((pp.jjtGetNumChildren() == 1)
&& (pp.jjtGetChild(0) instanceof ASTName)) {
if ((pp.jjtGetNumChildren() == 1) && (pp.jjtGetChild(0) instanceof ASTName)) {
return ((ASTName) pp.jjtGetChild(0)).getImage();
}
return null;

View File

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