001: /**
002: * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
003: */package net.sourceforge.pmd.symboltable;
004:
005: import net.sourceforge.pmd.ast.ASTFormalParameter;
006: import net.sourceforge.pmd.ast.ASTFormalParameters;
007: import net.sourceforge.pmd.ast.ASTMethodDeclarator;
008: import net.sourceforge.pmd.ast.ASTPrimitiveType;
009: import net.sourceforge.pmd.ast.SimpleNode;
010:
011: public class MethodNameDeclaration extends AbstractNameDeclaration {
012:
013: public MethodNameDeclaration(ASTMethodDeclarator node) {
014: super (node);
015: }
016:
017: public int getParameterCount() {
018: return ((ASTMethodDeclarator) node).getParameterCount();
019: }
020:
021: public ASTMethodDeclarator getMethodNameDeclaratorNode() {
022: return (ASTMethodDeclarator) node;
023: }
024:
025: public String getParameterDisplaySignature() {
026: StringBuffer sb = new StringBuffer("(");
027: ASTFormalParameters params = (ASTFormalParameters) node
028: .jjtGetChild(0);
029: // TODO - this can be optimized - add [0] then ,[n] in a loop.
030: // no need to trim at the end
031: for (int i = 0; i < ((ASTMethodDeclarator) node)
032: .getParameterCount(); i++) {
033: ASTFormalParameter p = (ASTFormalParameter) params
034: .jjtGetChild(i);
035: sb.append(p.getTypeNode().getTypeImage());
036: sb.append(',');
037: }
038: if (sb.charAt(sb.length() - 1) == ',') {
039: sb.deleteCharAt(sb.length() - 1);
040: }
041: sb.append(')');
042: return sb.toString();
043: }
044:
045: public boolean equals(Object o) {
046: MethodNameDeclaration other = (MethodNameDeclaration) o;
047:
048: // compare name
049: if (!other.node.getImage().equals(node.getImage())) {
050: return false;
051: }
052:
053: // compare parameter count - this catches the case where there are no params, too
054: if (((ASTMethodDeclarator) (other.node)).getParameterCount() != ((ASTMethodDeclarator) node)
055: .getParameterCount()) {
056: return false;
057: }
058:
059: // compare parameter types
060: ASTFormalParameters myParams = (ASTFormalParameters) node
061: .jjtGetChild(0);
062: ASTFormalParameters otherParams = (ASTFormalParameters) other.node
063: .jjtGetChild(0);
064: for (int i = 0; i < ((ASTMethodDeclarator) node)
065: .getParameterCount(); i++) {
066: ASTFormalParameter myParam = (ASTFormalParameter) myParams
067: .jjtGetChild(i);
068: ASTFormalParameter otherParam = (ASTFormalParameter) otherParams
069: .jjtGetChild(i);
070:
071: SimpleNode myTypeNode = (SimpleNode) myParam.getTypeNode()
072: .jjtGetChild(0);
073: SimpleNode otherTypeNode = (SimpleNode) otherParam
074: .getTypeNode().jjtGetChild(0);
075:
076: // compare primitive vs reference type
077: if (myTypeNode.getClass() != otherTypeNode.getClass()) {
078: return false;
079: }
080:
081: // simple comparison of type images
082: // this can be fooled by one method using "String"
083: // and the other method using "java.lang.String"
084: // once we get real types in here that should get fixed
085: String myTypeImg;
086: String otherTypeImg;
087: if (myTypeNode instanceof ASTPrimitiveType) {
088: myTypeImg = myTypeNode.getImage();
089: otherTypeImg = otherTypeNode.getImage();
090: } else {
091: myTypeImg = ((SimpleNode) (myTypeNode.jjtGetChild(0)))
092: .getImage();
093: otherTypeImg = ((SimpleNode) (otherTypeNode
094: .jjtGetChild(0))).getImage();
095: }
096:
097: if (!myTypeImg.equals(otherTypeImg)) {
098: return false;
099: }
100:
101: // if type is ASTPrimitiveType and is an array, make sure the other one is also
102: }
103: return true;
104: }
105:
106: public int hashCode() {
107: return node.getImage().hashCode()
108: + ((ASTMethodDeclarator) node).getParameterCount();
109: }
110:
111: public String toString() {
112: return "Method " + node.getImage() + ", line "
113: + node.getBeginLine() + ", params = "
114: + ((ASTMethodDeclarator) node).getParameterCount();
115: }
116: }
|