Skip to content

Commit 927b101

Browse files
committed
Added a dump() method to mimic jdeserialize
1 parent 9a55819 commit 927b101

File tree

1 file changed

+108
-6
lines changed

1 file changed

+108
-6
lines changed

javaobj/v2/beans.py

Lines changed: 108 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,18 @@ def __init__(self, content_type: ContentType):
8585
self.is_exception: bool = False
8686
self.handle: int = 0
8787

88+
def __str__(self):
89+
return "[ParseJavaObject 0x{0:x} - {1}]".format(self.handle, self.type)
90+
91+
__repr__ = __str__
92+
93+
def dump(self, indent=0):
94+
# type: (int) -> str
95+
"""
96+
Base implementation of a parsed object
97+
"""
98+
return "\t" * indent + str(self)
99+
88100
def validate(self) -> None:
89101
"""
90102
Validity check on the object
@@ -103,6 +115,13 @@ def __init__(self, exception_object: ParsedJavaContent, data: bytes):
103115
self.stream_data = data
104116
self.handle = exception_object.handle
105117

118+
def dump(self, indent=0):
119+
# type: (int) -> str
120+
"""
121+
Returns a dump representation of the exception
122+
"""
123+
return "\t" * indent + "[ExceptionState {0:x}]".format(self.handle)
124+
106125

107126
class ExceptionRead(Exception):
108127
"""
@@ -127,11 +146,19 @@ def __init__(self, handle: int, data: bytes):
127146

128147
def __repr__(self) -> str:
129148
return repr(self.value)
130-
# "[String {0:x}: {1}]".format(self.handle, self.value)
131149

132150
def __str__(self):
133151
return self.value
134152

153+
def dump(self, indent=0):
154+
# type: (int) -> str
155+
"""
156+
Returns a dump representation of the string
157+
"""
158+
return "\t" * indent + "[String {0:x}: {1}]".format(
159+
self.handle, repr(self.value)
160+
)
161+
135162
def __hash__(self):
136163
return hash(self.value)
137164

@@ -226,6 +253,15 @@ def __str__(self):
226253

227254
__repr__ = __str__
228255

256+
def dump(self, indent=0):
257+
# type: (int) -> str
258+
"""
259+
Returns a dump representation of the exception
260+
"""
261+
return "\t" * indent + "[classdesc 0x{0:x}: name {1}, uid {2}]".format(
262+
self.handle, self.name, self.serial_version_uid
263+
)
264+
229265
@property
230266
def serialVersionUID(self):
231267
"""
@@ -322,6 +358,52 @@ def __str__(self):
322358

323359
__repr__ = __str__
324360

361+
def dump(self, indent=0):
362+
# type: (int) -> str
363+
"""
364+
Returns a dump representation of the exception
365+
"""
366+
prefix = "\t" * indent
367+
sub_prefix = "\t" * (indent + 1)
368+
369+
dump = [
370+
prefix
371+
+ "[instance 0x{0:x}: {1:x} / {2}]".format(
372+
self.handle, self.classdesc.handle, self.classdesc.name
373+
)
374+
]
375+
376+
for cd, annotations in self.annotations.items():
377+
dump.append(
378+
"{0}{1} -- {2} annotations".format(
379+
prefix, cd.name, len(annotations)
380+
)
381+
)
382+
for ann in annotations:
383+
dump.append(sub_prefix + repr(ann))
384+
385+
for cd, fields in self.field_data.items():
386+
dump.append(
387+
"{0}{1} -- {2} fields".format(prefix, cd.name, len(fields))
388+
)
389+
for field, value in fields.items():
390+
if isinstance(value, ParsedJavaContent):
391+
if self.handle != 0 and value.handle == self.handle:
392+
value_str = "this"
393+
else:
394+
value_str = "\n" + value.dump(indent + 2)
395+
else:
396+
value_str = repr(value)
397+
398+
dump.append(
399+
"{0}{1} {2}: {3}".format(
400+
sub_prefix, field.type.name, field.name, value_str
401+
)
402+
)
403+
404+
dump.append(prefix + "[/instance 0x{0:x}]".format(self.handle))
405+
return "\n".join(dump)
406+
325407
def __getattr__(self, name):
326408
"""
327409
Returns the field with the given name
@@ -422,15 +504,34 @@ def __init__(
422504
self.handle = handle
423505
self.classdesc = class_desc
424506
self.field_type = field_type
425-
self.content = content
507+
self.data = content
426508

427509
def __str__(self):
428-
return "[array 0x{0:x}: {1} items]".format(
429-
self.handle, len(self.content)
430-
)
510+
return "[{0}]".format(", ".join(repr(x) for x in self))
431511

432512
__repr__ = __str__
433513

514+
def dump(self, indent=0):
515+
# type: (int) -> str
516+
"""
517+
Returns a dump representation of the array
518+
"""
519+
prefix = "\t" * indent
520+
sub_prefix = "\t" * (indent + 1)
521+
dump = [
522+
prefix + "[array 0x{0:x}: {1} items]".format(self.handle, len(self))
523+
]
524+
for x in self:
525+
if isinstance(x, ParsedJavaContent):
526+
if self.handle != 0 and x.handle == self.handle:
527+
dump.append("this,")
528+
else:
529+
dump.append(x.dump(indent + 1) + ",")
530+
else:
531+
dump.append(sub_prefix + repr(x) + ",")
532+
dump.append(prefix + "[/array 0x{0:x}]".format(self.handle))
533+
return "\n".join(dump)
534+
434535
@property
435536
def _data(self):
436537
"""
@@ -453,7 +554,8 @@ def __str__(self):
453554
self.handle, len(self.data)
454555
)
455556

456-
__repr__ = __str__
557+
def __repr__(self):
558+
return repr(self.data)
457559

458560
def __eq__(self, other):
459561
if isinstance(other, (str, UNICODE_TYPE)):

0 commit comments

Comments
 (0)