Clover coverage report - PMD - 3.7 Coverage timestamp: Wed May 31 2006 09:25:59 EDT FRAMES NO FRAMES |

file stats: | LOC: | 69 |  | Methods: | 2
| NCLOC: | 50 |  | Classes: | 1

Source file | Conditionals | Statements | Methods | TOTAL
MethodReturnsInternalArray.java | 83.3% | 91.7% | 100% | 88.6% |

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