Clover coverage report - PMD - 3.3
Coverage timestamp: Thu Sep 15 2005 17:59:57 EDT
file stats: LOC: 65   Methods: 2
NCLOC: 46   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
MethodReturnsInternalArray.java 81.2% 90.9% 100% 87.5%
coverage coverage
 1    /*
 2    * Created on Jan 17, 2005
 3    *
 4    * $Id: MethodReturnsInternalArray.java,v 1.9 2005/09/02 19:36:22 tomcopeland Exp $
 5    */
 6    package net.sourceforge.pmd.rules.sunsecure;
 7   
 8    import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration;
 9    import net.sourceforge.pmd.ast.ASTMethodDeclaration;
 10    import net.sourceforge.pmd.ast.ASTPrimaryPrefix;
 11    import net.sourceforge.pmd.ast.ASTPrimarySuffix;
 12    import net.sourceforge.pmd.ast.ASTReturnStatement;
 13    import net.sourceforge.pmd.ast.ASTTypeDeclaration;
 14   
 15    import java.util.Iterator;
 16    import java.util.List;
 17   
 18    /**
 19    * Implementation note: this rule currently ignores return types of y.x.z,
 20    * currently it handles only local type fields.
 21    *
 22    * @author mgriffa
 23    */
 24    public class MethodReturnsInternalArray extends AbstractSunSecureRule {
 25   
 26  8 public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
 27  8 if (node.isInterface()) {
 28  0 return data;
 29    }
 30  8 return super.visit(node, data);
 31    }
 32   
 33  8 public Object visit(ASTMethodDeclaration method, Object data) {
 34  8 if (!method.getResultType().returnsArray()) {
 35  0 return data;
 36    }
 37  8 List returns = method.findChildrenOfType(ASTReturnStatement.class);
 38  8 ASTTypeDeclaration td = (ASTTypeDeclaration) method.getFirstParentOfType(ASTTypeDeclaration.class);
 39  8 for (Iterator it = returns.iterator() ; it.hasNext() ; ) {
 40  8 final ASTReturnStatement ret = (ASTReturnStatement) it.next();
 41  8 final String vn = getReturnedVariableName(ret);
 42  8 if (!isField(vn, td)) {
 43  3 continue;
 44    }
 45  5 if (ret.findChildrenOfType(ASTPrimarySuffix.class).size() > 2) {
 46  1 continue;
 47    }
 48  4 if (!isLocalVariable(vn, method)) {
 49  2 addViolation(data, ret, vn);
 50    } else {
 51    // This is to handle field hiding
 52  2 final ASTPrimaryPrefix pp = (ASTPrimaryPrefix) ret.getFirstChildOfType(ASTPrimaryPrefix.class);
 53  2 if (pp!=null && pp.usesThisModifier()) {
 54  1 final ASTPrimarySuffix ps = (ASTPrimarySuffix) ret.getFirstChildOfType(ASTPrimarySuffix.class);
 55  1 if (ps.getImage().equals(vn)) {
 56  1 addViolation(data, ret, vn);
 57    }
 58    }
 59    }
 60    }
 61  8 return data;
 62    }
 63   
 64   
 65    }