whitespaces
This commit is contained in:
@ -3,79 +3,74 @@
|
|||||||
*/
|
*/
|
||||||
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;
|
||||||
|
|
||||||
public class SingleMethodSingletonRule extends AbstractJavaRule {
|
public class SingleMethodSingletonRule extends AbstractJavaRule {
|
||||||
|
|
||||||
private static Map<String, ASTFieldDeclaration> fieldDecls = new HashMap<String, ASTFieldDeclaration>();
|
private static Map<String, ASTFieldDeclaration> fieldDecls = new HashMap<String, ASTFieldDeclaration>();
|
||||||
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
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object visit(ASTCompilationUnit node, Object data){
|
public Object visit(ASTFieldDeclaration node, Object data) {
|
||||||
violation=false;
|
|
||||||
fieldDecls.clear();
|
|
||||||
returnset.clear();
|
|
||||||
methodset.clear();
|
|
||||||
return super.visit(node,data);
|
|
||||||
}
|
|
||||||
@Override
|
|
||||||
public Object visit(ASTMethodDeclaration node, Object data) {
|
|
||||||
|
|
||||||
violation=false;
|
if (node.isStatic() && node.isPrivate()) {
|
||||||
if (node.getResultType().isVoid()) {
|
ASTVariableDeclaratorId varDeclaratorId = node.getFirstDescendantOfType(ASTVariableDeclaratorId.class);
|
||||||
return super.visit(node, data);
|
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())){
|
@Override
|
||||||
violation=true;
|
public Object visit(ASTCompilationUnit node, Object data) {
|
||||||
}
|
violation = false;
|
||||||
}
|
fieldDecls.clear();
|
||||||
|
returnset.clear();
|
||||||
if(violation){
|
methodset.clear();
|
||||||
addViolation(data, node);
|
return super.visit(node, data);
|
||||||
}
|
}
|
||||||
return super.visit(node, data);
|
|
||||||
}
|
|
||||||
|
|
||||||
private String getNameFromPrimaryPrefix(ASTPrimaryPrefix pp) {
|
@Override
|
||||||
if ((pp.jjtGetNumChildren() == 1)
|
public Object visit(ASTMethodDeclaration node, Object data) {
|
||||||
&& (pp.jjtGetChild(0) instanceof ASTName)) {
|
|
||||||
return ((ASTName) pp.jjtGetChild(0)).getImage();
|
violation = false;
|
||||||
}
|
if (node.getResultType().isVoid()) {
|
||||||
return null;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,112 +17,97 @@ import net.sourceforge.pmd.lang.java.ast.ASTReturnStatement;
|
|||||||
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
|
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
|
||||||
|
|
||||||
public class SingletonClassReturningNewInstanceRule extends AbstractJavaRule {
|
public class SingletonClassReturningNewInstanceRule extends AbstractJavaRule {
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object visit(ASTMethodDeclaration node, Object data) {
|
|
||||||
|
|
||||||
boolean violation = false;
|
@Override
|
||||||
String localVarName = null;
|
public Object visit(ASTMethodDeclaration node, Object data) {
|
||||||
String returnVariableName = null;
|
|
||||||
|
|
||||||
if (node.getResultType().isVoid()) {
|
boolean violation = false;
|
||||||
return super.visit(node, data);
|
String localVarName = null;
|
||||||
}
|
String returnVariableName = null;
|
||||||
|
|
||||||
if ("getInstance".equals(node.getMethodName())) {
|
if (node.getResultType().isVoid()) {
|
||||||
List<ASTReturnStatement> rsl = node
|
return super.visit(node, data);
|
||||||
.findDescendantsOfType(ASTReturnStatement.class);
|
}
|
||||||
if (rsl.isEmpty()) {
|
|
||||||
return super.visit(node, data);
|
|
||||||
} else {
|
|
||||||
for(ASTReturnStatement rs : rsl){
|
|
||||||
|
|
||||||
List<ASTPrimaryExpression> pel = rs
|
|
||||||
.findDescendantsOfType(ASTPrimaryExpression.class);
|
|
||||||
ASTPrimaryExpression ape = pel.get(0);
|
|
||||||
if (ape.getFirstDescendantOfType(ASTAllocationExpression.class) != null) {
|
|
||||||
violation = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
if ("getInstance".equals(node.getMethodName())) {
|
||||||
* public class Singleton {
|
List<ASTReturnStatement> rsl = node.findDescendantsOfType(ASTReturnStatement.class);
|
||||||
*
|
if (rsl.isEmpty()) {
|
||||||
* private static Singleton m_instance=null;
|
return super.visit(node, data);
|
||||||
*
|
} else {
|
||||||
* public static Singleton getInstance() {
|
for (ASTReturnStatement rs : rsl) {
|
||||||
*
|
|
||||||
* Singleton m_instance=null;
|
|
||||||
*
|
|
||||||
* if ( m_instance == null ) {
|
|
||||||
* synchronized(Singleton.class) {
|
|
||||||
* if(m_instance == null) {
|
|
||||||
* m_instance = new Singleton();
|
|
||||||
* }
|
|
||||||
* }
|
|
||||||
* }
|
|
||||||
* return m_instance;
|
|
||||||
* }
|
|
||||||
* }
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
List<ASTBlockStatement> ASTBlockStatements = node
|
List<ASTPrimaryExpression> pel = rs.findDescendantsOfType(ASTPrimaryExpression.class);
|
||||||
.findDescendantsOfType(ASTBlockStatement.class);
|
ASTPrimaryExpression ape = pel.get(0);
|
||||||
returnVariableName = getReturnVariableName(node);
|
if (ape.getFirstDescendantOfType(ASTAllocationExpression.class) != null) {
|
||||||
if (ASTBlockStatements.size() != 0) {
|
violation = true;
|
||||||
for (ASTBlockStatement blockStatement : ASTBlockStatements) {
|
break;
|
||||||
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)) {
|
|
||||||
violation = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (violation) {
|
|
||||||
addViolation(data, node);
|
|
||||||
}
|
|
||||||
return super.visit(node, data);
|
|
||||||
}
|
|
||||||
|
|
||||||
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<ASTReturnStatement> rsl = node
|
List<ASTBlockStatement> ASTBlockStatements = node.findDescendantsOfType(ASTBlockStatement.class);
|
||||||
.findDescendantsOfType(ASTReturnStatement.class);
|
returnVariableName = getReturnVariableName(node);
|
||||||
ASTReturnStatement rs = rsl.get(0);
|
if (ASTBlockStatements.size() != 0) {
|
||||||
List<ASTPrimaryExpression> pel = rs
|
for (ASTBlockStatement blockStatement : ASTBlockStatements) {
|
||||||
.findDescendantsOfType(ASTPrimaryExpression.class);
|
if (blockStatement.hasDescendantOfType(ASTLocalVariableDeclaration.class)) {
|
||||||
ASTPrimaryExpression ape = pel.get(0);
|
List<ASTLocalVariableDeclaration> lVarList = blockStatement
|
||||||
Node lastChild = ape.jjtGetChild(0);
|
.findDescendantsOfType(ASTLocalVariableDeclaration.class);
|
||||||
String returnVariableName = null;
|
if (!lVarList.isEmpty()) {
|
||||||
if (lastChild instanceof ASTPrimaryPrefix) {
|
for (ASTLocalVariableDeclaration localVar : lVarList) {
|
||||||
returnVariableName = getNameFromPrimaryPrefix((ASTPrimaryPrefix) lastChild);
|
localVarName = localVar.getVariableName();
|
||||||
}
|
if (returnVariableName != null && returnVariableName.equals(localVarName)) {
|
||||||
/*
|
violation = true;
|
||||||
* if(lastChild instanceof ASTPrimarySuffix){ returnVariableName =
|
break;
|
||||||
* getNameFromPrimarySuffix((ASTPrimarySuffix) lastChild); }
|
}
|
||||||
*/
|
}
|
||||||
return returnVariableName;
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (violation) {
|
||||||
|
addViolation(data, node);
|
||||||
|
}
|
||||||
|
return super.visit(node, data);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
private String getReturnVariableName(ASTMethodDeclaration node) {
|
||||||
|
|
||||||
private String getNameFromPrimaryPrefix(ASTPrimaryPrefix pp) {
|
List<ASTReturnStatement> rsl = node.findDescendantsOfType(ASTReturnStatement.class);
|
||||||
if ((pp.jjtGetNumChildren() == 1)
|
ASTReturnStatement rs = rsl.get(0);
|
||||||
&& (pp.jjtGetChild(0) instanceof ASTName)) {
|
List<ASTPrimaryExpression> pel = rs.findDescendantsOfType(ASTPrimaryExpression.class);
|
||||||
return ((ASTName) pp.jjtGetChild(0)).getImage();
|
ASTPrimaryExpression ape = pel.get(0);
|
||||||
}
|
Node lastChild = ape.jjtGetChild(0);
|
||||||
return null;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user