Clover coverage report - PMD - 3.3
Coverage timestamp: Thu Sep 15 2005 17:59:57 EDT
file stats: LOC: 73   Methods: 2
NCLOC: 60   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
BeanMembersShouldSerializeRule.java 90% 96.9% 100% 94.4%
coverage coverage
 1    /**
 2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
 3    */
 4    package net.sourceforge.pmd.rules;
 5   
 6    import net.sourceforge.pmd.AbstractRule;
 7    import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration;
 8    import net.sourceforge.pmd.ast.ASTMethodDeclarator;
 9    import net.sourceforge.pmd.ast.ASTPrimitiveType;
 10    import net.sourceforge.pmd.ast.ASTResultType;
 11    import net.sourceforge.pmd.symboltable.VariableNameDeclaration;
 12   
 13    import java.util.ArrayList;
 14    import java.util.Arrays;
 15    import java.util.Iterator;
 16    import java.util.List;
 17    import java.util.Map;
 18   
 19    public class BeanMembersShouldSerializeRule extends AbstractRule {
 20   
 21  8 public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
 22  8 if (node.isInterface()) {
 23  0 return data;
 24    }
 25   
 26    // TODO use ClassScope.getMethodDeclarations instead
 27  8 List methList = node.findChildrenOfType(ASTMethodDeclarator.class);
 28   
 29  8 List getSetMethList = new ArrayList();
 30  8 for (Iterator i = methList.iterator(); i.hasNext();) {
 31  8 ASTMethodDeclarator meth = (ASTMethodDeclarator) i.next();
 32  8 if (isBeanAccessor(meth)) {
 33  7 getSetMethList.add(meth);
 34    }
 35    }
 36  8 String[] methNameArray = new String[getSetMethList.size()];
 37  8 for (int i = 0; i < getSetMethList.size(); i++) {
 38  7 methNameArray[i] = ((ASTMethodDeclarator) getSetMethList.get(i)).getImage();
 39    }
 40   
 41  8 Arrays.sort(methNameArray);
 42   
 43  8 Map vars = node.getScope().getVariableDeclarations();
 44  8 for (Iterator i = vars.keySet().iterator(); i.hasNext();) {
 45  14 VariableNameDeclaration decl = (VariableNameDeclaration) i.next();
 46  14 if (((List) vars.get(decl)).isEmpty() || decl.getAccessNodeParent().isTransient() || decl.getAccessNodeParent().isStatic()) {
 47  8 continue;
 48    }
 49  6 String varName = decl.getImage();
 50  6 varName = varName.substring(0, 1).toUpperCase() + varName.substring(1, varName.length());
 51  6 boolean hasGetMethod = Arrays.binarySearch(methNameArray, "get" + varName) >= 0 || Arrays.binarySearch(methNameArray, "is" + varName) >= 0;
 52  6 boolean hasSetMethod = Arrays.binarySearch(methNameArray, "set" + varName) >= 0;
 53  6 if (!hasGetMethod || !hasSetMethod) {
 54  4 addViolation(data, decl.getNode(), decl.getImage());
 55    }
 56    }
 57  8 return super.visit(node, data);
 58    }
 59   
 60  8 private boolean isBeanAccessor(ASTMethodDeclarator meth) {
 61  8 if (meth.getImage().startsWith("get") || meth.getImage().startsWith("set")) {
 62  6 return true;
 63    }
 64  2 if (meth.getImage().startsWith("is")) {
 65  2 ASTResultType ret = (ASTResultType) meth.jjtGetParent().jjtGetChild(0);
 66  2 List primitives = ret.findChildrenOfType(ASTPrimitiveType.class);
 67  2 if (!primitives.isEmpty() && ((ASTPrimitiveType) primitives.get(0)).isBoolean()) {
 68  1 return true;
 69    }
 70    }
 71  1 return false;
 72    }
 73    }