View Javadoc

1   /*
2    * Created on Jan 17, 2005 
3    *
4    * $Id$
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.List;
19  
20  /**
21   * Utility methods for the package
22   *
23   * @author mgriffa
24   */
25  public abstract class AbstractSunSecureRule extends AbstractRule {
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.findChildrenOfType(ASTFieldDeclaration.class);
36          if (fds != null) {
37              for (ASTFieldDeclaration fd: fds) {
38                  final ASTVariableDeclaratorId vid = fd.getFirstChildOfType(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.getFirstChildOfType(ASTName.class);
60          if (n != null)
61              return n.getImage();
62          final ASTPrimarySuffix ps = ret.getFirstChildOfType(ASTPrimarySuffix.class);
63          if (ps != null)
64              return ps.getImage();
65          return null;
66      }
67  
68      /**
69       * TODO modify usages to use symbol table
70       * Tells if the variable name is a local variable declared in the method.
71       *
72       * @param vn   the variable name
73       * @param node the ASTMethodDeclaration where the local variable name will be searched
74       * @return <code>true</code> if the method declaration contains any local variable named vn and <code>false</code> in other case
75       */
76      protected boolean isLocalVariable(String vn, SimpleNode node) {
77          final List<ASTLocalVariableDeclaration> lvars = node.findChildrenOfType(ASTLocalVariableDeclaration.class);
78          if (lvars != null) {
79              for (ASTLocalVariableDeclaration lvd: lvars) {
80                  final ASTVariableDeclaratorId vid = lvd.getFirstChildOfType(ASTVariableDeclaratorId.class);
81                  if (vid != null && vid.hasImageEqualTo(vn)) {
82                      return true;
83                  }
84              }
85          }
86          return false;
87      }
88  
89      /**
90       * Gets the image of the first ASTName node found by {@link SimpleNode#getFirstChildOfType(Class)}
91       *
92       * @param n the node to search
93       * @return the image of the first ASTName or <code>null</code>
94       */
95      protected String getFirstNameImage(SimpleNode n) {
96          ASTName name = n.getFirstChildOfType(ASTName.class);
97          if (name != null)
98              return name.getImage();
99          return null;
100     }
101 
102 }