KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > pmd > rules > sunsecure > AbstractSunSecureRule


1 /*
2  * Created on Jan 17, 2005
3  *
4  * $Id: AbstractSunSecureRule.java,v 1.10 2006/10/17 01:07:18 hooperbloob Exp $
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.ASTName;
12 import net.sourceforge.pmd.ast.ASTPrimarySuffix;
13 import net.sourceforge.pmd.ast.ASTReturnStatement;
14 import net.sourceforge.pmd.ast.ASTTypeDeclaration;
15 import net.sourceforge.pmd.ast.ASTVariableDeclaratorId;
16 import net.sourceforge.pmd.ast.SimpleNode;
17
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20
21 /**
22  * Utility methods for the package
23  *
24  * @author mgriffa
25  */

26 public abstract class AbstractSunSecureRule extends AbstractRule {
27
28     /**
29      * Tells if the type declaration has a field with varName.
30      *
31      * @param varName the name of the field to search
32      * @param typeDeclaration the type declaration
33      * @return <code>true</code> if there is a field in the type declaration named varName, <code>false</code> in other case
34      */

35     protected final boolean isField(String JavaDoc varName, ASTTypeDeclaration typeDeclaration) {
36         final List JavaDoc fds = typeDeclaration.findChildrenOfType(ASTFieldDeclaration.class);
37         if (fds != null) {
38             for (Iterator JavaDoc it = fds.iterator(); it.hasNext();) {
39                 final ASTFieldDeclaration fd = (ASTFieldDeclaration) it.next();
40                 final ASTVariableDeclaratorId vid = (ASTVariableDeclaratorId) fd.getFirstChildOfType(ASTVariableDeclaratorId.class);
41                 if (vid != null && vid.hasImageEqualTo(varName)) {
42                     return true;
43                 }
44             }
45         }
46         return false;
47     }
48
49
50     /**
51      * Gets the name of the variable returned.
52      * Some examples: <br>
53      * for this.foo returns foo <br>
54      * for foo returns foo <br>
55      * for foo.bar returns foo.bar
56      *
57      * @param ret a return statement to evaluate
58      * @return the name of the variable associated or <code>null</code> if it cannot be detected
59      */

60     protected final String JavaDoc getReturnedVariableName(ASTReturnStatement ret) {
61         final ASTName n = (ASTName) ret.getFirstChildOfType(ASTName.class);
62         if (n != null)
63             return n.getImage();
64         final ASTPrimarySuffix ps = (ASTPrimarySuffix) ret.getFirstChildOfType(ASTPrimarySuffix.class);
65         if (ps != null)
66             return ps.getImage();
67         return null;
68     }
69
70     /**
71      * TODO modify usages to use symbol table
72      * Tells if the variable name is a local variable declared in the method.
73      *
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 JavaDoc vn, SimpleNode node) {
79         final List JavaDoc lvars = node.findChildrenOfType(ASTLocalVariableDeclaration.class);
80         if (lvars != null) {
81             for (Iterator JavaDoc 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.hasImageEqualTo(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 JavaDoc 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 }
106
Popular Tags