KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > python > compiler > JavaMaker


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

3 package org.python.compiler;
4 import java.lang.reflect.Method JavaDoc;
5 import java.lang.reflect.Modifier JavaDoc;
6 import java.util.Hashtable JavaDoc;
7 import org.python.core.PyObject;
8 import org.python.core.PyProxy;
9 import org.python.core.Py;
10
11 public class JavaMaker extends ProxyMaker implements ClassConstants
12 {
13     public String JavaDoc pythonClass, pythonModule;
14     public String JavaDoc[] properties;
15     public String JavaDoc[] packages;
16     //Hashtable methods;
17
PyObject methods;
18     public boolean frozen, main;
19
20     public JavaMaker(Class JavaDoc superclass, Class JavaDoc[] interfaces,
21                      String JavaDoc pythonClass, String JavaDoc pythonModule, String JavaDoc myClass,
22                      PyObject methods)
23     {
24         this(superclass, interfaces, pythonClass, pythonModule, myClass,
25              null, null, methods, false, false);
26     }
27
28     public JavaMaker(Class JavaDoc superclass, Class JavaDoc[] interfaces,
29                      String JavaDoc pythonClass, String JavaDoc pythonModule, String JavaDoc myClass,
30                      String JavaDoc[] packages, String JavaDoc[] properties,
31                      PyObject methods,
32                      boolean frozen, boolean main)
33     {
34         super(myClass, superclass, interfaces);
35 // System.out.println("props: "+properties+", "+properties.length);
36
this.pythonClass = pythonClass;
37         this.pythonModule = pythonModule;
38         this.packages = packages;
39         this.properties = properties;
40         this.frozen = frozen;
41         this.main = main;
42         this.methods = methods;
43     }
44
45     private void makeStrings(Code code, String JavaDoc[] list) throws Exception JavaDoc {
46         if (list != null) {
47             int n = list.length;
48             code.iconst(n);
49             code.anewarray(code.pool.Class("java/lang/String"));
50             int strings = code.getLocal("[java/lang/String");
51             code.astore(strings);
52             for(int i=0; i<n; i++) {
53                 code.aload(strings);
54                 code.iconst(i);
55                 code.ldc(list[i]);
56                 code.aastore();
57             }
58             code.aload(strings);
59             code.freeLocal(strings);
60         } else {
61             code.aconst_null();
62         }
63     }
64
65     public void addConstructor(String JavaDoc name, Class JavaDoc[] parameters, Class JavaDoc ret,
66                                String JavaDoc sig, int access)
67         throws Exception JavaDoc
68     {
69         /* Need a fancy constructor for the Java side of things */
70         Code code = classfile.addMethod("<init>", sig, access);
71         callSuper(code, "<init>", name, parameters, null, sig);
72         code.aload(0);
73         getArgs(code, parameters);
74
75         int initProxy = code.pool.Methodref(
76                  classfile.name, "__initProxy__",
77                  "([Ljava/lang/Object;)V");
78         code.invokevirtual(initProxy);
79         code.return_();
80     }
81
82     public void addProxy() throws Exception JavaDoc {
83         if (methods != null)
84             super.addProxy();
85
86         // _initProxy method
87
Code code = classfile.addMethod("__initProxy__",
88                         "([Ljava/lang/Object;)V", Modifier.PUBLIC);
89
90         code.aload(0);
91         code.ldc(pythonModule);
92         code.ldc(pythonClass);
93
94         code.aload(1);
95
96         makeStrings(code, packages);
97         makeStrings(code, properties);
98
99         code.iconst(frozen ? 1 : 0);
100
101         int initProxy = code.pool.Methodref(
102             "org/python/core/Py", "initProxy",
103             "(" + $pyProxy + $str + $str + $objArr +
104                 $strArr + $strArr + "Z)V");
105         code.invokestatic(initProxy);
106         code.return_();
107
108         if (main)
109             addMain();
110     }
111
112 // public void addMethods(Class c) throws Exception {
113
// if (methods != null) {
114
// super.addMethods(c);
115
// }
116
// }
117

118     public void addMethod(Method JavaDoc method, int access) throws Exception JavaDoc {
119         //System.out.println("add: "+method.getName()+", "+
120
// methods.containsKey(method.getName()));
121
// Check to see if it's an abstract method
122
if (Modifier.isAbstract(access)) {
123             // Maybe throw an exception here???
124
super.addMethod(method, access);
125         } else if (methods.__finditem__(method.getName().intern()) != null) {
126             super.addMethod(method, access);
127         } else if (Modifier.isProtected(method.getModifiers())) {
128             addSuperMethod(method, access);
129         }
130     }
131
132 /*
133     public void addSuperMethod(String methodName, String superName,
134                                String superclass, Class[] parameters,
135                                Class ret, String sig, int access)
136         throws Exception
137     {
138         if (!PyProxy.class.isAssignableFrom(this.superclass)) {
139             super.addSuperMethod(methodName,superName,superclass,parameters,
140                                  ret,sig,access);
141         }
142     }
143
144 */

145
146     public void addMain() throws Exception JavaDoc {
147         Code code = classfile.addMethod("main", "(" + $str + ")V",
148                                         ClassFile.PUBLIC | ClassFile.STATIC);
149
150         // Load the class of the Python module to run
151
int forname = code.pool.Methodref(
152                "java/lang/Class","forName", "(" + $str + ")" + $clss);
153         code.ldc(pythonModule);
154         code.invokestatic(forname);
155
156         // Load in any command line arguments
157
code.aload(0);
158         makeStrings(code, packages);
159         makeStrings(code, properties);
160         code.iconst(frozen ? 1 : 0);
161
162         int runMain = code.pool.Methodref(
163             "org/python/core/Py", "runMain",
164             "(" + $clss + $strArr + $strArr + $strArr + "Z)V");
165         code.invokestatic(runMain);
166         code.return_();
167     }
168 }
169
Popular Tags