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