Skip to content

Commit c77d7ec

Browse files
author
vbuell
committed
TC_Exception test added
git-svn-id: http://python-javaobj.googlecode.com/svn/trunk@26 003243c8-0679-609b-391f-34f4f14b4f20
1 parent 4405aa6 commit c77d7ec

File tree

4 files changed

+63
-7
lines changed

4 files changed

+63
-7
lines changed

java/src/OneTest.java

+30
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,27 @@
1212
import org.junit.Before;
1313
import org.junit.Test;
1414

15+
class MyExceptionWhenDumping implements java.io.Serializable {
16+
private static class MyException extends java.io.IOException {
17+
};
18+
19+
public boolean anInstanceVar = false;
20+
21+
public MyExceptionWhenDumping() {
22+
super();
23+
}
24+
25+
private void readObject(java.io.ObjectInputStream in)
26+
throws java.io.IOException, ClassNotFoundException {
27+
in.defaultReadObject();
28+
}
29+
30+
private void writeObject(java.io.ObjectOutputStream out)
31+
throws java.io.IOException, ClassNotFoundException {
32+
throw new MyException();
33+
}
34+
}
35+
1536
enum Color
1637
{
1738
RED("RED"), GREEN("GREEN"), BLUE("BLUE"), UNKNOWN("UNKNOWN");
@@ -197,6 +218,15 @@ public void testEnums() throws Exception {
197218
oos.flush();
198219
}
199220

221+
@Test
222+
public void testException() throws Exception {
223+
oos = new ObjectOutputStream(fos = new FileOutputStream("objException.ser"));
224+
MyExceptionWhenDumping ts = new MyExceptionWhenDumping();
225+
226+
oos.writeObject(ts);
227+
oos.flush();
228+
}
229+
200230
// public void test_readObject() throws Exception {
201231
// String s = "HelloWorld";
202232
// oos.writeObject(s);

javaobj.py

+23-7
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ class JavaObject(object):
7575
def get_class(self):
7676
return self.classdesc
7777

78+
def __str__(self):
79+
return self.__repr__()
80+
81+
def __repr__(self):
82+
name = "UNKNOWN"
83+
if self.classdesc:
84+
name = self.classdesc.name
85+
return "<javaobj:%s>" % name
7886

7987
class JavaObjectConstants:
8088

@@ -232,21 +240,23 @@ def do_classdesc(self, parent=None, ident=0):
232240
clazz.fields_names = []
233241
clazz.fields_types = []
234242
for fieldId in range(length):
235-
(type, ) = self._readStruct(">B")
243+
(typecode, ) = self._readStruct(">B")
236244
field_name = self._readString()
237245
field_type = None
238-
field_type = self._convert_char_to_type(type)
246+
field_type = self._convert_char_to_type(typecode)
239247

240248
if field_type == self.TYPE_ARRAY:
241249
field_type = self._read_and_exec_opcode(ident=ident+1, expect=[self.TC_STRING, self.TC_REFERENCE])
250+
assert type(field_type) is str
242251
# if field_type is not None:
243252
# field_type = "array of " + field_type
244253
# else:
245254
# field_type = "array of None"
246255
elif field_type == self.TYPE_OBJECT:
247256
field_type = self._read_and_exec_opcode(ident=ident+1, expect=[self.TC_STRING, self.TC_REFERENCE])
257+
assert type(field_type) is str
248258

249-
log_debug("FieldName: 0x%X" % type + " " + str(field_name) + " " + str(field_type), ident)
259+
log_debug("FieldName: 0x%X" % typecode + " " + str(field_name) + " " + str(field_type), ident)
250260
assert field_name is not None
251261
assert field_type is not None
252262

@@ -257,9 +267,9 @@ def do_classdesc(self, parent=None, ident=0):
257267
parent.__types = clazz.fields_types
258268
# classAnnotation
259269
(opid, ) = self._readStruct(">B")
270+
log_debug("OpCode: 0x%X" % opid, ident)
260271
if opid != self.TC_ENDBLOCKDATA:
261272
raise NotImplementedError("classAnnotation isn't implemented yet")
262-
log_debug("OpCode: 0x%X" % opid, ident)
263273
# superClassDesc
264274
superclassdesc = self._read_and_exec_opcode(ident=ident+1, expect=[self.TC_CLASSDESC, self.TC_NULL, self.TC_REFERENCE])
265275
log_debug(str(superclassdesc), ident)
@@ -310,6 +320,7 @@ def do_object(self, parent=None, ident=0):
310320
megatypes = []
311321
while tempclass:
312322
log_debug(">>> " + str(tempclass.fields_names) + " " + str(tempclass), ident)
323+
log_debug(">>> " + str(tempclass.fields_types), ident)
313324
fieldscopy = tempclass.fields_names[:]
314325
fieldscopy.extend(megalist)
315326
megalist = fieldscopy
@@ -331,12 +342,17 @@ def do_object(self, parent=None, ident=0):
331342
if classdesc.flags & self.SC_SERIALIZABLE and classdesc.flags & self.SC_WRITE_METHOD or classdesc.flags & self.SC_EXTERNALIZABLE and classdesc.flags & self.SC_BLOCK_DATA:
332343
# objectAnnotation
333344
(opid, ) = self._readStruct(">B")
334-
if opid != self.TC_ENDBLOCKDATA: # 0x78:
345+
log_debug("OpCode: 0x%X" % opid, ident)
346+
if opid == self.TC_ENDBLOCKDATA: # 0x78:
347+
log_debug("TC_ENDBLOCKDATA: no object annotation")
348+
elif opid == self.TC_OBJECT:
349+
self.object_stream.seek(-1, mode=1)
350+
obj = self._read_and_exec_opcode(ident=ident+1, expect=[self.TC_OBJECT, self.TC_NULL, self.TC_REFERENCE])
351+
log_debug("objectAnnotation: " + str(obj))
352+
else:
335353
self.object_stream.seek(-1, mode=1)
336-
# print self._create_hexdump(self.object_stream.read())
337354
raise NotImplementedError("objectAnnotation isn't fully implemented yet") # TODO:
338355

339-
340356
return java_object
341357

342358
def do_string(self, parent=None, ident=0):

objException.ser

2.42 KB
Binary file not shown.

tests.py

+10
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,16 @@ def test_enums(self):
137137
print classdesc.fields_names
138138
print classdesc.fields_types
139139

140+
def test_exception(self):
141+
jobj = self.read_file("objException.ser")
142+
pobj = javaobj.loads(jobj)
143+
print pobj
144+
145+
classdesc = pobj.get_class()
146+
print classdesc
147+
print classdesc.fields_names
148+
print classdesc.fields_types
149+
140150
def test_sun_example(self):
141151
marshaller = javaobj.JavaObjectUnmarshaller(open("sunExample.ser"))
142152
pobj = marshaller.readObject()

0 commit comments

Comments
 (0)