1 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 getParameterDisplaySignature() { 27 StringBuffer sb = new StringBuffer ("("); 28 ASTFormalParameters params = (ASTFormalParameters) node.jjtGetChild(0); 29 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 o) { 44 MethodNameDeclaration other = (MethodNameDeclaration) o; 45 46 if (!other.node.getImage().equals(node.getImage())) { 48 return false; 49 } 50 51 if (((ASTMethodDeclarator) (other.node)).getParameterCount() != ((ASTMethodDeclarator) node).getParameterCount()) { 53 return false; 54 } 55 56 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 if (myTypeNode.getClass() != otherTypeNode.getClass()) { 68 return false; 69 } 70 71 String myTypeImg; 76 String 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 } 91 return true; 92 } 93 94 public int hashCode() { 95 return node.getImage().hashCode() + ((ASTMethodDeclarator) node).getParameterCount(); 96 } 97 98 public String toString() { 99 return "Method " + node.getImage() + ", line " + node.getBeginLine() + ", params = " + ((ASTMethodDeclarator) node).getParameterCount(); 100 } 101 } 102 | Popular Tags |