Skip to content

Commit 8e78c7a

Browse files
committed
Removed unncessary parameter in transformer
The "instance" argument in JavaInstance.load_from_instance() is unncessary: it is self.
1 parent 29c6a9b commit 8e78c7a

File tree

4 files changed

+69
-31
lines changed

4 files changed

+69
-31
lines changed

README.rst

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -183,17 +183,36 @@ the ``javaobj.v2.transformer`` module to see the whole implementation.
183183
dict.__init__(self)
184184
JavaInstance.__init__(self)
185185
186-
def load_from_instance(self, instance, indent=0):
187-
# type: (JavaInstance, int) -> bool
186+
def load_from_blockdata(self, parser, reader, indent=0):
187+
"""
188+
Reads content stored in a block data.
189+
190+
This method is called only if the class description has both the
191+
``SC_EXTERNALIZABLE`` and ``SC_BLOCK_DATA`` flags set.
192+
193+
The stream parsing will stop and fail if this method returns False.
194+
195+
:param parser: The JavaStreamParser in use
196+
:param reader: The underlying data stream reader
197+
:param indent: Indentation to use in logs
198+
:return: True on success, False on error
199+
"""
200+
# This kind of class is not supposed to have the SC_BLOCK_DATA flag set
201+
return False
202+
203+
def load_from_instance(self, indent=0):
204+
# type: (int) -> bool
188205
"""
189-
Load content from a parsed instance object
206+
Load content from the parsed instance object.
207+
208+
This method is called after the block data (if any), the fields and
209+
the annotations have been loaded.
190210
191-
:param instance: The currently loaded instance
192211
:param indent: Indentation to use while logging
193-
:return: True on success
212+
:return: True on success (currently ignored)
194213
"""
195214
# Maps have their content in their annotations
196-
for cd, annotations in instance.annotations.items():
215+
for cd, annotations in self.annotations.items():
197216
# Annotations are associated to their definition class
198217
if cd.name == "java.util.HashMap":
199218
# We are in the annotation created by the handled class
@@ -210,6 +229,10 @@ the ``javaobj.v2.transformer`` module to see the whole implementation.
210229
return False
211230
212231
class MapObjectTransformer(javaobj.v2.api.ObjectTransformer):
232+
"""
233+
Creates a JavaInstance object with custom loading methods for the
234+
classes it can handle
235+
"""
213236
def create_instance(self, classdesc):
214237
# type: (JavaClassDesc) -> Optional[JavaInstance]
215238
"""
@@ -219,8 +242,8 @@ the ``javaobj.v2.transformer`` module to see the whole implementation.
219242
:return: The Python form of the object, or the original JavaObject
220243
"""
221244
if classdesc.name == "java.util.HashMap":
222-
# We can handle it
245+
# We can handle this class description
223246
return JavaMap()
224247
else:
225-
# Return None if not handled
248+
# Return None if the class is not handled
226249
return None

javaobj/v2/beans.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -430,13 +430,28 @@ def get_class(self):
430430

431431
def load_from_blockdata(self, parser, reader, indent=0):
432432
"""
433-
Reads content stored in a block data
433+
Reads content stored in a block data.
434+
435+
This method is called only if the class description has both the
436+
``SC_EXTERNALIZABLE`` and ``SC_BLOCK_DATA`` flags set.
437+
438+
The stream parsing will stop and fail if this method returns False.
439+
440+
:param parser: The JavaStreamParser in use
441+
:param reader: The underlying data stream reader
442+
:param indent: Indentation to use in logs
443+
:return: True on success, False on error
434444
"""
435445
return False
436446

437-
def load_from_instance(self, instance, indent=0):
447+
def load_from_instance(self, indent=0):
448+
# type: (int) -> bool
438449
"""
439-
Load content from a parsed instance object
450+
Updates the content of this instance from its parsed fields and
451+
annotations
452+
453+
:param indent: Indentation to use in logs
454+
:return: True on success, False on error (currently ignored)
440455
"""
441456
return False
442457

javaobj/v2/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ def _read_class_data(self, instance):
532532
instance.field_data = all_data
533533

534534
# Load transformation from the fields and annotations
535-
instance.load_from_instance(instance)
535+
instance.load_from_instance()
536536

537537
def _read_field_value(self, field_type):
538538
# type: (FieldType) -> Any

