1
2
3
4
5
6 package net.sourceforge.pmd.rules.sunsecure;
7
8 import net.sourceforge.pmd.AbstractRule;
9 import net.sourceforge.pmd.ast.ASTFieldDeclaration;
10 import net.sourceforge.pmd.ast.ASTLocalVariableDeclaration;
11 import net.sourceforge.pmd.ast.ASTMethodDeclaration;
12 import net.sourceforge.pmd.ast.ASTName;
13 import net.sourceforge.pmd.ast.ASTPrimarySuffix;
14 import net.sourceforge.pmd.ast.ASTReturnStatement;
15 import net.sourceforge.pmd.ast.ASTTypeDeclaration;
16 import net.sourceforge.pmd.ast.ASTVariableDeclaratorId;
17 import net.sourceforge.pmd.ast.SimpleNode;
18
19 import java.util.Iterator;
20 import java.util.List;
21
22 /***
23 * Utility methods for the package
24 *
25 * @author mgriffa
26 */
27 public abstract class AbstractSunSecureRule extends AbstractRule {
28
29 /***
30 * Tells if the type declaration has a field with varName.
31 *
32 * @param varName the name of the field to search
33 * @param typeDeclaration the type declaration
34 * @return <code>true</code> if there is a field in the type declaration named varName, <code>false</code> in other case
35 */
36 protected final boolean isField(String varName, ASTTypeDeclaration typeDeclaration) {
37 final List fds = typeDeclaration.findChildrenOfType(ASTFieldDeclaration.class);
38 if (fds!=null) {
39 for (Iterator it=fds.iterator() ; it.hasNext(); ) {
40 final ASTFieldDeclaration fd = (ASTFieldDeclaration) it.next();
41 final ASTVariableDeclaratorId vid = (ASTVariableDeclaratorId) fd.getFirstChildOfType(ASTVariableDeclaratorId.class);
42 if (vid!=null && vid.getImage().equals(varName)) {
43 return true;
44 }
45 }
46 }
47 return false;
48 }
49
50
51 /***
52 * Gets the name of the variable returned.
53 * Some examples: <br>
54 * for this.foo returns foo <br>
55 * for foo returns foo <br>
56 * for foo.bar returns foo.bar
57 *
58 * @param ret a return statement to evaluate
59 * @return the name of the variable associated or <code>null</code> if it cannot be detected
60 */
61 protected final String getReturnedVariableName(ASTReturnStatement ret) {
62 final ASTName n = (ASTName) ret.getFirstChildOfType(ASTName.class);
63 if (n!=null)
64 return n.getImage();
65 final ASTPrimarySuffix ps = (ASTPrimarySuffix) ret.getFirstChildOfType(ASTPrimarySuffix.class);
66 if (ps!=null)
67 return ps.getImage();
68 return null;
69 }
70
71 /***
72 * TODO modify usages to use symbol table
73 * Tells if the variable name is a local variable declared in the method.
74 * @param vn the variable name
75 * @param node the ASTMethodDeclaration where the local variable name will be searched
76 * @return <code>true</code> if the method declaration contains any local variable named vn and <code>false</code> in other case
77 */
78 protected boolean isLocalVariable(String vn, ASTMethodDeclaration node) {
79 final List lvars = node.findChildrenOfType(ASTLocalVariableDeclaration.class);
80 if (lvars!=null) {
81 for (Iterator it = lvars.iterator() ; it.hasNext() ; ) {
82 final ASTLocalVariableDeclaration lvd = (ASTLocalVariableDeclaration) it.next();
83 final ASTVariableDeclaratorId vid = (ASTVariableDeclaratorId) lvd.getFirstChildOfType(ASTVariableDeclaratorId.class);
84 if (vid!=null && vid.getImage().equals(vn)) {
85 return true;
86 }
87 }
88 }
89 return false;
90 }
91
92 /***
93 * Gets the image of the first ASTName node found by {@link SimpleNode#getFirstChildOfType(Class)}
94 *
95 * @param n the node to search
96 * @return the image of the first ASTName or <code>null</code>
97 */
98 protected String getFirstNameImage(SimpleNode n) {
99 ASTName name = (ASTName) n.getFirstChildOfType(ASTName.class);
100 if (name!=null)
101 return name.getImage();
102 return null;
103 }
104
105 }