Skip to content

Commit 8c17de0

Browse files
author
vbuell
committed
superclasses supported. more checking and warnings.
1 parent 5bd3b2b commit 8c17de0

File tree

4 files changed

+112
-12
lines changed

4 files changed

+112
-12
lines changed

java/src/OneTest.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,24 @@
1212
import org.junit.Before;
1313
import org.junit.Test;
1414

15+
class SuperAaaa implements Serializable {
16+
17+
public String superString = "Super!!";
18+
public int integer = -1;
19+
public boolean bool = true;
20+
21+
}
22+
23+
class TestConcrete extends SuperAaaa implements Serializable {
24+
25+
public String childString = "Child!!";
26+
27+
TestConcrete() {
28+
super();
29+
}
30+
31+
}
32+
1533
public class OneTest {
1634

1735
ObjectOutputStream oos;
@@ -64,7 +82,7 @@ public void setText2(String s) {
6482
aField2 = s;
6583
}
6684
}
67-
85+
6886
public static class A1 implements Serializable {
6987
private static final long serialVersionUID = 5942584913446079661L;
7088
B1 b1 = new B1();
@@ -138,6 +156,16 @@ public void windowClosing(WindowEvent e) {
138156
oos.writeObject(frame);
139157
oos.flush();
140158
}
159+
160+
@Test
161+
public void testSuper() throws Exception {
162+
oos = new ObjectOutputStream(fos = new FileOutputStream("objSuper.ser"));
163+
TestConcrete ts = new TestConcrete();
164+
165+
// ts.setChild("and Child!!!!");
166+
oos.writeObject(ts);
167+
oos.flush();
168+
}
141169

142170
// public void test_readObject() throws Exception {
143171
// String s = "HelloWorld";

javaobj.py

Lines changed: 68 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ def __init__(self):
3434
self.flags = None
3535
self.fields_names = []
3636
self.fields_types = []
37+
self.superclass = None
3738

3839
def __str__(self):
3940
return "[%s:0x%X]" % (self.name, self.serialVersionUID)
@@ -102,7 +103,10 @@ def readObject(self):
102103

103104
the_rest = self.object_stream.read()
104105
if len(the_rest):
105-
print "Warning!!!!: Stream still has %s bytes left." % str(the_rest)
106+
print "Warning!!!!: Stream still has %s bytes left." % len(the_rest)
107+
print self.hexdump(the_rest)
108+
else:
109+
print "Ok!!!!"
106110

107111
return res
108112

@@ -166,9 +170,14 @@ def do_classdesc(self, parent=None, ident=0):
166170
field_type = "boolean"
167171
elif type == 0x5B: # '[': Array
168172
field_type = self.read_and_exec_opcode(ident=ident+1, expect=[self.TC_STRING, self.TC_REFERENCE])
169-
field_type = "array of " + field_type
173+
if field_type is not None:
174+
field_type = "array of " + field_type
175+
else:
176+
field_type = "array of None"
170177
elif type == 0x42: # 'B': Byte
171178
field_type = "byte"
179+
elif type == 0x46: # 'F': Float
180+
field_type = "float"
172181
elif type == 0x4C: # 'L': Object
173182
field_type = self.read_and_exec_opcode(ident=ident+1, expect=[self.TC_STRING, self.TC_REFERENCE])
174183
else:
@@ -186,7 +195,8 @@ def do_classdesc(self, parent=None, ident=0):
186195
self.print_ident("OpCode: 0x%X" % opid, ident)
187196
# superClassDesc
188197
superclassdesc = self.read_and_exec_opcode(ident=ident+1, expect=[self.TC_CLASSDESC, self.TC_NULL])
189-
print superclassdesc
198+
self.print_ident(str(superclassdesc), ident)
199+
clazz.superclass = superclassdesc
190200

191201
return clazz
192202

@@ -225,28 +235,79 @@ def do_object(self, parent=None, ident=0):
225235
pass
226236
else:
227237
raise NotImplementedError("only nowrclass is implemented.")
228-
for field_name in classdesc.fields_names:
229-
res = self.read_and_exec_opcode(ident=ident+1)
238+
# create megalist
239+
tempclass = classdesc
240+
megalist = []
241+
megatypes = []
242+
while tempclass:
243+
print ">>>", tempclass.fields_names, tempclass
244+
fieldscopy = tempclass.fields_names[:]
245+
fieldscopy.extend(megalist)
246+
megalist = fieldscopy
247+
248+
fieldscopy = tempclass.fields_types[:]
249+
fieldscopy.extend(megatypes)
250+
megatypes = fieldscopy
251+
252+
tempclass = tempclass.superclass
253+
254+
print "Prepared list of values:", megalist
255+
print "Prepared list of types:", megatypes
256+
257+
for field_name, field_type in zip(megalist, megatypes):
258+
if field_type == "boolean":
259+
(val, ) = self._readStruct(">B", 1)
260+
res = bool(val)
261+
elif field_type == "byte":
262+
(res, ) = self._readStruct(">b", 1)
263+
elif field_type == "integer":
264+
(res, ) = self._readStruct(">i", 4)
265+
elif field_type == "long":
266+
(res, ) = self._readStruct(">q", 8)
267+
elif field_type == "float":
268+
(res, ) = self._readStruct(">f", 4)
269+
elif field_type == "double":
270+
(res, ) = self._readStruct(">d", 8)
271+
else:
272+
res = self.read_and_exec_opcode(ident=ident+1)
230273
java_object.__setattr__(field_name, res)
231274
return java_object
232275

276+
# field_type = "double"
277+
# field_type = "long"
278+
# field_type = "array of " + field_type
279+
280+
233281
def do_string(self, parent=None, ident=0):
234282
self.print_ident("[string]", ident)
235283
ba = self._readString()
236-
# (handle, ) = self._readStruct(">B", 1)
237284
return str(ba)
238285

239286
def do_reference(self, parent=None, ident=0):
287+
# TODO: Reference isn't supported yed
240288
(handle, reference) = self._readStruct(">HH", 4)
289+
print "## Reference:", handle, reference
290+
# raise NotImplementedError("Reference isn't supported yed.")
241291

242292
def do_null(self, parent=None, ident=0):
243293
return None
244294

245295
def do_default_stuff(self, parent=None, ident=0):
246-
raise RuntimeError("Unknown opcode")
296+
raise RuntimeError("Unknown OpCode")
247297

248298
def print_ident(self, message, ident):
249299
print " " * ident + str(message)
300+
301+
def hexdump(self, src, length=16):
302+
FILTER=''.join([(len(repr(chr(x)))==3) and chr(x) or '.' for x in range(256)])
303+
result = []
304+
for i in xrange(0, len(src), length):
305+
s = src[i:i+length]
306+
hexa = ' '.join(["%02X"%ord(x) for x in s])
307+
printable = s.translate(FILTER)
308+
result.append("%04X %-*s %s\n" % (i, length*3, hexa, printable))
309+
return ''.join(result)
310+
250311
# =====================================================================================
251312

252313
def dump(self, obj):

objSuper.ser

153 Bytes
Binary file not shown.

tests.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ def test_4(self):
4747
jobj = self.read_file("obj4.ser")
4848
pobj = javaobj.loads(jobj)
4949
print pobj
50-
self.assertEqual(pobj, 'HelloWorld')
50+
self.assertEqual(pobj, 127)
51+
5152
jobj_ = javaobj.dumps(pobj)
5253
self.assertEqual(jobj, jobj_)
5354

@@ -91,9 +92,19 @@ def test_7(self):
9192
print classdesc.fields_names
9293
print classdesc.fields_types
9394

94-
# def test_choice(self):
95-
# element = random.choice(self.seq)
96-
# self.assertTrue(element in self.seq)
95+
def test_super(self):
96+
jobj = self.read_file("objSuper.ser")
97+
pobj = javaobj.loads(jobj)
98+
print pobj
99+
100+
classdesc = pobj.get_class()
101+
print classdesc
102+
print classdesc.fields_names
103+
print classdesc.fields_types
104+
105+
print pobj.childString
106+
print pobj.bool
107+
print pobj.integer
97108

98109
if __name__ == '__main__':
99110
unittest.main()

0 commit comments

Comments
 (0)