1 3 package org.python.compiler; 4 import java.lang.reflect.Method ; 5 import java.lang.reflect.Modifier ; 6 import java.util.Hashtable ; 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 pythonClass, pythonModule; 14 public String [] properties; 15 public String [] packages; 16 PyObject methods; 18 public boolean frozen, main; 19 20 public JavaMaker(Class superclass, Class [] interfaces, 21 String pythonClass, String pythonModule, String myClass, 22 PyObject methods) 23 { 24 this(superclass, interfaces, pythonClass, pythonModule, myClass, 25 null, null, methods, false, false); 26 } 27 28 public JavaMaker(Class superclass, Class [] interfaces, 29 String pythonClass, String pythonModule, String myClass, 30 String [] packages, String [] properties, 31 PyObject methods, 32 boolean frozen, boolean main) 33 { 34 super(myClass, superclass, interfaces); 35 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 [] list) throws Exception { 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 name, Class [] parameters, Class ret, 66 String sig, int access) 67 throws Exception 68 { 69 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 { 83 if (methods != null) 84 super.addProxy(); 85 86 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 118 public void addMethod(Method method, int access) throws Exception { 119 if (Modifier.isAbstract(access)) { 123 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 145 146 public void addMain() throws Exception { 147 Code code = classfile.addMethod("main", "(" + $str + ")V", 148 ClassFile.PUBLIC | ClassFile.STATIC); 149 150 int forname = code.pool.Methodref( 152 "java/lang/Class","forName", "(" + $str + ")" + $clss); 153 code.ldc(pythonModule); 154 code.invokestatic(forname); 155 156 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 |