Skip to content

Commit ec511d4

Browse files
committed
Second pass on typing + fixes
1 parent 4bfdb17 commit ec511d4

File tree

4 files changed

+14
-11
lines changed

4 files changed

+14
-11
lines changed

javaobj/v2/api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def create_instance(self, classdesc):
6262
"""
6363
return None
6464

65-
def load_array(self, reader, field_type, size):
65+
def load_array(self, reader, type_code, size):
6666
# type: (DataStreamReader, TypeCode, int) -> Optional[list]
6767
"""
6868
Loads and returns the content of a Java array, if possible.
@@ -74,7 +74,7 @@ def load_array(self, reader, field_type, size):
7474
This method must return None if it can't handle the array.
7575
7676
:param reader: The data stream reader
77-
:param field_type: Type of the elements of the array
77+
:param type_code: Type of the elements of the array
7878
:param size: Number of elements in the array
7979
"""
8080
return None

javaobj/v2/beans.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ class FieldType(IntEnum):
9797
ARRAY = TypeCode.TYPE_ARRAY.value
9898
OBJECT = TypeCode.TYPE_OBJECT.value
9999

100+
def type_code(self) -> TypeCode:
101+
return TypeCode(self.value)
102+
100103

101104
class ParsedJavaContent(object):
102105
"""

javaobj/v2/core.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def __init__(self, fd, transformers):
100100
self.__handles = {} # type: Dict[int, ParsedJavaContent]
101101

102102
# Initial handle value
103-
self.__current_handle = StreamConstants.BASE_REFERENCE_IDX
103+
self.__current_handle = StreamConstants.BASE_REFERENCE_IDX.value
104104

105105
# Definition of the type code handlers
106106
# Each takes the type code as argument
@@ -663,7 +663,7 @@ def _do_array(self, type_code):
663663

664664
# Array content
665665
for transformer in self.__transformers:
666-
content = transformer.load_array(self.__reader, field_type, size)
666+
content = transformer.load_array(self.__reader, field_type.type_code(), size)
667667
if content is not None:
668668
break
669669
else:

javaobj/v2/transformers.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"""
2626

2727
# Standard library
28-
from typing import List, Optional, Tuple
28+
from typing import List, Optional, Tuple, Union
2929
import functools
3030

3131
# Numpy (optional)
@@ -37,7 +37,7 @@
3737

3838
# Javaobj
3939
from .api import ObjectTransformer
40-
from .beans import JavaInstance, JavaClassDesc
40+
from .beans import JavaInstance, JavaClassDesc, FieldType
4141
from ..constants import TerminalCode, TypeCode
4242
from ..utils import to_bytes, log_error, log_debug, read_struct, read_string
4343

@@ -238,7 +238,7 @@ def load_from_instance(self, indent=0):
238238
"""
239239
# Lists have their content in there annotations
240240
for cd, annotations in self.annotations.items():
241-
if cd.name == self.HANDLED_CLASSES:
241+
if cd.name in self.HANDLED_CLASSES:
242242
# Annotation[1] == size of the set
243243
self.update(x for x in annotations[2:])
244244
return True
@@ -254,7 +254,7 @@ class JavaTime(JavaInstance):
254254
parsed
255255
"""
256256

257-
HANDLED_CLASSES = "java.time.Ser"
257+
HANDLED_CLASSES = ("java.time.Ser",) # type: Tuple[str, ...]
258258

259259
DURATION_TYPE = 1
260260
INSTANT_TYPE = 2
@@ -322,7 +322,7 @@ def load_from_instance(self, indent=0):
322322
"""
323323
# Lists have their content in there annotations
324324
for cd, annotations in self.annotations.items():
325-
if cd.name == self.HANDLED_CLASSES:
325+
if cd.name in self.HANDLED_CLASSES:
326326
# Convert back annotations to bytes
327327
# latin-1 is used to ensure that bytes are kept as is
328328
content = to_bytes(annotations[0].data, "latin1")
@@ -495,14 +495,14 @@ class NumpyArrayTransformer(ObjectTransformer):
495495
TypeCode.TYPE_BOOLEAN: ">B",
496496
}
497497

498-
def load_array(self, reader, field_type, size):
498+
def load_array(self, reader, type_code, size):
499499
# type: (DataStreamReader, TypeCode, int) -> Optional[list]
500500
"""
501501
Loads a Java array, if possible
502502
"""
503503
if numpy is not None:
504504
try:
505-
dtype = self.NUMPY_TYPE_MAP[field_type]
505+
dtype = self.NUMPY_TYPE_MAP[type_code]
506506
except KeyError:
507507
# Unhandled data type
508508
return None

0 commit comments

Comments
 (0)