Clover coverage report - PMD - 3.7
Coverage timestamp: Wed May 31 2006 09:25:59 EDT
file stats: LOC: 80   Methods: 3
NCLOC: 67   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
BeanMembersShouldSerializeRule.java 86.4% 94.3% 100% 91.7%
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.MethodNameDeclaration;
 12    import net.sourceforge.pmd.symboltable.VariableNameDeclaration;
 13   
 14    import java.util.ArrayList;
 15    import java.util.Arrays;
 16    import java.util.Iterator;
 17    import java.util.List;
 18    import java.util.Map;
 19   
 20    public class BeanMembersShouldSerializeRule extends AbstractRule {
 21   
 22  10 public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
 23  10 if (node.isInterface()) {
 24  0 return data;
 25    }
 26   
 27  10 Map methods = node.getScope().getEnclosingClassScope().getMethodDeclarations();
 28  10 List getSetMethList = new ArrayList();
 29  10 for (Iterator i = methods.keySet().iterator(); i.hasNext();) {
 30  12 ASTMethodDeclarator mnd = ((MethodNameDeclaration) i.next()).getMethodNameDeclaratorNode();
 31  12 if (isBeanAccessor(mnd)) {
 32  11 getSetMethList.add(mnd);
 33    }
 34    }
 35   
 36  10 String[] methNameArray = new String[getSetMethList.size()];
 37  10 for (int i = 0; i < getSetMethList.size(); i++) {
 38  11 methNameArray[i] = ((ASTMethodDeclarator) getSetMethList.get(i)).getImage();
 39    }
 40   
 41  10 Arrays.sort(methNameArray);
 42   
 43  10 Map vars = node.getScope().getVariableDeclarations();
 44  10 for (Iterator i = vars.keySet().iterator(); i.hasNext();) {
 45  16 VariableNameDeclaration decl = (VariableNameDeclaration) i.next();
 46  16 if (((List) vars.get(decl)).isEmpty() || decl.getAccessNodeParent().isTransient() || decl.getAccessNodeParent().isStatic()) {
 47  8 continue;
 48    }
 49  8 String varName = trimIfPrefix(decl.getImage());
 50  8 varName = varName.substring(0, 1).toUpperCase() + varName.substring(1, varName.length());
 51  8 boolean hasGetMethod = Arrays.binarySearch(methNameArray, "get" + varName) >= 0 || Arrays.binarySearch(methNameArray, "is" + varName) >= 0;
 52  8 boolean hasSetMethod = Arrays.binarySearch(methNameArray, "set" + varName) >= 0;
 53  8 if (!hasGetMethod || !hasSetMethod) {
 54  5 addViolation(data, decl.getNode(), decl.getImage());
 55    }
 56    }
 57  10 return super.visit(node, data);
 58    }
 59   
 60  8 private String trimIfPrefix(String img) {
 61  8 if (getStringProperty("prefix") != null && img.startsWith(getStringProperty("prefix"))) {
 62  8 return img.substring(getStringProperty("prefix").length());
 63    }
 64  0 return img;
 65    }
 66   
 67  12 private boolean isBeanAccessor(ASTMethodDeclarator meth) {
 68  12 if (meth.getImage().startsWith("get") || meth.getImage().startsWith("set")) {
 69  10 return true;
 70    }
 71  2 if (meth.getImage().startsWith("is")) {
 72  2 ASTResultType ret = (ASTResultType) meth.jjtGetParent().jjtGetChild(0);
 73  2 List primitives = ret.findChildrenOfType(ASTPrimitiveType.class);
 74  2 if (!primitives.isEmpty() && ((ASTPrimitiveType) primitives.get(0)).isBoolean()) {
 75  1 return true;
 76    }
 77    }
 78  1 return false;
 79    }
 80    }