001 /*
002 $Id: MethodNode.java,v 1.17 2005/07/16 20:00:26 phk Exp $
003
004 Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved.
005
006 Redistribution and use of this software and associated documentation
007 ("Software"), with or without modification, are permitted provided
008 that the following conditions are met:
009
010 1. Redistributions of source code must retain copyright
011 statements and notices. Redistributions must also contain a
012 copy of this document.
013
014 2. Redistributions in binary form must reproduce the
015 above copyright notice, this list of conditions and the
016 following disclaimer in the documentation and/or other
017 materials provided with the distribution.
018
019 3. The name "groovy" must not be used to endorse or promote
020 products derived from this Software without prior written
021 permission of The Codehaus. For written permission,
022 please contact info@codehaus.org.
023
024 4. Products derived from this Software may not be called "groovy"
025 nor may "groovy" appear in their names without prior written
026 permission of The Codehaus. "groovy" is a registered
027 trademark of The Codehaus.
028
029 5. Due credit should be given to The Codehaus -
030 http://groovy.codehaus.org/
031
032 THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS
033 ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
034 NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
035 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
036 THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
037 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
038 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
039 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
040 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
041 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
042 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
043 OF THE POSSIBILITY OF SUCH DAMAGE.
044
045 */
046 package org.codehaus.groovy.ast;
047
048 import org.codehaus.groovy.ast.stmt.Statement;
049 import org.codehaus.groovy.classgen.VariableScopeCodeVisitor;
050 import org.objectweb.asm.Opcodes;
051
052 /**
053 * Represents a method declaration
054 *
055 * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
056 * @version $Revision: 1.17 $
057 */
058 public class MethodNode extends AnnotatedNode implements Opcodes {
059
060 private String name;
061 private int modifiers;
062 private String returnType;
063 private Parameter[] parameters;
064 private boolean hasDefaultValue = false;
065 private Statement code;
066 private boolean dynamicReturnType;
067 private VariableScope variableScope;
068
069 public MethodNode(String name, int modifiers, String returnType, Parameter[] parameters, Statement code) {
070 this.name = name;
071 this.modifiers = modifiers;
072 this.parameters = parameters;
073 this.code = code;
074
075 if (parameters != null && parameters.length > 0) {
076 for (int i = 0; i < parameters.length; i++) {
077 if (parameters[i].hasDefaultValue()) {
078 this.hasDefaultValue = true;
079 break;
080 }
081 }
082 }
083
084 if (returnType == null || returnType.length() == 0) {
085 this.returnType = "java.lang.Object";
086 this.dynamicReturnType = true;
087 }
088 else {
089 this.returnType = ensureJavaTypeNameSyntax(returnType);
090 }
091 }
092
093 /**
094 * The type descriptor for a method node is a string containing the name of the method, its return type,
095 * and its parameter types in a canonical form. For simplicity, I'm using the format of a Java declaration
096 * without parameter names, and with $dynamic as the type for any dynamically typed values.
097 *
098 * @return
099 */
100 // TODO: add test case for type descriptor
101 public String getTypeDescriptor() {
102 StringBuffer buf = new StringBuffer();
103 // buf.append(dynamicReturnType ? "$dynamic" : cleanupTypeName(returnType));
104 //
105 buf.append(ensureJavaTypeNameSyntax(returnType)); // br to replace the above. Dynamic type returns Object.
106 //
107 buf.append(' ');
108 buf.append(name);
109 buf.append('(');
110 for (int i = 0; i < parameters.length; i++) {
111 if (i > 0) {
112 buf.append(',');
113 }
114 Parameter param = parameters[i];
115 buf.append(ensureJavaTypeNameSyntax(param.getType()));
116 }
117 buf.append(')');
118 return buf.toString();
119 }
120
121 public static String ensureJavaTypeNameSyntax(String typename) {
122 // if the typename begins with "[", ends with ";", or is
123 // one character long, it's in .class syntax.
124 if (typename.charAt(0) == '[') {
125 return ensureJavaTypeNameSyntax(typename.substring(1)) + "[]";
126 }
127 if (typename.length() == 1) {
128 switch (typename.charAt(0)) {
129 case 'B':
130 return "byte";
131 case 'C':
132 return "char";
133 case 'D':
134 return "double";
135 case 'F':
136 return "float";
137 case 'J':
138 return "long";
139 case 'I':
140 return "int";
141 case 'S':
142 return "short";
143 case 'V':
144 return "void";
145 case 'Z':
146 return "boolean";
147 }
148 }
149 if (typename.endsWith(";")) {
150 // Type should be "Lclassname;"
151 return typename.substring(1, typename.length() - 1);
152 }
153 return typename;
154
155 }
156
157 public boolean isVoidMethod() {
158 return "void".equals(returnType);
159 }
160
161 public Statement getCode() {
162 return code;
163 }
164
165 public void setCode(Statement code) {
166 this.code = code;
167 }
168
169 public int getModifiers() {
170 return modifiers;
171 }
172
173 public void setModifiers(int modifiers) {
174 this.modifiers = modifiers;
175 }
176
177 public String getName() {
178 return name;
179 }
180
181 public Parameter[] getParameters() {
182 return parameters;
183 }
184
185 public String getReturnType() {
186 return returnType;
187 }
188
189 public VariableScope getVariableScope() {
190 if (variableScope == null) {
191 variableScope = createVariableScope();
192 }
193 return variableScope;
194 }
195
196 public void setVariableScope(VariableScope variableScope) {
197 this.variableScope = variableScope;
198 }
199
200 public boolean isDynamicReturnType() {
201 return dynamicReturnType;
202 }
203
204 public boolean isAbstract() {
205 return (modifiers & ACC_ABSTRACT) != 0;
206 }
207
208 public boolean isStatic() {
209 return (modifiers & ACC_STATIC) != 0;
210 }
211
212 public boolean hasDefaultValue() {
213 return this.hasDefaultValue;
214 }
215
216 public String toString() {
217 return super.toString() + "[name: " + name + "]";
218 }
219
220 public void setReturnType(String returnType) {
221 this.returnType = returnType;
222 }
223
224
225 protected VariableScope createVariableScope() {
226 VariableScope variableScope = new VariableScope();
227 VariableScopeCodeVisitor visitor = new VariableScopeCodeVisitor(variableScope);
228 visitor.setParameters(getParameters());
229 Statement code = getCode();
230 if (code != null) {
231 code.visit(visitor);
232 }
233 addFieldsToVisitor(variableScope);
234 return variableScope;
235 }
236 }