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