Skip to content

Commit 0a24bcb

Browse files
committed
Using new transformers with deserialize
Work in progress
1 parent f22a5fc commit 0a24bcb

File tree

6 files changed

+784
-221
lines changed

6 files changed

+784
-221
lines changed

javaobj/api.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Definition of the object transformer API
4+
"""
5+
6+
from typing import Optional
7+
8+
from .deserialize.beans import JavaClassDesc, JavaInstance
9+
10+
11+
class JavaStreamParser:
12+
pass
13+
14+
15+
class ObjectTransformer:
16+
"""
17+
Representation of an object transformer
18+
"""
19+
20+
def create(
21+
self,
22+
classdesc: JavaClassDesc,
23+
parser: Optional[JavaStreamParser] = None,
24+
) -> Optional[JavaInstance]:
25+
"""
26+
Transforms a parsed Java object into a Python object
27+
28+
:param classdesc: The description of a Java class
29+
:return: The Python form of the object, or the original JavaObject
30+
"""
31+
raise NotImplementedError

javaobj/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2000,7 +2000,7 @@ def do_period(self, unmarshaller, data):
20002000
}
20012001

20022002
def create(self, classdesc, unmarshaller=None):
2003-
# type: (JavaClass, JavaObjectUnmarshaller) -> JavaObject
2003+
# type: (JavaClassDesc, JavaObjectUnmarshaller) -> JavaObject
20042004
"""
20052005
Transforms a deserialized Java object into a Python object
20062006

javaobj/deserialize/beans.py

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
from typing import Any, Dict, List, Optional, Set
88
import logging
99

10-
from .constants import *
10+
from . import constants
11+
from .stream import DataStreamReader
1112
from ..modifiedutf8 import decode_modified_utf8
1213

1314

@@ -40,19 +41,19 @@ class FieldType(IntEnum):
4041
Types of class fields
4142
"""
4243

43-
BYTE = TYPE_BYTE
44-
CHAR = TYPE_CHAR
45-
DOUBLE = TYPE_DOUBLE
46-
FLOAT = TYPE_FLOAT
47-
INTEGER = TYPE_INTEGER
48-
LONG = TYPE_LONG
49-
SHORT = TYPE_SHORT
50-
BOOLEAN = TYPE_BOOLEAN
51-
ARRAY = TYPE_ARRAY
52-
OBJECT = TYPE_OBJECT
44+
BYTE = constants.TYPE_BYTE
45+
CHAR = constants.TYPE_CHAR
46+
DOUBLE = constants.TYPE_DOUBLE
47+
FLOAT = constants.TYPE_FLOAT
48+
INTEGER = constants.TYPE_INTEGER
49+
LONG = constants.TYPE_LONG
50+
SHORT = constants.TYPE_SHORT
51+
BOOLEAN = constants.TYPE_BOOLEAN
52+
ARRAY = constants.TYPE_ARRAY
53+
OBJECT = constants.TYPE_OBJECT
5354

5455

55-
class Content:
56+
class ParsedJavaContent:
5657
"""
5758
Generic representation of data parsed from the stream
5859
"""
@@ -69,12 +70,12 @@ def validate(self) -> None:
6970
pass
7071

7172

72-
class ExceptionState(Content):
73+
class ExceptionState(ParsedJavaContent):
7374
"""
7475
Representation of a failed parsing
7576
"""
7677

77-
def __init__(self, exception_object: Content, data: bytes):
78+
def __init__(self, exception_object: ParsedJavaContent, data: bytes):
7879
super().__init__(ContentType.EXCEPTIONSTATE)
7980
self.exception_object = exception_object
8081
self.stream_data = data
@@ -86,11 +87,11 @@ class ExceptionRead(Exception):
8687
Exception used to indicate that an exception object has been parsed
8788
"""
8889

89-
def __init__(self, content: Content):
90+
def __init__(self, content: ParsedJavaContent):
9091
self.exception_object = content
9192

9293

