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