Skip to content

Commit 2b3593b

Browse files
author
Fede A
committed
main project sync
2 parents 574b076 + 776e894 commit 2b3593b

File tree

10 files changed

+169
-65
lines changed

10 files changed

+169
-65
lines changed

javaobj/utils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from __future__ import absolute_import
3131

3232
# Standard library
33+
from typing import Any, Tuple
3334
import logging
3435
import struct
3536
import sys
@@ -76,7 +77,7 @@ def log_error(message, ident=0):
7677

7778

7879
def read_struct(data, fmt_str):
79-
# type: (bytes, str) -> list
80+
# type: (bytes, str) -> Tuple
8081
"""
8182
Reads input bytes and extract the given structure. Returns both the read
8283
elements and the remaining data
@@ -90,7 +91,7 @@ def read_struct(data, fmt_str):
9091

9192

9293
def read_string(data, length_fmt="H"):
93-
# type: (bytes, str) -> UNICODE_TYPE
94+
# type: (bytes, str) -> Tuple[UNICODE_TYPE, bytes]
9495
"""
9596
Reads a serialized string
9697

javaobj/v1/beans.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ def __init__(self, classdesc=None):
198198
JavaObject.__init__(self)
199199
self.classdesc = classdesc
200200

201+
def __hash__(self):
202+
return list.__hash__(self)
201203

202204
class JavaByteArray(JavaObject):
203205
"""

javaobj/v1/transformers.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import functools
3131

3232
from .beans import JavaObject
33+
from .unmarshaller import JavaObjectUnmarshaller
34+
from ..constants import ClassDescFlags, TerminalCode, TypeCode
3335
from ..utils import (
3436
log_debug,
3537
log_error,
@@ -59,6 +61,9 @@ def __init__(self, unmarshaller):
5961
list.__init__(self)
6062
JavaObject.__init__(self)
6163

64+
def __hash__(self):
65+
return list.__hash__(self)
66+
6267
def __extra_loading__(self, unmarshaller, ident=0):
6368
# type: (JavaObjectUnmarshaller, int) -> None
6469
"""
@@ -110,6 +115,9 @@ def __init__(self, unmarshaller):
110115
dict.__init__(self)
111116
JavaObject.__init__(self)
112117

118+
def __hash__(self):
119+
return dict.__hash__(self)
120+
113121
def __extra_loading__(self, unmarshaller, ident=0):
114122
# type: (JavaObjectUnmarshaller, int) -> None
115123
"""
@@ -128,15 +136,15 @@ def __extra_loading__(self, unmarshaller, ident=0):
128136
"""
129137
# Ignore the blockdata opid
130138
(opid,) = unmarshaller._readStruct(">B")
131-
if opid != unmarshaller.SC_BLOCK_DATA:
139+
if opid != ClassDescFlags.SC_BLOCK_DATA:
132140
raise ValueError("Start of block data not found")
133141

134142
# Read HashMap fields
135143
self.buckets = unmarshaller._read_value(
136-
unmarshaller.TYPE_INTEGER, ident
144+
TypeCode.TYPE_INTEGER, ident
137145
)
138146
self.size = unmarshaller._read_value(
139-
unmarshaller.TYPE_INTEGER, ident
147+
TypeCode.TYPE_INTEGER, ident
140148
)
141149

142150
# Read entries
@@ -147,7 +155,7 @@ def __extra_loading__(self, unmarshaller, ident=0):
147155

148156
# Ignore the end of the blockdata
149157
unmarshaller._read_and_exec_opcode(
150-
ident, [unmarshaller.TC_ENDBLOCKDATA]
158+
ident, [TerminalCode.TC_ENDBLOCKDATA]
151159
)
152160

153161
# Ignore the trailing 0
@@ -165,6 +173,9 @@ def __init__(self, unmarshaller):
165173
set.__init__(self)
166174
JavaObject.__init__(self)
167175

