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