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.SimpleNode;
11  
12  public class MethodNameDeclaration extends AbstractNameDeclaration {
13  
14      public MethodNameDeclaration(ASTMethodDeclarator node) {
15          super(node);
16      }
17  
18      public int getParameterCount() {
19          return ((ASTMethodDeclarator) node).getParameterCount();
20      }
21  
22      public boolean isVarargs() {
23          ASTFormalParameters params = (ASTFormalParameters) node.jjtGetChild(0);
24          for (int i = 0; i < ((ASTMethodDeclarator) node).getParameterCount(); i++) {
25              ASTFormalParameter p = (ASTFormalParameter) params.jjtGetChild(i);
26              if (p.isVarargs()) {
27              	return true;
28              }
29          }
30          return false;
31      }
32  
33      public ASTMethodDeclarator getMethodNameDeclaratorNode() {
34          return (ASTMethodDeclarator) node;
35      }
36  
37      public String getParameterDisplaySignature() {
38          StringBuffer sb = new StringBuffer("(");
39          ASTFormalParameters params = (ASTFormalParameters) node.jjtGetChild(0);
40          // TODO - this can be optimized - add [0] then ,[n] in a loop.
41          //        no need to trim at the end
42          for (int i = 0; i < ((ASTMethodDeclarator) node).getParameterCount(); i++) {
43              ASTFormalParameter p = (ASTFormalParameter) params.jjtGetChild(i);
44              sb.append(p.getTypeNode().getTypeImage());
45              if (p.isVarargs()) {
46              	sb.append("...");
47              }
48              sb.append(',');
49          }
50          if (sb.charAt(sb.length() - 1) == ',') {
51              sb.deleteCharAt(sb.length() - 1);
52          }
53          sb.append(')');
54          return sb.toString();
55      }
56  
57      public boolean equals(Object o) {
58          if (!(o instanceof MethodNameDeclaration)) {
59              return false;
60          }
61  
62          MethodNameDeclaration other = (MethodNameDeclaration) o;
63  
64          // compare name
65          if (!other.node.getImage().equals(node.getImage())) {
66              return false;
67          }
68  
69          // compare parameter count - this catches the case where there are no params, too
70          if (((ASTMethodDeclarator) (other.node)).getParameterCount() != ((ASTMethodDeclarator) node).getParameterCount()) {
71              return false;
72          }
73  
74          // compare parameter types
75          ASTFormalParameters myParams = (ASTFormalParameters) node.jjtGetChild(0);
76          ASTFormalParameters otherParams = (ASTFormalParameters) other.node.jjtGetChild(0);
77          for (int i = 0; i < ((ASTMethodDeclarator) node).getParameterCount(); i++) {
78              ASTFormalParameter myParam = (ASTFormalParameter) myParams.jjtGetChild(i);
79              ASTFormalParameter otherParam = (ASTFormalParameter) otherParams.jjtGetChild(i);
80  
81              // Compare vararg
82              if (myParam.isVarargs() != otherParam.isVarargs()) {
83              	return false;
84              }
85  
86              SimpleNode myTypeNode = (SimpleNode) myParam.getTypeNode().jjtGetChild(0);
87              SimpleNode otherTypeNode = (SimpleNode) otherParam.getTypeNode().jjtGetChild(0);
88  
89              // compare primitive vs reference type
90              if (myTypeNode.getClass() != otherTypeNode.getClass()) {
91                  return false;
92              }
93  
94              // simple comparison of type images
95              // this can be fooled by one method using "String"
96              // and the other method using "java.lang.String"
97              // once we get real types in here that should get fixed
98              String myTypeImg;
99              String otherTypeImg;
100             if (myTypeNode instanceof ASTPrimitiveType) {
101                 myTypeImg = myTypeNode.getImage();
102                 otherTypeImg = otherTypeNode.getImage();
103             } else {
104                 myTypeImg = ((SimpleNode) (myTypeNode.jjtGetChild(0))).getImage();
105                 otherTypeImg = ((SimpleNode) (otherTypeNode.jjtGetChild(0))).getImage();
106             }
107 
108             if (!myTypeImg.equals(otherTypeImg)) {
109                 return false;
110             }
111 
112             // if type is ASTPrimitiveType and is an array, make sure the other one is also
113         }
114         return true;
115     }
116 
117     public int hashCode() {
118         return node.getImage().hashCode() + ((ASTMethodDeclarator) node).getParameterCount();
119     }
120 
121     public String toString() {
122         return "Method " + node.getImage() + ", line " + node.getBeginLine() + ", params = " + ((ASTMethodDeclarator) node).getParameterCount();
123     }
124 }