01: // Copyright (c) Corporation for National Research Initiatives
02:
03: package org.python.core;
04:
05: import java.io.ByteArrayOutputStream;
06: import java.util.Vector;
07:
08: import org.python.compiler.AdapterMaker;
09: import org.python.compiler.JavaMaker;
10:
11: class MakeProxies {
12: private static Class makeClass(Class referent, Vector secondary,
13: String name, ByteArrayOutputStream bytes) {
14: Vector referents = null;
15:
16: if (secondary != null) {
17: if (referent != null) {
18: secondary.insertElementAt(referent, 0);
19: }
20: referents = secondary;
21: } else {
22: if (referent != null) {
23: referents = new Vector();
24: referents.addElement(referent);
25: }
26: }
27:
28: return BytecodeLoader.makeClass(name, referents, bytes
29: .toByteArray());
30: }
31:
32: public static Class makeAdapter(Class c) {
33: ByteArrayOutputStream bytes = new ByteArrayOutputStream();
34: String name;
35: try {
36: name = AdapterMaker.makeAdapter(c, bytes);
37: } catch (Exception exc) {
38: throw Py.JavaError(exc);
39: }
40:
41: Py.saveClassFile(name, bytes);
42:
43: Class pc = makeClass(c, null, name, bytes);
44: return pc;
45: }
46:
47: private static final String proxyPrefix = "org.python.proxies.";
48:
49: private static int proxyNumber = 0;
50:
51: public static synchronized Class makeProxy(Class super class,
52: Vector vinterfaces, String className, String proxyName,
53: PyObject dict) {
54: Class[] interfaces = new Class[vinterfaces.size()];
55:
56: for (int i = 0; i < vinterfaces.size(); i++) {
57: interfaces[i] = (Class) vinterfaces.elementAt(i);
58: }
59: String fullProxyName = proxyPrefix + proxyName + "$"
60: + proxyNumber++;
61: String pythonModuleName;
62: PyObject mn = dict.__finditem__("__module__");
63: if (mn == null) {
64: pythonModuleName = "foo";
65: } else {
66: pythonModuleName = (String) mn.__tojava__(String.class);
67: }
68: JavaMaker jm = new JavaMaker(super class, interfaces, className,
69: pythonModuleName, fullProxyName, dict);
70: try {
71: jm.build();
72: ByteArrayOutputStream bytes = new ByteArrayOutputStream();
73: jm.classfile.write(bytes);
74: Py.saveClassFile(fullProxyName, bytes);
75:
76: return makeClass(super class, vinterfaces, jm.myClass, bytes);
77: } catch (Exception exc) {
78: throw Py.JavaError(exc);
79: }
80: }
81: }
|