001 /*
002 $Id: MethodNode.java,v 1.20 2005/11/13 16:42:09 blackdrag 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.20 $
057 */
058 public class MethodNode extends AnnotatedNode implements Opcodes {
059
060 private String name;
061 private int modifiers;
062 private ClassNode 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, ClassNode returnType, Parameter[] parameters, Statement code) {
070 this.name = name;
071 this.modifiers = modifiers;
072 this.parameters = parameters;
073 this.code = code;
074 this.returnType = returnType;
075 if (returnType==null) this.returnType = ClassHelper.OBJECT_TYPE;
076
077 if (parameters != null && parameters.length > 0) {
078 for (int i = 0; i < parameters.length; i++) {
079 if (parameters[i].hasInitialExpression()) {
080 this.hasDefaultValue = true;
081 }
082 parameters[i].setInStaticContext(isStatic());
083 }
084 }
085 }
086
087 /**
088 * The type descriptor for a method node is a string containing the name of the method, its return type,
089 * and its parameter types in a canonical form. For simplicity, I'm using the format of a Java declaration
090 * without parameter names, and with $dynamic as the type for any dynamically typed values.
091 *
092 * @return
093 */
094 // TODO: add test case for type descriptor
095 public String getTypeDescriptor() {
096 StringBuffer buf = new StringBuffer();
097 // buf.append(dynamicReturnType ? "$dynamic" : cleanupTypeName(returnType));
098 //
099 buf.append(returnType.getName()); // br to replace the above. Dynamic type returns Object.
100 //
101 buf.append(' ');
102 buf.append(name);
103 buf.append('(');
104 for (int i = 0; i < parameters.length; i++) {
105 if (i > 0) {
106 buf.append(',');
107 }
108 Parameter param = parameters[i];
109 buf.append(param.getType().getName());
110 }
111 buf.append(')');
112 return buf.toString();
113 }
114
115 public boolean isVoidMethod() {
116 return returnType==ClassHelper.VOID_TYPE;
117 }
118
119 public Statement getCode() {
120 return code;
121 }
122
123 public void setCode(Statement code) {
124 this.code = code;
125 }
126
127 public int getModifiers() {
128 return modifiers;
129 }
130
131 public void setModifiers(int modifiers) {
132 this.modifiers = modifiers;
133 }
134
135 public String getName() {
136 return name;
137 }
138
139 public Parameter[] getParameters() {
140 return parameters;
141 }
142
143 public ClassNode getReturnType() {
144 return returnType;
145 }
146
147 public VariableScope getVariableScope() {
148 if (variableScope == null) {
149 variableScope = createVariableScope();
150 }
151 return variableScope;
152 }
153
154 public void setVariableScope(VariableScope variableScope) {
155 this.variableScope = variableScope;
156 }
157
158 public boolean isDynamicReturnType() {
159 return dynamicReturnType;
160 }
161
162 public boolean isAbstract() {
163 return (modifiers & ACC_ABSTRACT) != 0;
164 }
165
166 public boolean isStatic() {
167 return (modifiers & ACC_STATIC) != 0;
168 }
169
170 public boolean hasDefaultValue() {
171 return this.hasDefaultValue;
172 }
173
174 public String toString() {
175 return super.toString() + "[name: " + name + "]";
176 }
177
178 public void setReturnType(ClassNode returnType) {
179 this.returnType = returnType;
180 }
181
182
183 protected VariableScope createVariableScope() {
184 VariableScope variableScope = new VariableScope();
185 VariableScopeCodeVisitor visitor = new VariableScopeCodeVisitor(variableScope);
186 visitor.setParameters(getParameters());
187 Statement code = getCode();
188 if (code != null) {
189 code.visit(visitor);
190 }
191 addFieldsToVisitor(variableScope);
192 return variableScope;
193 }
194 }