Skip to content

Commit f98a9d3

Browse files
committed
Code review
1 parent 68ad3cc commit f98a9d3

File tree

1 file changed

+43
-23
lines changed

1 file changed

+43
-23
lines changed

javaobj.py

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ def __eq__(self, other):
304304
return False
305305

306306
for name in self.classdesc.fields_names:
307-
if not (getattr(self, name) == getattr(other, name)):
307+
if not getattr(self, name) == getattr(other, name):
308308
return False
309309
return True
310310

@@ -427,6 +427,9 @@ class JavaObjectConstants(object):
427427

428428

429429
class OpCodeDebug(object):
430+
"""
431+
OP Codes definition and utility methods
432+
"""
430433
# Type codes
431434
OP_CODE = dict((getattr(JavaObjectConstants, key), key)
432435
for key in dir(JavaObjectConstants)
@@ -442,22 +445,38 @@ class OpCodeDebug(object):
442445

443446
@staticmethod
444447
def op_id(op_id):
448+
"""
449+
Returns the name of the given OP Code
450+
:param op_id: OP Code
451+
:return: Name of the OP Code
452+
"""
445453
return OpCodeDebug.OP_CODE.get(
446454
op_id, "<unknown OP:{0}>".format(op_id))
447455

448456
@staticmethod
449457
def type_code(type_id):
458+
"""
459+
Returns the name of the given Type Code
460+
:param type_id: Type code
461+
:return: Name of the type code
462+
"""
450463
return OpCodeDebug.TYPE.get(
451464
type_id, "<unknown Type:{0}>".format(type_id))
452465

453466
@staticmethod
454467
def flags(flags):
468+
"""
469+
Returns the names of the class description flags found in the given
470+
integer
471+
472+
:param flags: A class description flag entry
473+
:return: The flags names as a single string
474+
"""
455475
names = sorted(
456476
descr for key, descr in OpCodeDebug.STREAM_CONSTANT.items()
457477
if key & flags)
458478
return ', '.join(names)
459479

460-
461480
# ------------------------------------------------------------------------------
462481

463482

@@ -1545,46 +1564,47 @@ class DefaultObjectTransformer(object):
15451564
Converts JavaObject objects to Python types (maps, lists, ...)
15461565
"""
15471566
class JavaList(list, JavaObject):
1567+
"""
1568+
Python-Java list bridge type
1569+
"""
15481570
def __init__(self, *args, **kwargs):
15491571
list.__init__(self, *args, **kwargs)
15501572
JavaObject.__init__(self)
15511573

15521574
class JavaMap(dict, JavaObject):
1575+
"""
1576+
Python-Java dictionary/map bridge type
1577+
"""
15531578
def __init__(self, *args, **kwargs):
15541579
dict.__init__(self, *args, **kwargs)
15551580
JavaObject.__init__(self)
15561581

1582+
TYPE_MAPPER = {
1583+
"java.util.ArrayList": JavaList,
1584+
"java.util.LinkedList": JavaList,
1585+
"java.util.HashMap": JavaMap,
1586+
"java.util.LinkedHashMap": JavaMap,
1587+
"java.util.TreeMap": JavaMap,
1588+
}
1589+
15571590
def create(self, classdesc):
15581591
"""
15591592
Transforms a deserialized Java object into a Python object
15601593
1561-
:param java_object: A JavaObject instance
1594+
:param classdesc: The description of a Java class
15621595
:return: The Python form of the object, or the original JavaObject
15631596
"""
1564-
1565-
if classdesc.name in ("java.util.ArrayList", "java.util.LinkedList"):
1566-
# @serialData The length of the array backing the
1567-
# <tt>ArrayList</tt> instance is emitted (int),
1568-
# followed by all of its elements
1569-
# (each an <tt>Object</tt>) in the proper order
1597+
try:
1598+
mapped_type = self.TYPE_MAPPER[classdesc.name]
1599+
except KeyError:
1600+
# Return a JavaObject by default
1601+
return JavaObject()
1602+
else:
15701603
log_debug("---")
15711604
log_debug(classdesc.name)
15721605
log_debug("---")
15731606

1574-
java_object = self.JavaList()
1575-
1576-
log_debug(">>> java_object: {0}".format(java_object))
1577-
return java_object
1578-
1579-
if classdesc.name == "java.util.HashMap":
1580-
log_debug("---")
1581-
log_debug("java.util.HashMap")
1582-
log_debug("---")
1583-
1584-
java_object = self.JavaMap()
1607+
java_object = mapped_type()
15851608

15861609
log_debug(">>> java_object: {0}".format(java_object))
15871610
return java_object
1588-
1589-
# Return a JavaObject by default
1590-
return JavaObject()

0 commit comments

Comments
 (0)