1 |
| |
2 |
| |
3 |
| |
4 |
| |
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 |
| |
21 |
| |
22 |
| |
23 |
| |
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 |
| |
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 |
| } |