1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 package org.codehaus.groovy.ast;
47
48 import java.util.List;
49
50 import org.codehaus.groovy.ast.stmt.BlockStatement;
51 import org.codehaus.groovy.ast.stmt.Statement;
52 import org.objectweb.asm.Opcodes;
53
54 /***
55 * Represents a method declaration
56 *
57 * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
58 * @version $Revision: 4032 $
59 */
60 public class MethodNode extends AnnotatedNode implements Opcodes {
61
62 private String name;
63 private int modifiers;
64 private ClassNode returnType;
65 private Parameter[] parameters;
66 private boolean hasDefaultValue = false;
67 private Statement code;
68 private boolean dynamicReturnType;
69 private VariableScope variableScope;
70 private ClassNode[] exceptions;
71
72 public MethodNode(String name, int modifiers, ClassNode returnType, Parameter[] parameters, ClassNode[] exceptions, Statement code) {
73 this.name = name;
74 this.modifiers = modifiers;
75 this.parameters = parameters;
76 this.code = code;
77 this.returnType = returnType;
78 if (returnType==null) this.returnType = ClassHelper.OBJECT_TYPE;
79
80 variableScope = new VariableScope();
81 if (parameters != null && parameters.length > 0) {
82 for (int i = 0; i < parameters.length; i++) {
83 Parameter para = parameters[i];
84 if (para.hasInitialExpression()) {
85 this.hasDefaultValue = true;
86 }
87 para.setInStaticContext(isStatic());
88 variableScope.getDeclaredVariables().put(para.getName(),para);
89 }
90 }
91 variableScope.setInStaticContext(isStatic());
92
93 this.exceptions = exceptions;
94 }
95
96 /***
97 * The type descriptor for a method node is a string containing the name of the method, its return type,
98 * and its parameter types in a canonical form. For simplicity, I'm using the format of a Java declaration
99 * without parameter names, and with $dynamic as the type for any dynamically typed values.
100 */
101
102 public String getTypeDescriptor() {
103 StringBuffer buf = new StringBuffer();
104
105
106 buf.append(returnType.getName());
107
108 buf.append(' ');
109 buf.append(name);
110 buf.append('(');
111 for (int i = 0; i < parameters.length; i++) {
112 if (i > 0) {
113 buf.append(',');
114 }
115 Parameter param = parameters[i];
116 buf.append(param.getType().getName());
117 }
118 buf.append(')');
119 return buf.toString();
120 }
121
122 public boolean isVoidMethod() {
123 return returnType==ClassHelper.VOID_TYPE;
124 }
125
126 public Statement getCode() {
127 return code;
128 }
129
130 public void setCode(Statement code) {
131 this.code = code;
132 }
133
134 public int getModifiers() {
135 return modifiers;
136 }
137
138 public void setModifiers(int modifiers) {
139 this.modifiers = modifiers;
140 }
141
142 public String getName() {
143 return name;
144 }
145
146 public Parameter[] getParameters() {
147 return parameters;
148 }
149
150 public ClassNode getReturnType() {
151 return returnType;
152 }
153
154 public VariableScope getVariableScope() {
155 return variableScope;
156 }
157
158 public void setVariableScope(VariableScope variableScope) {
159 this.variableScope = variableScope;
160 }
161
162 public boolean isDynamicReturnType() {
163 return dynamicReturnType;
164 }
165
166 public boolean isAbstract() {
167 return (modifiers & ACC_ABSTRACT) != 0;
168 }
169
170 public boolean isStatic() {
171 return (modifiers & ACC_STATIC) != 0;
172 }
173
174 public boolean hasDefaultValue() {
175 return this.hasDefaultValue;
176 }
177
178 public String toString() {
179 return super.toString() + "[name: " + name + "]";
180 }
181
182 public void setReturnType(ClassNode returnType) {
183 this.returnType = returnType;
184 }
185
186 public ClassNode[] getExceptions() {
187 return exceptions;
188 }
189
190 public Statement getFirstStatement(){
191 if (code == null) return null;
192 if (code instanceof BlockStatement) {
193 List list = ((BlockStatement) code).getStatements();
194 if (list.size()>0) return (Statement) list.get(0);
195 return null;
196 }
197 return code;
198 }
199 }