def addPyProxyInterface(self): self.statements.append( jast.Declare('private PyInstance', jast.Identifier('__proxy'))) code = jast.Set(jast.Identifier("__proxy"), jast.Identifier("inst")) code = jast.Block([code]) self.statements.append( jast.Method("_setPyInstance", "public", ["void", ("PyInstance", "inst")], code)) code = jast.Block([jast.Return(jast.Identifier("__proxy"))]) self.statements.append( jast.Method("_getPyInstance", "public", ["PyInstance"], code)) self.statements.append( jast.Declare('private PySystemState', jast.Identifier('__sysstate'))) code = jast.Set(jast.Identifier("__sysstate"), jast.Identifier("inst")) code = jast.Block([code]) self.statements.append( jast.Method("_setPySystemState", "public", ["void", ("PySystemState", "inst")], code)) code = jast.Block([jast.Return(jast.Identifier("__sysstate"))]) self.statements.append( jast.Method("_getPySystemState", "public", ["PySystemState"], code)) frozen = self.module.getFrozen() this = jast.Identifier("this") initargs = [ this, jast.StringConstant(self.modname), jast.StringConstant(self.name), jast.Identifier("args"), self.packages, self.properties, frozen, jast.StringArray(self.modules) ] initproxy = jast.InvokeStatic("Py", "initProxy", initargs) code = jast.Block([initproxy]) self.statements.append( jast.Method("__initProxy__", "public", ["void", ("Object[]", "args")], code)) self.interfaces.append(org.python.core.PyProxy)
def dumpConstants(self): self.dumpCodes() stmts = [] decls = [] for type, label, value in self.constants: decls.append(jast.Declare("private static " + type, label)) stmts.append(jast.Set(label, value)) setconstants = jast.Method("initConstants", "private static", ["void"], jast.Block(stmts)) decls.append(setconstants) return decls
def dumpMain(self): meths = [] if self.javamain: code = [] newargs = jast.Identifier("newargs") code.append( jast.Declare("String[]", newargs, jast.NewArray("String", ["args.length+1"]))) code.append( jast.Set(jast.Identifier("newargs[0]"), jast.StringConstant(self.name))) args = [ jast.Identifier('args'), jast.IntegerConstant(0), jast.Identifier('newargs'), jast.IntegerConstant(1), jast.Identifier('args.length') ] code.append( jast.InvokeStatic("java.lang.System", "arraycopy", args)) args = [ jast.GetStaticAttribute( self.getclassname(self.name + '.' + self.pyinner.name), "class"), jast.Identifier('newargs'), self.getPackages(qual=1), self.getMainProperties(qual=1), self.getFrozen(), jast.StringArray(self.modules.keys()) ] code.append([jast.InvokeStatic("Py", "runMain", args)]) maincode = jast.Block(code) meths.append( jast.Method("main", "public static", ["void", ("String[]", "args")], maincode, ["java.lang.Exception"])) return meths
def dumpDictionary(self, dict, field): props = [] for name, value in dict.items(): props.append(name) props.append(value) return jast.Declare("static String[]", field, jast.StringArray(props))
def callMethod(self, name, access, ret, sig, throws=[], dosuper=1): args = [typeName(ret)] argids = [] objects = [] throws = filterThrows(throws) for c in sig: if isinstance(c, TupleType): argname = c[1] c = c[0] else: argname = "arg" + str(len(argids)) args.append((typeName(c), argname)) argid = jast.Identifier(argname) argids.append(argid) objects.append(makeObject(argid, c)) objects = jast.FilledArray("Object", objects) stmts = [] this = jast.Identifier("this") if isinstance(access, IntType) and isAbstract(access): dosuper = 0 access = access & ~ABSTRACT if not dosuper and not self.isAdapter: getattr = jast.InvokeStatic("Py", "jgetattr", [this, jast.StringConstant(name)]) else: getattr = jast.InvokeStatic("Py", "jfindattr", [this, jast.StringConstant(name)]) inst = jast.Identifier("inst") if len(throws) == 0: jcall = "_jcall" else: jcall = "_jcallexc" jcall = makeReturn(jast.Invoke(inst, jcall, [objects]), ret) jcall = wrapThrows(jcall, throws, ret) if dosuper: supercall = jast.Invoke(jast.Identifier("super"), name, argids) if ret != Void.TYPE: supercall = jast.Return(supercall) supermethod = None if not self.issuperproxy and name not in self.getCandSupermethods( incl=0): supermethod = jast.Method("super__" + name, jast.Modifier.ModifierString(access), args, jast.Block([supercall]), throws) self.supermethods["super__" + name] = "super__" + name else: if self.isAdapter: supercall = nullReturn(ret) else: supercall = None supermethod = None if not dosuper and not self.isAdapter: test = jcall else: test = jast.If(jast.Operation("!=", inst, jast.Null), jcall, supercall) code = jast.Block([jast.Declare("PyObject", inst, getattr), test]) meth = jast.Method(name, jast.Modifier.ModifierString(access), args, code, throws) if supermethod is not None: self.statements.append(supermethod) self.statements.append(meth)
def makeFreeDecl(self,type,value): self.free_decls.append(jast.Declare(type,value))