1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
import java
import os
import os.path
from java.lang.reflect import Modifier
from org.python.util import CodegenUtils
from org.python.compiler import CustomMaker, ProxyCodeHelpers
__all__ = ["PackageProxy", "SerializableProxies"]
class SerializableProxies(CustomMaker):
# NOTE: SerializableProxies is itself a java proxy, but it's not a custom one!
serialized_path = None
def doConstants(self):
self.classfile.addField("serialVersionUID",
CodegenUtils.ci(java.lang.Long.TYPE), Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL)
code = self.classfile.addMethod("<clinit>", ProxyCodeHelpers.makeSig("V"), Modifier.STATIC)
code.visitLdcInsn(java.lang.Long(1))
code.putstatic(self.classfile.name, "serialVersionUID", CodegenUtils.ci(java.lang.Long.TYPE))
code.return_()
def saveBytes(self, bytes):
if self.serialized_path:
path = os.path.join(self.serialized_path, os.path.join(*self.myClass.split(".")) + ".class")
parent = os.path.dirname(path)
try:
os.makedirs(parent)
except OSError:
pass # Directory exists
with open(path, "wb") as f:
f.write(bytes.toByteArray())
def makeClass(self):
try:
# If already defined on CLASSPATH, simply return this class
cls = java.lang.Class.forName(self.myClass)
print "Class defined on CLASSPATH", cls
except:
# Otherwise build it
cls = CustomMaker.makeClass(self)
return cls
class PackageProxy(object):
def __init__(self, package):
self.package = package
def __call__(self, superclass, interfaces, className, pythonModuleName, fullProxyName, mapping):
"""Constructs a usable proxy name that does not depend on ordering"""
if "." in pythonModuleName:
# get around that will be called differently from regrtest, as test.module instead of module
pythonModuleName = pythonModuleName.split(".")[-1]
return SerializableProxies(superclass, interfaces, className, pythonModuleName, self.package + "." + pythonModuleName + "." + className, mapping)
|