|
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 |
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 |
| |
|
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 |
| } |