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.ASTConditionalExpression;
10 import net.sourceforge.pmd.lang.java.ast.ASTEqualityExpression;
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.ASTNullLiteral;
15 import net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix;
16 import net.sourceforge.pmd.lang.java.ast.ASTReturnStatement;
17 import net.sourceforge.pmd.lang.java.ast.ASTTypeDeclaration;
18 import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId;
19 import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
20
21
22
23
24
25
26
27
28 public abstract class AbstractSunSecureRule extends AbstractJavaRule {
29
30
31
32
33
34
35
36
37
38
39
40 protected final boolean isField(String varName, ASTTypeDeclaration typeDeclaration) {
41 final List<ASTFieldDeclaration> fds = typeDeclaration.findDescendantsOfType(ASTFieldDeclaration.class);
42 if (fds != null) {
43 for (ASTFieldDeclaration fd : fds) {
44 final ASTVariableDeclaratorId vid = fd.getFirstDescendantOfType(ASTVariableDeclaratorId.class);
45 if (vid != null && vid.hasImageEqualTo(varName)) {
46 return true;
47 }
48 }
49 }
50 return false;
51 }
52
53
54
55
56
57
58
59
60
61
62
63
64 protected final String getReturnedVariableName(ASTReturnStatement ret) {
65 if (hasTernaryCondition(ret) && hasTernaryNullCheck(ret)) {
66 return ret.getFirstDescendantOfType(ASTConditionalExpression.class).jjtGetChild(0)
67 .getFirstDescendantOfType(ASTName.class).getImage();
68 }
69
70 final ASTName n = ret.getFirstDescendantOfType(ASTName.class);
71 if (n != null) {
72 return n.getImage();
73 }
74 final ASTPrimarySuffix ps = ret.getFirstDescendantOfType(ASTPrimarySuffix.class);
75 if (ps != null) {
76 return ps.getImage();
77 }
78 return null;
79 }
80
81 private boolean hasTernaryNullCheck(ASTReturnStatement ret) {
82 ASTConditionalExpression condition = ret.getFirstDescendantOfType(ASTConditionalExpression.class);
83 return condition.jjtGetChild(0) instanceof ASTEqualityExpression
84 && condition.jjtGetChild(0).hasImageEqualTo("==")
85 && condition.jjtGetChild(0).jjtGetChild(0).hasDescendantOfType(ASTName.class)
86 && condition.jjtGetChild(0).jjtGetChild(1).hasDescendantOfType(ASTNullLiteral.class);
87 }
88
89 private boolean hasTernaryCondition(ASTReturnStatement ret) {
90 ASTConditionalExpression condition = ret.getFirstDescendantOfType(ASTConditionalExpression.class);
91 return condition != null && condition.isTernary();
92 }
93
94
95
96
97
98
99
100
101
102
103
104
105
106 protected boolean isLocalVariable(String vn, Node node) {
107 final List<ASTLocalVariableDeclaration> lvars = node.findDescendantsOfType(ASTLocalVariableDeclaration.class);
108 if (lvars != null) {
109 for (ASTLocalVariableDeclaration lvd : lvars) {
110 final ASTVariableDeclaratorId vid = lvd.getFirstDescendantOfType(ASTVariableDeclaratorId.class);
111 if (vid != null && vid.hasImageEqualTo(vn)) {
112 return true;
113 }
114 }
115 }
116 return false;
117 }
118
119
120
121
122
123
124
125
126
127 protected String getFirstNameImage(Node n) {
128 ASTName name = n.getFirstDescendantOfType(ASTName.class);
129 if (name != null) {
130 return name.getImage();
131 }
132 return null;
133 }
134
135 }