Clover coverage report - PMD - 3.9 Coverage timestamp: Tue Dec 19 2006 09:38:44 EST FRAMES NO FRAMES |
file stats: | LOC: | 69 |  | Methods: | 2
| NCLOC: | 50 |  | Classes: | 1
Source file | Conditionals | Statements | Methods | TOTAL
MethodReturnsInternalArray.java | 94.4% | 100% | 100% | 97.7% |

1 |  | /*
2 |  | * Created on Jan 17, 2005
3 |  | *
4 |  | * $Id: MethodReturnsInternalArray.java,v 1.14 2006/10/17 00:48:10 hooperbloob 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 | 11 | public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
28 | 11 | if (node.isInterface()) {
29 | 1 | return data;
30 |  | }
31 | 10 | return super.visit(node, data);
32 |  | }
33 |  |
34 | 10 | public Object visit(ASTMethodDeclaration method, Object data) {
35 | 10 | if (!method.getResultType().returnsArray()) {
36 | 1 | 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.hasImageEqualTo(vn)) {
60 | 1 | addViolation(data, ret, vn);
61 |  | }
62 |  | }
63 |  | }
64 |  | }
65 | 9 | return data;
66 |  | }
67 |  |
68 |  |
69 |  | }