001 /*
002 * Created on Jan 17, 2005
003 *
004 * $Id: AbstractSunSecureRule.java 6245 2008-06-22 18:07:30Z xlv $
005 */
006 package net.sourceforge.pmd.lang.java.rule.sunsecure;
007
008 import java.util.List;
009
010 import net.sourceforge.pmd.lang.ast.Node;
011 import net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration;
012 import net.sourceforge.pmd.lang.java.ast.ASTLocalVariableDeclaration;
013 import net.sourceforge.pmd.lang.java.ast.ASTName;
014 import net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix;
015 import net.sourceforge.pmd.lang.java.ast.ASTReturnStatement;
016 import net.sourceforge.pmd.lang.java.ast.ASTTypeDeclaration;
017 import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId;
018 import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
019
020 /**
021 * Utility methods for the package
022 *
023 * @author mgriffa
024 */
025 public abstract class AbstractSunSecureRule extends AbstractJavaRule {
026
027 /**
028 * Tells if the type declaration has a field with varName.
029 *
030 * @param varName the name of the field to search
031 * @param typeDeclaration the type declaration
032 * @return <code>true</code> if there is a field in the type declaration named varName, <code>false</code> in other case
033 */
034 protected final boolean isField(String varName, ASTTypeDeclaration typeDeclaration) {
035 final List<ASTFieldDeclaration> fds = typeDeclaration.findDescendantsOfType(ASTFieldDeclaration.class);
036 if (fds != null) {
037 for (ASTFieldDeclaration fd: fds) {
038 final ASTVariableDeclaratorId vid = fd.getFirstDescendantOfType(ASTVariableDeclaratorId.class);
039 if (vid != null && vid.hasImageEqualTo(varName)) {
040 return true;
041 }
042 }
043 }
044 return false;
045 }
046
047
048 /**
049 * Gets the name of the variable returned.
050 * Some examples: <br>
051 * for this.foo returns foo <br>
052 * for foo returns foo <br>
053 * for foo.bar returns foo.bar
054 *
055 * @param ret a return statement to evaluate
056 * @return the name of the variable associated or <code>null</code> if it cannot be detected
057 */
058 protected final String getReturnedVariableName(ASTReturnStatement ret) {
059 final ASTName n = ret.getFirstDescendantOfType(ASTName.class);
060 if (n != null) {
061 return n.getImage();
062 }
063 final ASTPrimarySuffix ps = ret.getFirstDescendantOfType(ASTPrimarySuffix.class);
064 if (ps != null) {
065 return ps.getImage();
066 }
067 return null;
068 }
069
070 /**
071 * TODO modify usages to use symbol table
072 * Tells if the variable name is a local variable declared in the method.
073 *
074 * @param vn the variable name
075 * @param node the ASTMethodDeclaration where the local variable name will be searched
076 * @return <code>true</code> if the method declaration contains any local variable named vn and <code>false</code> in other case
077 */
078 protected boolean isLocalVariable(String vn, Node node) {
079 final List<ASTLocalVariableDeclaration> lvars = node.findDescendantsOfType(ASTLocalVariableDeclaration.class);
080 if (lvars != null) {
081 for (ASTLocalVariableDeclaration lvd: lvars) {
082 final ASTVariableDeclaratorId vid = lvd.getFirstDescendantOfType(ASTVariableDeclaratorId.class);
083 if (vid != null && vid.hasImageEqualTo(vn)) {
084 return true;
085 }
086 }
087 }
088 return false;
089 }
090
091 /**
092 * Gets the image of the first ASTName node found by {@link Node#getFirstDescendantOfType(Class)}
093 *
094 * @param n the node to search
095 * @return the image of the first ASTName or <code>null</code>
096 */
097 protected String getFirstNameImage(Node n) {
098 ASTName name = n.getFirstDescendantOfType(ASTName.class);
099 if (name != null) {
100 return name.getImage();
101 }
102 return null;
103 }
104
105 }
|