KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > protoactive > core > body > future > FutureClassGenerator


1 /***
2  * Julia: France Telecom's implementation of the Fractal API
3  * Copyright (C) 2001-2002 France Telecom R&D
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: Eric.Bruneton@rd.francetelecom.com
20  *
21  * Author: Eric Bruneton
22  */

23
24 package protoactive.core.body.future;
25
26 import org.objectweb.fractal.julia.asm.AbstractClassGenerator;
27
28 import org.objectweb.asm.CodeVisitor;
29 import org.objectweb.asm.Type;
30
31 import java.lang.reflect.Method JavaDoc;
32
33 /**
34  * A class generator to generate LocalFuture classes.
35  * This class generator generates sub classes of the LocalFuture class that
36  * implement the given set of interfaces.
37  * <p>
38  * The code generated for the I interface below is the following:
39  * <pre>
40  * public interface I {
41  * void m (int i);
42  * Object n (String s);
43  * }
44  *
45  * public class <i>XYZ</i> extends LocalFuture implements I, Generated {
46  *
47  * public void m (int i) {
48  * waitReply();
49  * ((I)reply).m();
50  * }
51  *
52  * public Object n (String s) {
53  * waitReply();
54  * return ((I)reply).n(s);
55  * }
56  *
57  * public String getFcGeneratorParameters () {
58  * return "(... LocalFuture (I))";
59  * }
60  * }
61  * </pre>
62  */

63
64 public class FutureClassGenerator extends AbstractClassGenerator {
65
66   protected void generateMethod (final Method JavaDoc m) {
67     // generate the header of the forwarder method
68
String JavaDoc itf = m.getDeclaringClass().getName().replace('.', '/');
69     String JavaDoc mName = m.getName();
70     String JavaDoc mDesc = Type.getMethodDescriptor(m);
71     Class JavaDoc[] params = m.getParameterTypes();
72     Class JavaDoc result = m.getReturnType();
73     Class JavaDoc[] exceptions = m.getExceptionTypes();
74     String JavaDoc[] excepts = new String JavaDoc[exceptions.length];
75     for (int i = 0; i < exceptions.length; ++i) {
76       excepts[i] = exceptions[i].getName().replace('.', '/');
77     }
78     CodeVisitor mv = cw.visitMethod(ACC_PUBLIC, mName, mDesc, excepts, null);
79
80     // generate code to invoke 'waitReply'
81
mv.visitVarInsn(ALOAD, 0);
82     mv.visitMethodInsn(INVOKEVIRTUAL, name, "waitReply", "()V");
83
84     // generate code to load and cast the 'reply' field
85
mv.visitVarInsn(ALOAD, 0);
86     mv.visitFieldInsn(GETFIELD, name, "reply", "Ljava/lang/Object;");
87     mv.visitTypeInsn(CHECKCAST, itf);
88
89     // generate code to push method parameters
90
int offset = 1;
91     for (int i = 0; i < params.length; ++i) {
92       mv.visitVarInsn(ILOAD + getOpcodeOffset(params[i]), offset);
93       offset += getSize(params[i]);
94     }
95
96     // generate code to invoke method
97
mv.visitMethodInsn(INVOKEINTERFACE, itf, mName, mDesc);
98
99     // generate code to return result
100
if (result == Void.TYPE) {
101       mv.visitInsn(RETURN);
102     } else {
103       mv.visitInsn(IRETURN + getOpcodeOffset(result));
104     }
105
106     // end method
107
mv.visitMaxs(Math.max(offset, getSize(result)), offset);
108   }
109 }
110
Popular Tags