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