View Javadoc
1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.lang.plsql.symboltable;
5   
6   import java.util.logging.Level;
7   import java.util.logging.Logger;
8   
9   import net.sourceforge.pmd.lang.ast.Node;
10  import net.sourceforge.pmd.lang.plsql.ast.ASTFormalParameter;
11  import net.sourceforge.pmd.lang.plsql.ast.ASTFormalParameters;
12  import net.sourceforge.pmd.lang.plsql.ast.ASTMethodDeclarator;
13  import net.sourceforge.pmd.lang.plsql.ast.ASTTriggerTimingPointSection;
14  import net.sourceforge.pmd.lang.plsql.ast.AbstractPLSQLNode;
15  import net.sourceforge.pmd.lang.symboltable.AbstractNameDeclaration;
16  
17  public class MethodNameDeclaration extends AbstractNameDeclaration {
18      private final static Logger LOGGER = Logger.getLogger(MethodNameDeclaration.class.getName());
19  
20      public MethodNameDeclaration(ASTMethodDeclarator node) {
21          super(node);
22      }
23  
24      /**
25       * Treat a TimingPointSection within a Compound Trigger like a packaged
26       * FUNCTION or PROCEDURE. SRT
27       * 
28       * @param node
29       */
30      public MethodNameDeclaration(ASTTriggerTimingPointSection node) {
31          super(node);
32      }
33  
34      public int getParameterCount() {
35          return ((ASTMethodDeclarator) node).getParameterCount();
36      }
37  
38      /**
39       * PL/SQL does not currently allow varargs outside the STANDARD package.
40       * 
41       * @return false
42       */
43      public boolean isVarargs() {
44          return false;
45      }
46  
47      public ASTMethodDeclarator getMethodNameDeclaratorNode() {
48          return (ASTMethodDeclarator) node;
49      }
50  
51      public String getParameterDisplaySignature() {
52          StringBuilder sb = new StringBuilder("(");
53          ASTFormalParameters params = (ASTFormalParameters) node.jjtGetChild(0);
54          // TODO - this can be optimized - add [0] then ,[n] in a loop.
55          // no need to trim at the end
56          for (int i = 0; i < ((ASTMethodDeclarator) node).getParameterCount(); i++) {
57              ASTFormalParameter p = (ASTFormalParameter) params.jjtGetChild(i);
58              sb.append(p.getTypeNode().getTypeImage());
59              // if (p.isVarargs()) {
60              // sb.append("...");
61              // }
62              sb.append(',');
63          }
64          if (sb.charAt(sb.length() - 1) == ',') {
65              sb.deleteCharAt(sb.length() - 1);
66          }
67          sb.append(')');
68          return sb.toString();
69      }
70  
71      @Override
72      public boolean equals(Object o) {
73          if (!(o instanceof MethodNameDeclaration)) {
74              return false;
75          }
76  
77          MethodNameDeclaration other = (MethodNameDeclaration) o;
78  
79          // compare name
80          if (!other.node.getImage().equals(node.getImage())) {
81              return false;
82          }
83  
84          // compare parameter count - this catches the case where there are no
85          // params, too
86          if (((ASTMethodDeclarator) other.node).getParameterCount() != ((ASTMethodDeclarator) node).getParameterCount()) {
87              return false;
88          }
89  
90          // compare parameter types
91          // SRT ASTFormalParameters myParams = (ASTFormalParameters)
92          // node.jjtGetChild(0);
93          // SRT ASTFormalParameters otherParams = (ASTFormalParameters)
94          // other.node.jjtGetChild(0);
95          ASTFormalParameters myParams = node.getFirstDescendantOfType(ASTFormalParameters.class);
96          ASTFormalParameters otherParams = other.node.getFirstDescendantOfType(ASTFormalParameters.class);
97          for (int i = 0; i < ((ASTMethodDeclarator) node).getParameterCount(); i++) {
98              ASTFormalParameter myParam = (ASTFormalParameter) myParams.jjtGetChild(i);
99              ASTFormalParameter otherParam = (ASTFormalParameter) otherParams.jjtGetChild(i);
100 
101             // Compare vararg
102             // if (myParam.isVarargs() != otherParam.isVarargs()) {
103             // return false;
104             // }
105 
106             Node myTypeNode = myParam.getTypeNode().jjtGetChild(0);
107             Node otherTypeNode = otherParam.getTypeNode().jjtGetChild(0);
108 
109             // compare primitive vs reference type
110             if (myTypeNode.getClass() != otherTypeNode.getClass()) {
111                 return false;
112             }
113 
114             // simple comparison of type images
115             // this can be fooled by one method using "String"
116             // and the other method using "java.lang.String"
117             // once we get real types in here that should get fixed
118             String myTypeImg;
119             String otherTypeImg;
120             // if (myTypeNode instanceof ASTPrimitiveType) {
121             // myTypeImg = myTypeNode.getImage();
122             // otherTypeImg = otherTypeNode.getImage();
123             // } else {
124             myTypeImg = ((AbstractPLSQLNode) myTypeNode.jjtGetChild(0)).getImage();
125             otherTypeImg = ((AbstractPLSQLNode) otherTypeNode.jjtGetChild(0)).getImage();
126             // }
127 
128             if (!myTypeImg.equals(otherTypeImg)) {
129                 return false;
130             }
131 
132             // if type is ASTPrimitiveType and is an array, make sure the other
133             // one is also
134         }
135         return true;
136     }
137 
138     @Override
139     public int hashCode() {
140         try {
141             return node.hashCode(); // SRT node.getImage().hashCode() +
142                                     // ((ASTMethodDeclarator)
143                                     // node).getParameterCount();
144         } catch (Exception e) {
145             if (LOGGER.isLoggable(Level.FINEST)) {
146                 LOGGER.finest("MethodNameDeclaration problem for " + node + " of class "
147                         + node.getClass().getCanonicalName() + " => " + node.getBeginLine() + "/"
148                         + node.getBeginColumn());
149             }
150             // @TODO SRT restore the thrown exception - throw e;
151             return 0;
152         }
153     }
154 
155     @Override
156     public String toString() {
157         // SRT return "Method " + node.getImage() + ", line " +
158         // node.getBeginLine() + ", params = " + ((ASTMethodDeclarator)
159         // node).getParameterCount();
160         return node.toString();
161     }
162 }