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