View Javadoc

1   /***
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.symboltable;
5   
6   import net.sourceforge.pmd.ast.ASTFormalParameter;
7   import net.sourceforge.pmd.ast.ASTFormalParameters;
8   import net.sourceforge.pmd.ast.ASTMethodDeclarator;
9   import net.sourceforge.pmd.ast.ASTPrimitiveType;
10  import net.sourceforge.pmd.ast.ASTType;
11  import net.sourceforge.pmd.ast.SimpleNode;
12  
13  public class MethodNameDeclaration extends AbstractNameDeclaration {
14  
15      public MethodNameDeclaration(ASTMethodDeclarator node) {
16          super(node);
17      }
18  
19      public int getParameterCount() {
20          return ((ASTMethodDeclarator) node).getParameterCount();
21      }
22  
23      public String getParameterDisplaySignature() {
24          StringBuffer sb = new StringBuffer("(");
25          ASTFormalParameters params = (ASTFormalParameters) node.jjtGetChild(0);
26          for (int i = 0; i < ((ASTMethodDeclarator) node).getParameterCount(); i++) {
27              ASTFormalParameter p = (ASTFormalParameter) params.jjtGetChild(i);
28              sb.append(((ASTType)p.getFirstChildOfType(ASTType.class)).getTypeImage());
29              sb.append(",");
30          }
31          if (sb.charAt(sb.length()-1) == ',') {
32              sb.deleteCharAt(sb.length()-1);
33          }
34          return sb.toString() + ")";
35      }
36  
37      public boolean equals(Object o) {
38          MethodNameDeclaration other = (MethodNameDeclaration) o;
39  
40          // compare name
41          if (!other.node.getImage().equals(node.getImage())) {
42              return false;
43          }
44  
45          // compare parameter count - this catches the case where there are no params, too
46          if (((ASTMethodDeclarator) (other.node)).getParameterCount() != ((ASTMethodDeclarator) node).getParameterCount()) {
47              return false;
48          }
49  
50          // compare parameter types
51          ASTFormalParameters myParams = (ASTFormalParameters) node.jjtGetChild(0);
52          ASTFormalParameters otherParams = (ASTFormalParameters) other.node.jjtGetChild(0);
53          for (int i = 0; i < ((ASTMethodDeclarator) node).getParameterCount(); i++) {
54              ASTFormalParameter myParam = (ASTFormalParameter) myParams.jjtGetChild(i);
55              ASTFormalParameter otherParam = (ASTFormalParameter) otherParams.jjtGetChild(i);
56  
57              SimpleNode myTypeNode = (SimpleNode) myParam.jjtGetChild(0).jjtGetChild(0);
58              SimpleNode otherTypeNode = (SimpleNode) otherParam.jjtGetChild(0).jjtGetChild(0);
59  
60              // compare primitive vs reference type
61              if (myTypeNode.getClass() != otherTypeNode.getClass()) {
62                  return false;
63              }
64  
65              // simple comparison of type images
66              // this can be fooled by one method using "String"
67              // and the other method using "java.lang.String"
68              // once we get real types in here that should get fixed
69              String myTypeImg;
70              String otherTypeImg;
71              if (myTypeNode instanceof ASTPrimitiveType) {
72                  myTypeImg = myTypeNode.getImage();
73                  otherTypeImg = otherTypeNode.getImage();
74              } else {
75                  myTypeImg = ((SimpleNode)(myTypeNode.jjtGetChild(0))).getImage();
76                  otherTypeImg = ((SimpleNode)(otherTypeNode.jjtGetChild(0))).getImage();
77              }
78  
79              if (!myTypeImg.equals(otherTypeImg)) {
80                  return false;
81              }
82  
83              // if type is ASTPrimitiveType and is an array, make sure the other one is also
84          }
85          return true;
86      }
87  
88      public int hashCode() {
89          return node.getImage().hashCode() + ((ASTMethodDeclarator) node).getParameterCount();
90      }
91  
92      public String toString() {
93          return "Method " + node.getImage() + ", line " + node.getBeginLine() + ", params = " +  ((ASTMethodDeclarator) node).getParameterCount();
94      }
95  }