01: // Copyright (c) Corporation for National Research Initiatives
02:
03: package org.python.compiler;
04:
05: import java.util.Hashtable;
06: import java.util.Enumeration;
07: import java.lang.reflect.Method;
08: import java.io.*;
09:
10: public class AdapterMaker extends ProxyMaker {
11: public AdapterMaker(Class interfac) {
12: super (interfac.getName() + "$Adapter", interfac);
13: }
14:
15: public void build() throws Exception {
16: names = new Hashtable();
17:
18: //Class superclass = org.python.core.PyAdapter.class;
19: int access = ClassFile.PUBLIC | ClassFile.SYNCHRONIZED;
20: classfile = new ClassFile(myClass, "java/lang/Object", access);
21:
22: classfile.addInterface(mapClass(interfaces[0]));
23:
24: addMethods(interfaces[0], new Hashtable());
25: addConstructors(Object.class);
26: doConstants();
27: }
28:
29: public static String makeAdapter(Class interfac,
30: OutputStream ostream) throws Exception {
31: AdapterMaker pm = new AdapterMaker(interfac);
32: pm.build();
33: pm.classfile.write(ostream);
34: return pm.myClass;
35: }
36:
37: public void doConstants() throws Exception {
38: for (Enumeration e = names.keys(); e.hasMoreElements();) {
39: String name = (String) e.nextElement();
40: classfile.addField(name, "Lorg/python/core/PyObject;",
41: ClassFile.PUBLIC);
42: }
43: }
44:
45: public void addMethod(Method method, int access) throws Exception {
46: Class[] parameters = method.getParameterTypes();
47: Class ret = method.getReturnType();
48: String sig = makeSignature(parameters, ret);
49:
50: String name = method.getName();
51: //System.out.println(name+": "+sig);
52: names.put(name, name);
53:
54: Code code = classfile.addMethod(name, sig, ClassFile.PUBLIC);
55:
56: code.aload(0);
57: int pyfunc = code.pool.Fieldref(classfile.name, name,
58: "Lorg/python/core/PyObject;");
59: code.getfield(pyfunc);
60: code.dup();
61: Label returnNull = code.getLabel();
62: code.ifnull(returnNull);
63: callMethod(code, name, parameters, ret, method
64: .getExceptionTypes());
65: returnNull.setPosition();
66: doNullReturn(code, ret);
67: }
68: }
|