View Javadoc

    1   /*
    2    * Created on Jan 17, 2005
    3    *
    4    * $Id: AbstractSunSecureRule.java,v 1.7 2006/02/10 14:26:31 tomcopeland 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.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       *
    75       * @param vn   the variable name
    76       * @param node the ASTMethodDeclaration where the local variable name will be searched
    77       * @return <code>true</code> if the method declaration contains any local variable named vn and <code>false</code> in other case
    78       */
    79      protected boolean isLocalVariable(String vn, ASTMethodDeclaration node) {
    80          final List lvars = node.findChildrenOfType(ASTLocalVariableDeclaration.class);
    81          if (lvars != null) {
    82              for (Iterator it = lvars.iterator(); it.hasNext();) {
    83                  final ASTLocalVariableDeclaration lvd = (ASTLocalVariableDeclaration) it.next();
    84                  final ASTVariableDeclaratorId vid = (ASTVariableDeclaratorId) lvd.getFirstChildOfType(ASTVariableDeclaratorId.class);
    85                  if (vid != null && vid.getImage().equals(vn)) {
    86                      return true;
    87                  }
    88              }
    89          }
    90          return false;
    91      }
    92
    93      /***
    94       * Gets the image of the first ASTName node found by {@link SimpleNode#getFirstChildOfType(Class)}
    95       *
    96       * @param n the node to search
    97       * @return the image of the first ASTName or <code>null</code>
    98       */
    99      protected String getFirstNameImage(SimpleNode n) {
    100         ASTName name = (ASTName) n.getFirstChildOfType(ASTName.class);
    101         if (name != null)
    102             return name.getImage();
    103         return null;
    104     }
    105
    106 }