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