176+
def __hash__(self):
177+
return set.__hash__(self)
178+
168179
def __extra_loading__(self, unmarshaller, ident=0):
169180
# type: (JavaObjectUnmarshaller, int) -> None
170181
"""

javaobj/v1/unmarshaller.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
from __future__ import absolute_import
3737

3838
# Standard library
39+
from typing import Any, Union
3940
import os
4041
import struct
4142

@@ -719,7 +720,7 @@ def do_enum(self, parent=None, ident=0):
719720
return enum
720721

721722
def _read_value(self, raw_field_type, ident, name=""):
722-
# type: (bytes, int, str) -> Any
723+
# type: (Union[bytes, int, TypeCode], int, str) -> Any
723724
"""
724725
Reads the next value, of the given type
725726
@@ -729,15 +730,21 @@ def _read_value(self, raw_field_type, ident, name=""):
729730
:return: The read value
730731
:raise RuntimeError: Unknown field type
731732
"""
732-
if isinstance(raw_field_type, (TypeCode, int)):
733+
if isinstance(raw_field_type, TypeCode):
733734
field_type = raw_field_type
735+
elif isinstance(raw_field_type, int):
736+
field_type = TypeCode(raw_field_type)
734737
else:
735738
# We don't need details for arrays and objects
736-
field_type = TypeCode(ord(raw_field_type[0]))
739+
raw_code = raw_field_type[0]
740+
if isinstance(raw_code, int):
741+
field_type = TypeCode(raw_code)
742+
else:
743+
field_type = TypeCode(ord(raw_code))
737744

738745
if field_type == TypeCode.TYPE_BOOLEAN:
739746
(val,) = self._readStruct(">B")
740-
res = bool(val)
747+
res = bool(val) # type: Any
741748
elif field_type == TypeCode.TYPE_BYTE:
742749
(res,) = self._readStruct(">b")
743750
elif field_type == TypeCode.TYPE_CHAR:

javaobj/v2/api.py

Lines changed: 5 additions & 4 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,12 +74,13 @@ 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
81-
81+
8282
def load_custom_writeObject(self, parser, reader, name):
83+
# type: (JavaStreamParser, DataStreamReader, str) -> Optional[JavaClassDesc]
8384
"""
8485
Reads content stored from a custom writeObject.
8586
@@ -91,6 +92,6 @@ def load_custom_writeObject(self, parser, reader, name):
9192
:param parser: The JavaStreamParser in use
9293
:param reader: The data stream reader
9394
:param name: The class description name
94-
:return: An array with the parsed fields or None
95+
:return: A JavaClassDesc instance or None
9596
"""
9697
return None

javaobj/v2/beans.py

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,17 @@ class ClassDataType(IntEnum):
7171
OBJECT_ANNOTATION = 3
7272

7373

74+
class ClassDataType(IntEnum):
75+
"""
76+
Class data types
77+
"""
78+
79+
NOWRCLASS = 0
80+
WRCLASS = 1
81+
EXTERNAL_CONTENTS = 2
82+
OBJECT_ANNOTATION = 3
83+
84+
7485
class ClassDescType(IntEnum):
7586
"""
7687
Types of class descriptions
@@ -96,6 +107,10 @@ class FieldType(IntEnum):
96107
ARRAY = TypeCode.TYPE_ARRAY.value
97108
OBJECT = TypeCode.TYPE_OBJECT.value
98109

110+
def type_code(self):
111+
# type: () -> TypeCode
112+
return TypeCode(self.value)
113+
99114

100115
class ParsedJavaContent(object):
101116
"""
@@ -200,7 +215,7 @@ def __init__(self, field_type, name, class_name=None):
200215
# type: (FieldType, str, Optional[JavaString]) -> None
201216
self.type = field_type
202217
self.name = name
203-
self.class_name = class_name # type: JavaString
218+
self.class_name = class_name
204219
self.is_inner_class_reference = False
205220

206221
if self.class_name:
@@ -318,13 +333,20 @@ def fields_types(self):
318333

319334
@property
320335
def data_type(self):
321-
if (ClassDescFlags.SC_SERIALIZABLE & self.desc_flags):
322-
return ClassDataType.WRCLASS if (ClassDescFlags.SC_WRITE_METHOD & self.desc_flags) else ClassDataType.NOWRCLASS
323-
elif (ClassDescFlags.SC_EXTERNALIZABLE & self.desc_flags):
324-
return ClassDataType.OBJECT_ANNOTATION if (ClassDescFlags.SC_WRITE_METHOD & self.desc_flags) else ClassDataType.EXTERNAL_CONTENTS
325-
336+
if ClassDescFlags.SC_SERIALIZABLE & self.desc_flags:
337+
return (
338+
ClassDataType.WRCLASS
339+
if (ClassDescFlags.SC_WRITE_METHOD & self.desc_flags)
340+
else ClassDataType.NOWRCLASS
341+
)
342+
elif ClassDescFlags.SC_EXTERNALIZABLE & self.desc_flags:
343+
return (
344+
ClassDataType.OBJECT_ANNOTATION
345+
if (ClassDescFlags.SC_WRITE_METHOD & self.desc_flags)
346+
else ClassDataType.EXTERNAL_CONTENTS
347+
)
348+
326349
raise ValueError("Unhandled Class Data Type")
327-
328350

329351
def is_array_class(self):
330352
# type: () -> bool

0 commit comments

Comments
 (0)