11// Copyright (c) Corporation for National Research Initiatives
2-
32package org .python .compiler ;
43
54import java .lang .reflect .Method ;
65import java .lang .reflect .Modifier ;
76
87import org .python .core .PyObject ;
98
10- public class JavaMaker extends ProxyMaker implements ClassConstants
11- {
9+ public class JavaMaker extends ProxyMaker implements ClassConstants {
10+
1211 public String pythonClass , pythonModule ;
13- public String [] properties ;
14- public String [] packages ;
15- PyObject methods ;
16- public boolean frozen , main ;
1712
18- public JavaMaker (Class <?> superclass , Class <?>[] interfaces ,
19- String pythonClass , String pythonModule , String myClass ,
20- PyObject methods )
21- {
22- this (superclass , interfaces , pythonClass , pythonModule , myClass ,
23- null , null , methods , false , false );
24- }
13+ PyObject methods ;
2514
26- public JavaMaker (Class <?> superclass , Class <?>[] interfaces ,
27- String pythonClass , String pythonModule , String myClass ,
28- String [] packages , String [] properties ,
29- PyObject methods ,
30- boolean frozen , boolean main )
31- {
15+ public JavaMaker (Class <?> superclass ,
16+ Class <?>[] interfaces ,
17+ String pythonClass ,
18+ String pythonModule ,
19+ String myClass ,
20+ PyObject methods ) {
3221 super (myClass , superclass , interfaces );
33- // System.out.println("props: "+properties+", "+properties.length);
3422 this .pythonClass = pythonClass ;
3523 this .pythonModule = pythonModule ;
36- this .packages = packages ;
37- this .properties = properties ;
38- this .frozen = frozen ;
39- this .main = main ;
4024 this .methods = methods ;
4125 }
4226
43- private void makeStrings (Code code , String [] list ) throws Exception {
44- if (list != null ) {
45- int n = list .length ;
46- code .iconst (n );
47- code .anewarray (code .pool .Class ("java/lang/String" ));
48- int strings = code .getLocal ("[java/lang/String" );
49- code .astore (strings );
50- for (int i =0 ; i <n ; i ++) {
51- code .aload (strings );
52- code .iconst (i );
53- code .ldc (list [i ]);
54- code .aastore ();
55- }
56- code .aload (strings );
57- code .freeLocal (strings );
58- } else {
59- code .aconst_null ();
60- }
61- }
62-
63- public void addConstructor (String name , Class <?>[] parameters , Class <?> ret ,
64- String sig , int access )
65- throws Exception
66- {
27+ public void addConstructor (String name ,
28+ Class <?>[] parameters ,
29+ Class <?> ret ,
30+ String sig ,
31+ int access ) throws Exception {
6732 /* Need a fancy constructor for the Java side of things */
6833 Code code = classfile .addMethod ("<init>" , sig , access );
6934 callSuper (code , "<init>" , name , parameters , null , sig );
7035 code .aload (0 );
7136 getArgs (code , parameters );
72-
37+
7338 int initProxy = code .pool .Methodref (classfile .name ,
7439 "__initProxy__" ,
7540 "([Ljava/lang/Object;)V" );
@@ -80,34 +45,20 @@ public void addConstructor(String name, Class<?>[] parameters, Class<?> ret,
8045 public void addProxy () throws Exception {
8146 if (methods != null )
8247 super .addProxy ();
83-
8448 // _initProxy method
8549 Code code = classfile .addMethod ("__initProxy__" , "([Ljava/lang/Object;)V" , Modifier .PUBLIC );
86-
8750 code .aload (0 );
8851 code .ldc (pythonModule );
8952 code .ldc (pythonClass );
90-
9153 code .aload (1 );
92-
93- makeStrings (code , packages );
94- makeStrings (code , properties );
95-
96- code .iconst (frozen ? 1 : 0 );
97-
9854 int initProxy = code .pool .Methodref ("org/python/core/Py" , "initProxy" , "(" + $pyProxy
99- + $str + $str + $objArr + $strArr + $strArr + "Z )V" );
55+ + $str + $str + $objArr + " )V" );
10056 code .invokestatic (initProxy );
10157 code .return_ ();
10258
103- if (main )
104- addMain ();
10559 }
10660
10761 public void addMethod (Method method , int access ) throws Exception {
108- //System.out.println("add: "+method.getName()+", "+
109- // methods.containsKey(method.getName()));
110- // Check to see if it's an abstract method
11162 if (Modifier .isAbstract (access )) {
11263 // Maybe throw an exception here???
11364 super .addMethod (method , access );
@@ -117,24 +68,4 @@ public void addMethod(Method method, int access) throws Exception {
11768 addSuperMethod (method , access );
11869 }
11970 }
120-
121- public void addMain () throws Exception {
122- Code code = classfile .addMethod ("main" , "(" + $str + ")V" , ClassFile .PUBLIC
123- | ClassFile .STATIC );
124-
125- // Load the class of the Python module to run
126- int forname = code .pool .Methodref ("java/lang/Class" , "forName" , "(" + $str + ")" + $clss );
127- code .ldc (pythonModule );
128- code .invokestatic (forname );
129-
130- // Load in any command line arguments
131- code .aload (0 );
132- makeStrings (code , packages );
133- makeStrings (code , properties );
134- code .iconst (frozen ? 1 : 0 );
135- int runMain = code .pool .Methodref ("org/python/core/Py" , "runMain" , "(" + $clss + $strArr
136- + $strArr + $strArr + "Z)V" );
137- code .invokestatic (runMain );
138- code .return_ ();
139- }
14071}
0 commit comments