1
2
3
4
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 public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
27 if (node.isInterface()) {
28 return data;
29 }
30 return super.visit(node, data);
31 }
32
33 public Object visit(ASTMethodDeclaration method, Object data) {
34 if (!method.getResultType().returnsArray()) {
35 return data;
36 }
37 List returns = method.findChildrenOfType(ASTReturnStatement.class);
38 ASTTypeDeclaration td = (ASTTypeDeclaration) method.getFirstParentOfType(ASTTypeDeclaration.class);
39 for (Iterator it = returns.iterator() ; it.hasNext() ; ) {
40 final ASTReturnStatement ret = (ASTReturnStatement) it.next();
41 final String vn = getReturnedVariableName(ret);
42 if (!isField(vn, td)) {
43 continue;
44 }
45 if (ret.findChildrenOfType(ASTPrimarySuffix.class).size() > 2) {
46 continue;
47 }
48 if (!isLocalVariable(vn, method)) {
49 addViolation(data, ret, vn);
50 } else {
51
52 final ASTPrimaryPrefix pp = (ASTPrimaryPrefix) ret.getFirstChildOfType(ASTPrimaryPrefix.class);
53 if (pp!=null && pp.usesThisModifier()) {
54 final ASTPrimarySuffix ps = (ASTPrimarySuffix) ret.getFirstChildOfType(ASTPrimarySuffix.class);
55 if (ps.getImage().equals(vn)) {
56 addViolation(data, ret, vn);
57 }
58 }
59 }
60 }
61 return data;
62 }
63
64
65 }