KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > pmd > symboltable > MethodNameDeclaration


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