View Javadoc

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