View Javadoc

    1   /*
    2    * Created on Jan 17, 2005
    3    *
    4    * $Id$
    5    */
    6   package net.sourceforge.pmd.lang.java.rule.sunsecure;
    7
    8   import java.util.List;
    9
    10  import net.sourceforge.pmd.lang.ast.Node;
    11  import net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration;
    12  import net.sourceforge.pmd.lang.java.ast.ASTLocalVariableDeclaration;
    13  import net.sourceforge.pmd.lang.java.ast.ASTName;
    14  import net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix;
    15  import net.sourceforge.pmd.lang.java.ast.ASTReturnStatement;
    16  import net.sourceforge.pmd.lang.java.ast.ASTTypeDeclaration;
    17  import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId;
    18  import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
    19
    20  /**
    21   * Utility methods for the package
    22   *
    23   * @author mgriffa
    24   */
    25  public abstract class AbstractSunSecureRule extends AbstractJavaRule {
    26
    27      /**
    28       * Tells if the type declaration has a field with varName.
    29       *
    30       * @param varName         the name of the field to search
    31       * @param typeDeclaration the type declaration
    32       * @return <code>true</code> if there is a field in the type declaration named varName, <code>false</code> in other case
    33       */
    34      protected final boolean isField(String varName, ASTTypeDeclaration typeDeclaration) {
    35          final List<ASTFieldDeclaration> fds = typeDeclaration.findDescendantsOfType(ASTFieldDeclaration.class);
    36          if (fds != null) {
    37              for (ASTFieldDeclaration fd: fds) {
    38                  final ASTVariableDeclaratorId vid = fd.getFirstDescendantOfType(ASTVariableDeclaratorId.class);
    39                  if (vid != null && vid.hasImageEqualTo(varName)) {
    40                      return true;
    41                  }
    42              }
    43          }
    44          return false;
    45      }
    46
    47
    48      /**
    49       * Gets the name of the variable returned.
    50       * Some examples: <br>
    51       * for this.foo returns foo <br>
    52       * for foo returns foo <br>
    53       * for foo.bar returns foo.bar
    54       *
    55       * @param ret a return statement to evaluate
    56       * @return the name of the variable associated or <code>null</code> if it cannot be detected
    57       */
    58      protected final String getReturnedVariableName(ASTReturnStatement ret) {
    59          final ASTName n = ret.getFirstDescendantOfType(ASTName.class);
    60          if (n != null) {
    61  	    return n.getImage();
    62  	}
    63          final ASTPrimarySuffix ps = ret.getFirstDescendantOfType(ASTPrimarySuffix.class);
    64          if (ps != null) {
    65  	    return ps.getImage();
    66  	}
    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 vn, Node node) {
    79          final List<ASTLocalVariableDeclaration> lvars = node.findDescendantsOfType(ASTLocalVariableDeclaration.class);
    80          if (lvars != null) {
    81              for (ASTLocalVariableDeclaration lvd: lvars) {
    82                  final ASTVariableDeclaratorId vid = lvd.getFirstDescendantOfType(ASTVariableDeclaratorId.class);
    83                  if (vid != null && vid.hasImageEqualTo(vn)) {
    84                      return true;
    85                  }
    86              }
    87          }
    88          return false;
    89      }
    90
    91      /**
    92       * Gets the image of the first ASTName node found by {@link Node#getFirstDescendantOfType(Class)}
    93       *
    94       * @param n the node to search
    95       * @return the image of the first ASTName or <code>null</code>
    96       */
    97      protected String getFirstNameImage(Node n) {
    98          ASTName name = n.getFirstDescendantOfType(ASTName.class);
    99          if (name != null) {
    100 	    return name.getImage();
    101 	}
    102         return null;
    103     }
    104
    105 }