93-
class JavaString(Content):
94+
class JavaString(ParsedJavaContent):
9495
"""
9596
Represents a Java string
9697
"""
@@ -141,7 +142,7 @@ def validate(self, java_type: str) -> None:
141142
)
142143

143144

144-
class JavaClassDesc(Content):
145+
class JavaClassDesc(ParsedJavaContent):
145146
"""
146147
Represents the description of a class
147148
"""
@@ -168,7 +169,7 @@ def __init__(self, class_desc_type: ClassDescType):
168169
self.inner_classes: List[JavaClassDesc] = []
169170

170171
# List of annotations objects
171-
self.annotations: List[Content] = []
172+
self.annotations: List[ParsedJavaContent] = []
172173

173174
# The super class of this one, if any
174175
self.super_class: JavaClassDesc = None
@@ -221,7 +222,9 @@ def validate(self):
221222
"""
222223
Checks the validity of this class description
223224
"""
224-
serial_or_extern = SC_SERIALIZABLE | SC_EXTERNALIZABLE
225+
serial_or_extern = (
226+
constants.SC_SERIALIZABLE | constants.SC_EXTERNALIZABLE
227+
)
225228
if (self.desc_flags & serial_or_extern) == 0 and self.fields:
226229
raise ValueError(
227230
"Non-serializable, non-externalizable class has fields"
@@ -230,7 +233,7 @@ def validate(self):
230233
if self.desc_flags & serial_or_extern == serial_or_extern:
231234
raise ValueError("Class is both serializable and externalizable")
232235

233-
if self.desc_flags & SC_ENUM:
236+
if self.desc_flags & constants.SC_ENUM:
234237
if self.fields or self.interfaces:
235238
raise ValueError(
236239
"Enums shouldn't implement interfaces "
@@ -243,7 +246,7 @@ def validate(self):
243246
)
244247

245248

246-
class JavaInstance(Content):
249+
class JavaInstance(ParsedJavaContent):
247250
"""
248251
Represents an instance of Java object
249252
"""
@@ -252,7 +255,7 @@ def __init__(self):
252255
super().__init__(ContentType.INSTANCE)
253256
self.classdesc: JavaClassDesc = None
254257
self.field_data: Dict[JavaClassDesc, Dict[JavaField, Any]] = {}
255-
self.annotations: Dict[JavaClassDesc, List[Content]] = {}
258+
self.annotations: Dict[JavaClassDesc, List[ParsedJavaContent]] = {}
256259

257260
def __str__(self):
258261
return "[instance 0x{0:x}: type {1}]".format(
@@ -261,8 +264,24 @@ def __str__(self):
261264

262265
__repr__ = __str__
263266

267+
def load_from_blockdata(
268+
self, reader: DataStreamReader, indent: int = 0
269+
) -> bool:
270+
"""
271+
Reads content stored in a block data
272+
"""
273+
return False
274+
275+
def load_from_instance(
276+
self, instance: "JavaInstance", indent: int = 0
277+
) -> bool:
278+
"""
279+
Load content from a parsed instance object
280+
"""
281+
return False
282+
264283

265-
class JavaClass(Content):
284+
class JavaClass(ParsedJavaContent):
266285
"""
267286
Represents a stored Java class
268287
"""
@@ -278,7 +297,7 @@ def __str__(self):
278297
__repr__ = __str__
279298

280299

281-
class JavaEnum(Content):
300+
class JavaEnum(ParsedJavaContent):
282301
"""
283302
Represents an enumeration value
284303
"""
@@ -297,7 +316,7 @@ def __str__(self):
297316
__repr__ = __str__
298317

299318

300-
class JavaArray(Content):
319+
class JavaArray(ParsedJavaContent):
301320
"""
302321
Represents a Java array
303322
"""
@@ -323,7 +342,7 @@ def __str__(self):
323342
__repr__ = __str__
324343

325344

326-
class BlockData(Content):
345+
class BlockData(ParsedJavaContent):
327346
"""
328347
Represents a data block
329348
"""

0 commit comments

Comments
 (0)