KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > python > core > MakeProxies


1 // Copyright (c) Corporation for National Research Initiatives
2

3 package org.python.core;
4
5 import java.lang.reflect.*;
6 import java.util.Vector JavaDoc;
7 import java.util.Hashtable JavaDoc;
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 JavaDoc makeClass(Class JavaDoc referent, Vector JavaDoc secondary,
17                                    String JavaDoc name, ByteArrayOutputStream bytes)
18     {
19         Vector JavaDoc 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 JavaDoc();
29                 referents.addElement(referent);
30             }
31         }
32
33         return BytecodeLoader.makeClass(name, referents, bytes.toByteArray());
34     }
35
36     public static Class JavaDoc makeAdapter(Class JavaDoc c) {
37         ByteArrayOutputStream bytes = new ByteArrayOutputStream();
38         String JavaDoc name;
39         try {
40             name = AdapterMaker.makeAdapter(c, bytes);
41         }
42         catch (Exception JavaDoc exc) {
43             throw Py.JavaError(exc);
44         }
45
46         Py.saveClassFile(name, bytes);
47
48         Class JavaDoc pc = makeClass(c, null, name, bytes);
49         return pc;
50     }
51
52     private static final String JavaDoc proxyPrefix = "org.python.proxies.";
53     private static int proxyNumber = 0;
54
55     public static synchronized Class JavaDoc makeProxy(Class JavaDoc superclass,
56                                                Vector JavaDoc vinterfaces,
57                                                String JavaDoc className,
58                                                String JavaDoc proxyName,
59                                                PyObject dict)
60     {
61         Class JavaDoc[] interfaces = new Class JavaDoc[vinterfaces.size()];
62
63         for (int i=0; i<vinterfaces.size(); i++) {
64             interfaces[i] = (Class JavaDoc)vinterfaces.elementAt(i);
65         }
66         String JavaDoc fullProxyName = proxyPrefix + proxyName + "$" + proxyNumber++;
67         String JavaDoc pythonModuleName;
68         PyObject mn=dict.__finditem__("__module__");
69         if (mn==null)
70             pythonModuleName = "foo";
71         else
72             pythonModuleName = (String JavaDoc)mn.__tojava__(String JavaDoc.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 JavaDoc exc) {
84             throw Py.JavaError(exc);
85         }
86     }
87 }
88
Popular Tags