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 implements NameDeclaration {
13  
14      public MethodNameDeclaration(ASTMethodDeclarator node) {
15          super(node);
16      }
17  
18      public boolean equals(Object o) {
19          MethodNameDeclaration otherMethodDecl = (MethodNameDeclaration) o;
20  
21          // compare method name
22          if (!otherMethodDecl.node.getImage().equals(node.getImage())) {
23              return false;
24          }
25  
26          // compare parameter count - this catches the case where there are no params, too
27          if (((ASTMethodDeclarator) (otherMethodDecl.node)).getParameterCount() != ((ASTMethodDeclarator) node).getParameterCount()) {
28              return false;
29          }
30  
31          // compare parameter types
32          ASTFormalParameters myParams = (ASTFormalParameters) node.jjtGetChild(0);
33          ASTFormalParameters otherParams = (ASTFormalParameters) otherMethodDecl.node.jjtGetChild(0);
34          for (int i = 0; i < ((ASTMethodDeclarator) node).getParameterCount(); i++) {
35              ASTFormalParameter myParam = (ASTFormalParameter) myParams.jjtGetChild(i);
36              ASTFormalParameter otherParam = (ASTFormalParameter) otherParams.jjtGetChild(i);
37  
38  
39              SimpleNode myTypeNode = (SimpleNode) myParam.jjtGetChild(0).jjtGetChild(0);
40              SimpleNode otherTypeNode = (SimpleNode) otherParam.jjtGetChild(0).jjtGetChild(0);
41  
42              // compare primitive vs reference type
43              if (myTypeNode.getClass() != otherTypeNode.getClass()) {
44                  return false;
45              }
46  
47              // simple comparison of type images
48              // this can be fooled by one method using "String"
49              // and the other method using "java.lang.String"
50              // once we get real types in here that should get fixed
51              String myTypeImg;
52              String otherTypeImg;
53              if (myTypeNode instanceof ASTPrimitiveType) {
54                  myTypeImg = myTypeNode.getImage();
55                  otherTypeImg = otherTypeNode.getImage();
56              } else {
57                  myTypeImg = ((SimpleNode)(myTypeNode.jjtGetChild(0))).getImage();
58                  otherTypeImg = ((SimpleNode)(otherTypeNode.jjtGetChild(0))).getImage();
59              }
60  
61              if (!myTypeImg.equals(otherTypeImg)) {
62                  return false;
63              }
64  
65              // if type is ASTPrimitiveType and is an array, make sure the other one is also
66          }
67          return true;
68      }
69  
70      public int hashCode() {
71          return node.getImage().hashCode() + ((ASTMethodDeclarator) node).getParameterCount();
72      }
73  
74      public String toString() {
75          return "Method " + node.getImage() + ":" + node.getBeginLine();
76      }
77  }