Skip to content

Commit aca8d82

Browse files
committed
Added extra loading capability of created objects
Should work for #23 Objects generated by a transformer can now have a method called after the object parsing (__extra_loading__) They can use the unmarshaller there to load trailing data (as seen in HashMap serialization)
1 parent f623558 commit aca8d82

File tree

1 file changed

+59
-12
lines changed

1 file changed

+59
-12
lines changed

javaobj.py

Lines changed: 59 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ def readObject(self, ignore_remaining_data=False):
565565
log_error("Warning!!!!: Stream still has {0} bytes left. "
566566
"Enable debug mode of logging to see the hexdump."
567567
.format(len(the_rest)))
568-
log_debug(self._create_hexdump(the_rest))
568+
log_debug("\n{0}".format(self._create_hexdump(the_rest)))
569569
else:
570570
log_debug("Java Object unmarshalled successfully!")
571571

@@ -838,8 +838,8 @@ def do_object(self, parent=None, ident=0):
838838

839839
# Create object
840840
for transformer in self.object_transformers:
841-
java_object = transformer.create(classdesc)
842-
if java_object:
841+
java_object = transformer.create(classdesc, self)
842+
if java_object is not None:
843843
break
844844

845845
# Store classdesc of this object
@@ -913,6 +913,11 @@ def do_object(self, parent=None, ident=0):
913913
log_debug("java_object.annotations after: {0}"
914914
.format(java_object.annotations), ident)
915915

916+
# Allow extra loading operations
917+
if hasattr(java_object, "__extra_loading__"):
918+
log_debug("Java object has extra loading capability.")
919+
java_object.__extra_loading__(self, ident)
920+
916921
log_debug(">>> java_object: {0}".format(java_object), ident)
917922
return java_object
918923

@@ -1157,9 +1162,9 @@ def _oops_dump_state(self, ignore_remaining_data=False):
11571162
the_rest = self.object_stream.read()
11581163

11591164
if not ignore_remaining_data and len(the_rest):
1160-
log_error("Warning!!!!: Stream still has {0} bytes left."
1161-
.format(len(the_rest)))
1162-
log_error(self._create_hexdump(the_rest, position))
1165+
log_error(
1166+
"Warning!!!!: Stream still has {0} bytes left:\n{1}".format(
1167+
len(the_rest), self._create_hexdump(the_rest, position)))
11631168

11641169
log_error("=" * 30)
11651170

@@ -1606,18 +1611,59 @@ class JavaList(list, JavaObject):
16061611
"""
16071612
Python-Java list bridge type
16081613
"""
1609-
def __init__(self, *args, **kwargs):
1610-
list.__init__(self, *args, **kwargs)
1614+
def __init__(self, unmarshaller):
1615+
# type: (JavaObjectUnmarshaller) -> None
1616+
list.__init__(self)
16111617
JavaObject.__init__(self)
16121618

1619+
def __extra_loading__(self, unmarshaller, ident=0):
1620+
# type: (JavaObjectUnmarshaller, int) -> None
1621+
"""
1622+
Loads the content of the map, written with a custom implementation
1623+
"""
1624+
# Lists have their content in there annotations
1625+
self.extend(self.annotations[1:])
1626+
16131627
class JavaMap(dict, JavaObject):
16141628
"""
16151629
Python-Java dictionary/map bridge type
16161630
"""
1617-
def __init__(self, *args, **kwargs):
1618-
dict.__init__(self, *args, **kwargs)
1631+
def __init__(self, unmarshaller):
1632+
# type: (JavaObjectUnmarshaller) -> None
1633+
dict.__init__(self)
16191634
JavaObject.__init__(self)
16201635

1636+
def __extra_loading__(self, unmarshaller, ident=0):
1637+
# type: (JavaObjectUnmarshaller, int) -> None
1638+
"""
1639+
Loads the content of the map, written with a custom implementation
1640+
"""
1641+
# Ignore the blockdata opid
1642+
(opid,) = unmarshaller._readStruct(">B")
1643+
if opid != unmarshaller.SC_BLOCK_DATA:
1644+
raise ValueError("Start of block data not found")
1645+
1646+
# Read HashMap fields
1647+
self.buckets = unmarshaller._read_value(
1648+
unmarshaller.TYPE_INTEGER, ident)
1649+
self.size = unmarshaller._read_value(
1650+
unmarshaller.TYPE_INTEGER, ident)
1651+
1652+
# Read entries
1653+
for _ in range(self.size):
1654+
key = unmarshaller._read_and_exec_opcode()[1]
1655+
value = unmarshaller._read_and_exec_opcode()[1]
1656+
self[key] = value
1657+
1658+
# Ignore the end of the blockdata
1659+
unmarshaller._read_and_exec_opcode(
1660+
ident, [unmarshaller.TC_ENDBLOCKDATA])
1661+
1662+
# Ignore the trailing 0
1663+
(opid,) = unmarshaller._readStruct(">B")
1664+
if opid != 0:
1665+
raise ValueError("Should find 0x0, got {0:x}".format(opid))
1666+
16211667
TYPE_MAPPER = {
16221668
"java.util.ArrayList": JavaList,
16231669
"java.util.LinkedList": JavaList,
@@ -1626,7 +1672,8 @@ def __init__(self, *args, **kwargs):
16261672
"java.util.TreeMap": JavaMap,
16271673
}
16281674

1629-
def create(self, classdesc):
1675+
def create(self, classdesc, unmarshaller=None):
1676+
# type: (JavaClass, JavaObjectUnmarshaller) -> JavaObject
16301677
"""
16311678
Transforms a deserialized Java object into a Python object
16321679
@@ -1643,7 +1690,7 @@ def create(self, classdesc):
16431690
log_debug(classdesc.name)
16441691
log_debug("---")
16451692

1646-
java_object = mapped_type()
1693+
java_object = mapped_type(unmarshaller)
16471694

16481695
log_debug(">>> java_object: {0}".format(java_object))
16491696
return java_object

0 commit comments

Comments
 (0)