1
2
3
4 package net.sourceforge.pmd.lang.java.rule.sunsecure;
5
6 import java.util.List;
7
8 import net.sourceforge.pmd.lang.ast.Node;
9 import net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration;
10 import net.sourceforge.pmd.lang.java.ast.ASTLocalVariableDeclaration;
11 import net.sourceforge.pmd.lang.java.ast.ASTName;
12 import net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix;
13 import net.sourceforge.pmd.lang.java.ast.ASTReturnStatement;
14 import net.sourceforge.pmd.lang.java.ast.ASTTypeDeclaration;
15 import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId;
16 import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
17
18
19
20
21
22
23
24 public abstract class AbstractSunSecureRule extends AbstractJavaRule {
25
26
27
28
29
30
31
32
33 protected final boolean isField(String varName, ASTTypeDeclaration typeDeclaration) {
34 final List<ASTFieldDeclaration> fds = typeDeclaration.findDescendantsOfType(ASTFieldDeclaration.class);
35 if (fds != null) {
36 for (ASTFieldDeclaration fd: fds) {
37 final ASTVariableDeclaratorId vid = fd.getFirstDescendantOfType(ASTVariableDeclaratorId.class);
38 if (vid != null && vid.hasImageEqualTo(varName)) {
39 return true;
40 }
41 }
42 }
43 return false;
44 }
45
46
47
48
49
50
51
52
53
54
55
56
57 protected final String getReturnedVariableName(ASTReturnStatement ret) {
58 final ASTName n = ret.getFirstDescendantOfType(ASTName.class);
59 if (n != null) {
60 return n.getImage();
61 }
62 final ASTPrimarySuffix ps = ret.getFirstDescendantOfType(ASTPrimarySuffix.class);
63 if (ps != null) {
64 return ps.getImage();
65 }
66 return null;
67 }
68
69
70
71
72
73
74
75
76
77 protected boolean isLocalVariable(String vn, Node node) {
78 final List<ASTLocalVariableDeclaration> lvars = node.findDescendantsOfType(ASTLocalVariableDeclaration.class);
79 if (lvars != null) {
80 for (ASTLocalVariableDeclaration lvd: lvars) {
81 final ASTVariableDeclaratorId vid = lvd.getFirstDescendantOfType(ASTVariableDeclaratorId.class);
82 if (vid != null && vid.hasImageEqualTo(vn)) {
83 return true;
84 }
85 }
86 }
87 return false;
88 }
89
90
91
92
93
94
95
96 protected String getFirstNameImage(Node n) {
97 ASTName name = n.getFirstDescendantOfType(ASTName.class);
98 if (name != null) {
99 return name.getImage();
100 }
101 return null;
102 }
103
104 }