1 /*
2 $Id: ReflectorGenerator.java,v 1.9 2005/05/27 10:13:09 russel Exp $
3
4 Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved.
5
6 Redistribution and use of this software and associated documentation
7 ("Software"), with or without modification, are permitted provided
8 that the following conditions are met:
9
10 1. Redistributions of source code must retain copyright
11 statements and notices. Redistributions must also contain a
12 copy of this document.
13
14 2. Redistributions in binary form must reproduce the
15 above copyright notice, this list of conditions and the
16 following disclaimer in the documentation and/or other
17 materials provided with the distribution.
18
19 3. The name "groovy" must not be used to endorse or promote
20 products derived from this Software without prior written
21 permission of The Codehaus. For written permission,
22 please contact info@codehaus.org.
23
24 4. Products derived from this Software may not be called "groovy"
25 nor may "groovy" appear in their names without prior written
26 permission of The Codehaus. "groovy" is a registered
27 trademark of The Codehaus.
28
29 5. Due credit should be given to The Codehaus -
30 http://groovy.codehaus.org/
31
32 THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS
33 ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
34 NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
35 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
36 THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
37 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
39 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
42 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
43 OF THE POSSIBILITY OF SUCH DAMAGE.
44
45 */
46 package org.codehaus.groovy.classgen;
47
48 import groovy.lang.MetaMethod;
49
50 import java.util.List;
51
52 import org.objectweb.asm.ClassVisitor;
53 import org.objectweb.asm.MethodVisitor;
54 import org.objectweb.asm.Opcodes;
55 import org.objectweb.asm.Label;
56
57 /***
58 * Code generates a Reflector
59 *
60 * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
61 * @version $Revision: 1.9 $
62 */
63 public class ReflectorGenerator implements Opcodes {
64
65 private List methods;
66 private ClassVisitor cw;
67 private MethodVisitor cv;
68 private BytecodeHelper helper = new BytecodeHelper(null);
69 private String classInternalName;
70
71 public ReflectorGenerator(List methods) {
72 this.methods = methods;
73 }
74
75 public void generate(ClassVisitor cw, String className) {
76 this.cw = cw;
77
78 classInternalName = BytecodeHelper.getClassInternalName(className);
79 cw.visit(ClassGenerator.asmJDKVersion, ACC_PUBLIC + ACC_SUPER, classInternalName, (String)null, "org/codehaus/groovy/runtime/Reflector", null);
80
81 cv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
82 cv.visitVarInsn(ALOAD, 0);
83 cv.visitMethodInsn(INVOKESPECIAL, "org/codehaus/groovy/runtime/Reflector", "<init>", "()V");
84 cv.visitInsn(RETURN);
85 cv.visitMaxs(1, 1);
86
87 generateInvokeMethod();
88
89 cw.visitEnd();
90 }
91
92 protected void generateInvokeMethod() {
93 int methodCount = methods.size();
94
95 cv =
96 cw.visitMethod(
97 ACC_PUBLIC,
98 "invoke",
99 "(Lgroovy/lang/MetaMethod;Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;",
100 null,
101 null);
102 helper = new BytecodeHelper(cv);
103
104 cv.visitVarInsn(ALOAD, 1);
105 cv.visitMethodInsn(INVOKEVIRTUAL, "groovy/lang/MetaMethod", "getMethodIndex", "()I");
106 Label defaultLabel = new Label();
107 Label[] labels = new Label[methodCount];
108 int[] indices = new int[methodCount];
109 for (int i = 0; i < methodCount; i++) {
110 labels[i] = new Label();
111
112 MetaMethod method = (MetaMethod) methods.get(i);
113 method.setMethodIndex(i + 1);
114 indices[i] = method.getMethodIndex();
115
116 //System.out.println("Index: " + method.getMethodIndex() + " for: " + method);
117 }
118
119 cv.visitLookupSwitchInsn(defaultLabel, indices, labels);
120 //cv.visitTableSwitchInsn(minMethodIndex, maxMethodIndex, defaultLabel, labels);
121
122 for (int i = 0; i < methodCount; i++) {
123 cv.visitLabel(labels[i]);
124
125 MetaMethod method = (MetaMethod) methods.get(i);
126 invokeMethod(method);
127 if (method.getReturnType() == void.class) {
128 cv.visitInsn(ACONST_NULL);
129 }
130 cv.visitInsn(ARETURN);
131 }
132
133 cv.visitLabel(defaultLabel);
134 cv.visitVarInsn(ALOAD, 0);
135 cv.visitVarInsn(ALOAD, 1);
136 cv.visitVarInsn(ALOAD, 2);
137 cv.visitVarInsn(ALOAD, 3);
138 cv.visitMethodInsn(
139 INVOKEVIRTUAL,
140 classInternalName,
141 "noSuchMethod",
142 "(Lgroovy/lang/MetaMethod;Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;");
143 cv.visitInsn(ARETURN);
144 cv.visitMaxs(4, 4);
145 }
146
147 protected void invokeMethod(MetaMethod method) {
148 /*** simple
149 cv.visitVarInsn(ALOAD, 2);
150 cv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "toString", "()Ljava/lang/String;");
151 */
152 Class ownerClass = method.getInterfaceClass();
153 boolean useInterface = false;
154 if (ownerClass == null) {
155 ownerClass = method.getDeclaringClass();
156 }
157 else {
158 useInterface = true;
159 }
160 String type = BytecodeHelper.getClassInternalName(ownerClass.getName());
161 String descriptor = BytecodeHelper.getMethodDescriptor(method.getReturnType(), method.getParameterTypes());
162
163 // System.out.println("Method: " + method);
164 // System.out.println("Descriptor: " + descriptor);
165
166 if (method.isStatic()) {
167 loadParameters(method, 3);
168 cv.visitMethodInsn(INVOKESTATIC, type, method.getName(), descriptor);
169 }
170 else {
171 cv.visitVarInsn(ALOAD, 2);
172 helper.doCast(ownerClass);
173 loadParameters(method, 3);
174 cv.visitMethodInsn((useInterface) ? INVOKEINTERFACE : INVOKEVIRTUAL, type, method.getName(), descriptor);
175 }
176
177 helper.box(method.getReturnType());
178 }
179
180 /*
181 protected void generateInvokeSuperMethod() {
182 List superMethods = new ArrayList(methods);
183 for (Iterator iter = methods.iterator(); iter.hasNext();) {
184 MetaMethod method = (MetaMethod) iter.next();
185 if (!validSuperMethod(method)) {
186 superMethods.remove(method);
187 }
188 }
189 int methodCount = superMethods.size();
190 if (methodCount == 0) {
191 return;
192 }
193 cv =
194 cw.visitMethod(
195 ACC_PUBLIC,
196 "invokeSuper",
197 "(Lgroovy/lang/MetaMethod;Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;",
198 null,
199 null);
200 helper = new BytecodeHelper(cv);
201
202 cv.visitVarInsn(ALOAD, 1);
203 cv.visitMethodInsn(INVOKEVIRTUAL, "groovy/lang/MetaMethod", "getMethodIndex", "()I");
204 Label defaultLabel = new Label();
205 Label[] labels = new Label[methodCount];
206 int[] indices = new int[methodCount];
207 for (int i = 0; i < methodCount; i++) {
208 labels[i] = new Label();
209
210 MetaMethod method = (MetaMethod) superMethods.get(i);
211 method.setMethodIndex(i + 1);
212 indices[i] = method.getMethodIndex();
213
214 //System.out.println("Index: " + method.getMethodIndex() + " for: " + method);
215 }
216
217 cv.visitLookupSwitchInsn(defaultLabel, indices, labels);
218 //cv.visitTableSwitchInsn(minMethodIndex, maxMethodIndex, defaultLabel, labels);
219
220 for (int i = 0; i < methodCount; i++) {
221 MetaMethod method = (MetaMethod) superMethods.get(i);
222 cv.visitLabel(labels[i]);
223
224 invokeSuperMethod(method);
225 if (method.getReturnType() == void.class) {
226 cv.visitInsn(ACONST_NULL);
227 }
228 cv.visitInsn(ARETURN);
229 }
230
231 cv.visitLabel(defaultLabel);
232 cv.visitVarInsn(ALOAD, 0);
233 cv.visitVarInsn(ALOAD, 1);
234 cv.visitVarInsn(ALOAD, 2);
235 cv.visitVarInsn(ALOAD, 3);
236 cv.visitMethodInsn(
237 INVOKEVIRTUAL,
238 classInternalName,
239 "noSuchMethod",
240 "(Lgroovy/lang/MetaMethod;Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;");
241 cv.visitInsn(ARETURN);
242 cv.visitMaxs(4, 4);
243 }
244
245 protected boolean validSuperMethod(MetaMethod method) {
246 return !method.isStatic() && (method.getModifiers() & (Modifier.FINAL | Modifier.ABSTRACT)) == 0 && theClass == method.getDeclaringClass();
247 }
248
249 protected void invokeSuperMethod(MetaMethod method) {
250 Class ownerClass = method.getDeclaringClass();
251 String type = helper.getClassInternalName(ownerClass.getName());
252 String descriptor = helper.getMethodDescriptor(method.getReturnType(), method.getParameterTypes());
253
254 // System.out.println("Method: " + method.getName());
255 // System.out.println("Descriptor: " + descriptor);
256
257 cv.visitVarInsn(ALOAD, 2);
258 //helper.doCast(ownerClass);
259 loadParameters(method, 3);
260 cv.visitMethodInsn(INVOKESPECIAL, type, method.getName(), descriptor);
261
262 helper.box(method.getReturnType());
263 }
264 */
265
266 protected void loadParameters(MetaMethod method, int argumentIndex) {
267 Class[] parameters = method.getParameterTypes();
268 int size = parameters.length;
269 for (int i = 0; i < size; i++) {
270 cv.visitVarInsn(ALOAD, argumentIndex);
271 helper.pushConstant(i);
272 cv.visitInsn(AALOAD);
273
274 // we should cast to something
275 Class type = parameters[i];
276 if (type.isPrimitive()) {
277 helper.unbox(type);
278 }
279 else {
280 helper.doCast(type.getName());
281 }
282 }
283 }
284 }