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          MethodNameDeclaration other = (MethodNameDeclaration) o;
59  
60          // compare name
61          if (!other.node.getImage().equals(node.getImage())) {
62              return false;
63          }
64  
65          // compare parameter count - this catches the case where there are no params, too
66          if (((ASTMethodDeclarator) (other.node)).getParameterCount() != ((ASTMethodDeclarator) node).getParameterCount()) {
67              return false;
68          }
69  
70          // compare parameter types
71          ASTFormalParameters myParams = (ASTFormalParameters) node.jjtGetChild(0);
72          ASTFormalParameters otherParams = (ASTFormalParameters) other.node.jjtGetChild(0);
73          for (int i = 0; i < ((ASTMethodDeclarator) node).getParameterCount(); i++) {
74              ASTFormalParameter myParam = (ASTFormalParameter) myParams.jjtGetChild(i);
75              ASTFormalParameter otherParam = (ASTFormalParameter) otherParams.jjtGetChild(i);
76  
77              // Compare vararg
78              if (myParam.isVarargs() != otherParam.isVarargs()) {
79              	return false;
80              }
81  
82              SimpleNode myTypeNode = (SimpleNode) myParam.getTypeNode().jjtGetChild(0);
83              SimpleNode otherTypeNode = (SimpleNode) otherParam.getTypeNode().jjtGetChild(0);
84  
85              // compare primitive vs reference type
86              if (myTypeNode.getClass() != otherTypeNode.getClass()) {
87                  return false;
88              }
89  
90              // simple comparison of type images
91              // this can be fooled by one method using "String"
92              // and the other method using "java.lang.String"
93              // once we get real types in here that should get fixed
94              String myTypeImg;
95              String otherTypeImg;
96              if (myTypeNode instanceof ASTPrimitiveType) {
97                  myTypeImg = myTypeNode.getImage();
98                  otherTypeImg = otherTypeNode.getImage();
99              } else {
100                 myTypeImg = ((SimpleNode) (myTypeNode.jjtGetChild(0))).getImage();
101                 otherTypeImg = ((SimpleNode) (otherTypeNode.jjtGetChild(0))).getImage();
102             }
103 
104             if (!myTypeImg.equals(otherTypeImg)) {
105                 return false;
106             }
107 
108             // if type is ASTPrimitiveType and is an array, make sure the other one is also
109         }
110         return true;
111     }
112 
113     public int hashCode() {
114         return node.getImage().hashCode() + ((ASTMethodDeclarator) node).getParameterCount();
115     }
116 
117     public String toString() {
118         return "Method " + node.getImage() + ", line " + node.getBeginLine() + ", params = " + ((ASTMethodDeclarator) node).getParameterCount();
119     }
120 }