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 org.codehaus.groovy.ast.stmt.Statement;
49 import org.objectweb.asm.Constants;
50
51 /***
52 * Represents a method declaration
53 *
54 * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
55 * @version $Revision: 1.13 $
56 */
57 public class MethodNode extends MetadataNode implements Constants {
58
59 private String name;
60 private int modifiers;
61 private String returnType;
62 private Parameter[] parameters;
63 private Statement code;
64 private boolean dynamicReturnType;
65 private VariableScope variableScope;
66 ClassNode declaringClass;
67
68 public MethodNode(String name, int modifiers, String returnType, Parameter[] parameters, Statement code) {
69 this.name = name;
70 this.modifiers = modifiers;
71 this.parameters = parameters;
72 this.code = code;
73
74 if (returnType == null || returnType.length() == 0) {
75 this.returnType = "java.lang.Object";
76 this.dynamicReturnType = true;
77 }
78 else {
79 this.returnType = ensureJavaTypeNameSyntax(returnType);
80 }
81 }
82
83 /***
84 * The type descriptor for a method node is a string containing the name of the method, its return type,
85 * and its parameter types in a canonical form. For simplicity, I'm using the format of a Java declaration
86 * without parameter names, and with $dynamic as the type for any dynamically typed values.
87 * @return
88 */
89
90 public String getTypeDescriptor() {
91 StringBuffer buf = new StringBuffer();
92
93
94 buf.append(ensureJavaTypeNameSyntax(returnType));
95
96 buf.append(' ');
97 buf.append(name);
98 buf.append('(');
99 for (int i=0; i < parameters.length; i++) {
100 if (i>0) buf.append(',');
101 Parameter param = parameters[i];
102 buf.append(ensureJavaTypeNameSyntax(param.getType()));
103 }
104 buf.append(')');
105 return buf.toString();
106 }
107
108 public static String ensureJavaTypeNameSyntax(String typename) {
109
110
111 if (typename.charAt(0) == '[') {
112 return ensureJavaTypeNameSyntax(typename.substring(1)) + "[]";
113 }
114 if (typename.length() == 1) {
115 switch (typename.charAt(0)) {
116 case 'B': return "byte";
117 case 'C': return "char";
118 case 'D': return "double";
119 case 'F': return "float";
120 case 'J': return "long";
121 case 'I': return "int";
122 case 'S': return "short";
123 case 'V': return "void";
124 case 'Z': return "boolean";
125 }
126 }
127 if (typename.endsWith(";")) {
128
129 return typename.substring(1, typename.length() - 1);
130 }
131 return typename;
132
133 }
134
135 public boolean isVoidMethod() {
136 return "void".equals(returnType);
137 }
138
139 public Statement getCode() {
140 return code;
141 }
142
143 public void setCode(Statement code) {
144 this.code = code;
145 }
146
147 public int getModifiers() {
148 return modifiers;
149 }
150
151 public void setModifiers(int modifiers) {
152 this.modifiers = modifiers;
153 }
154
155 public String getName() {
156 return name;
157 }
158
159 public Parameter[] getParameters() {
160 return parameters;
161 }
162
163 public String getReturnType() {
164 return returnType;
165 }
166
167 public VariableScope getVariableScope() {
168 return variableScope;
169 }
170
171 public void setVariableScope(VariableScope variableScope) {
172 this.variableScope = variableScope;
173 }
174
175 public boolean isDynamicReturnType() {
176 return dynamicReturnType;
177 }
178
179 public ClassNode getDeclaringClass() {
180 return declaringClass;
181 }
182
183 public boolean isAbstract() {
184 return (modifiers & ACC_ABSTRACT) != 0;
185 }
186
187 public boolean isStatic() {
188 return (modifiers & ACC_STATIC) != 0;
189 }
190
191 public String toString() {
192 return super.toString() + "[name: " + name + "]";
193 }
194
195 public void setReturnType(String returnType) {
196 this.returnType = returnType;
197 }
198 /***
199 * @param declaringClass The declaringClass to set.
200 */
201 public void setDeclaringClass(ClassNode declaringClass) {
202 this.declaringClass = declaringClass;
203 }
204 }