javaobj/v2/transformers.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,13 @@ def __init__(self):
5555
list.__init__(self)
5656
JavaInstance.__init__(self)
5757

58-
def load_from_instance(self, instance, indent=0):
59-
# type: (JavaInstance, int) -> bool
58+
def load_from_instance(self, indent=0):
59+
# type: (int) -> bool
6060
"""
6161
Load content from a parsed instance object
6262
"""
6363
# Lists have their content in there annotations
64-
for cd, annotations in instance.annotations.items():
64+
for cd, annotations in self.annotations.items():
6565
if cd.name in self.HANDLED_CLASSES:
6666
self.extend(ann for ann in annotations[1:])
6767
return True
@@ -94,12 +94,12 @@ def __eq__(self, other):
9494
def __lt__(self, other):
9595
return self.value < other
9696

97-
def load_from_instance(self, instance, indent=0):
98-
# type: (JavaInstance, int) -> bool
97+
def load_from_instance(self, indent=0):
98+
# type: (int) -> bool
9999
"""
100100
Load content from a parsed instance object
101101
"""
102-
for fields in instance.field_data.values():
102+
for fields in self.field_data.values():
103103
for field, value in fields.items():
104104
if field.name == "value":
105105
self.value = value
@@ -134,13 +134,13 @@ def __init__(self):
134134
dict.__init__(self)
135135
JavaInstance.__init__(self)
136136

137-
def load_from_instance(self, instance, indent=0):
138-
# type: (JavaInstance, int) -> bool
137+
def load_from_instance(self, indent=0):
138+
# type: (int) -> bool
139139
"""
140140
Load content from a parsed instance object
141141
"""
142142
# Maps have their content in there annotations
143-
for cd, annotations in instance.annotations.items():
143+
for cd, annotations in self.annotations.items():
144144
if cd.name in JavaMap.HANDLED_CLASSES:
145145
# Group annotation elements 2 by 2
146146
args = [iter(annotations[1:])] * 2
@@ -201,13 +201,13 @@ def __init__(self):
201201
set.__init__(self)
202202
JavaInstance.__init__(self)
203203

204-
def load_from_instance(self, instance, indent=0):
205-
# type: (JavaInstance, int) -> bool
204+
def load_from_instance(self, indent=0):
205+
# type: (int) -> bool
206206
"""
207207
Load content from a parsed instance object
208208
"""
209209
# Lists have their content in there annotations
210-
for cd, annotations in instance.annotations.items():
210+
for cd, annotations in self.annotations.items():
211211
if cd.name in self.HANDLED_CLASSES:
212212
self.update(x for x in annotations[1:])
213213
return True
@@ -222,13 +222,13 @@ class JavaTreeSet(JavaSet):
222222

223223
HANDLED_CLASSES = "java.util.TreeSet"
224224

225-
def load_from_instance(self, instance, indent=0):
226-
# type: (JavaInstance, int) -> bool
225+
def load_from_instance(self, indent=0):
226+
# type: (int) -> bool
227227
"""
228228
Load content from a parsed instance object
229229
"""
230230
# Lists have their content in there annotations
231-
for cd, annotations in instance.annotations.items():
231+
for cd, annotations in self.annotations.items():
232232
if cd.name == self.HANDLED_CLASSES:
233233
# Annotation[1] == size of the set
234234
self.update(x for x in annotations[2:])
@@ -300,19 +300,19 @@ def __str__(self):
300300
"nano={s.nano}, offset={s.offset}, zone={s.zone})"
301301
).format(s=self)
302302

303-
def load_from_blockdata(self, reader, indent=0):
303+
def load_from_blockdata(self, parser, reader, indent=0):
304304
"""
305305
Ignore the SC_BLOCK_DATA flag
306306
"""
307307
return True
308308

309-
def load_from_instance(self, instance, indent=0):
310-
# type: (JavaInstance, int) -> bool
309+
def load_from_instance(self, indent=0):
310+
# type: (int) -> bool
311311
"""
312312
Load content from a parsed instance object
313313
"""
314314
# Lists have their content in there annotations
315-
for cd, annotations in instance.annotations.items():
315+
for cd, annotations in self.annotations.items():
316316
if cd.name == self.HANDLED_CLASSES:
317317
# Convert back annotations to bytes
318318
# latin-1 is used to ensure that bytes are kept as is

0 commit comments

Comments
 (0)