Skip to content

Commit b711479

Browse files
Fede Atcalmant
authored andcommitted
adds tests for custom writeObject
1 parent 776e894 commit b711479

File tree

5 files changed

+214
-106
lines changed

5 files changed

+214
-106
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ Many thanks to the contributors:
1111
* Jason Spencer, Google LLC (@j8spencer)
1212
* @guywithface
1313
* Chris van Marle (@qistoph)
14+
* Federico Alves (@UruDev)

javaobj/v2/api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
# ------------------------------------------------------------------------------
4545

4646

47-
class ObjectTransformer:
47+
class ObjectTransformer(object):
4848
"""
4949
Representation of an object transformer
5050
"""

javaobj/v2/core.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,9 @@ def _do_classdesc(self, type_code):
395395
class_desc.annotations = self._read_class_annotations(class_desc)
396396
class_desc.super_class = self._read_classdesc()
397397

398+
if class_desc.super_class:
399+
class_desc.super_class.is_super_class = True
400+
398401
# Store the reference to the parsed bean
399402
self._set_handle(handle, class_desc)
400403
return class_desc
@@ -405,7 +408,8 @@ def _do_classdesc(self, type_code):
405408
# Reference to an already loading class description
406409
previous = self._do_reference()
407410
if not isinstance(previous, JavaClassDesc):
408-
raise ValueError("Referenced object is not a class description")
411+
raise ValueError(
412+
"Referenced object is not a class description")
409413
return previous
410414
elif type_code == TerminalCode.TC_PROXYCLASSDESC:
411415
# Proxy class description
@@ -421,6 +425,9 @@ def _do_classdesc(self, type_code):
421425
class_desc.annotations = self._read_class_annotations()
422426
class_desc.super_class = self._read_classdesc()
423427

428+
if class_desc.super_class:
429+
class_desc.super_class.is_super_class = True
430+
424431
# Store the reference to the parsed bean
425432
self._set_handle(handle, class_desc)
426433
return class_desc
@@ -481,6 +488,9 @@ def _create_instance(self, class_desc):
481488
for transformer in self.__transformers:
482489
instance = transformer.create_instance(class_desc)
483490
if instance is not None:
491+
if class_desc.name:
492+
instance.is_external_instance = not self._is_default_supported(
493+
class_desc.name)
484494
return instance
485495

486496
return JavaInstance()
@@ -546,14 +556,8 @@ def _read_class_data(self, instance):
546556
cd.data_type == ClassDataType.NOWRCLASS
547557
or cd.data_type == ClassDataType.WRCLASS
548558
):
549-
read_custom_data = (
550-
cd.data_type == ClassDataType.WRCLASS
551-
and cd.is_super_class
552-
and not self._is_default_supported(cd.name)
553-
)
554559
if (
555-
read_custom_data
556-
or cd.data_type == ClassDataType.WRCLASS
560+
cd.data_type == ClassDataType.WRCLASS
557561
and instance.is_external_instance
558562
):
559563
annotations[cd] = self._read_class_annotations(cd)

tests/java/src/test/java/OneTest.java

Lines changed: 51 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Set;
2222
import java.util.TreeSet;
2323
import java.util.Vector;
24+
import java.util.Random;
2425

2526
import javax.swing.JScrollPane;
2627
import javax.swing.SwingUtilities;
@@ -326,7 +327,7 @@ public void testTime() throws Exception {
326327
ZonedDateTime.now(),
327328
});
328329
oos.flush();
329-
}
330+
}
330331

331332
/**
332333
* Tests th pull request #27 by @qistoph:
@@ -388,115 +389,70 @@ public void windowClosing(final WindowEvent e) {
388389
});
389390
}
390391

391-
// public void test_readObject() throws Exception {
392-
// String s = "HelloWorld";
393-
// oos.writeObject(s);
394-
// oos.close();
395-
// ois = new ObjectInputStream(new ByteArrayInputStream(bao.toByteArray()));
396-
// assertEquals("Read incorrect Object value", s, ois.readObject());
397-
// ois.close();
398-
//
399-
// // Regression for HARMONY-91
400-
// // dynamically create serialization byte array for the next hierarchy:
401-
// // - class A implements Serializable
402-
// // - class C extends A
403-
//
404-
// byte[] cName = C.class.getName().getBytes("UTF-8");
405-
// byte[] aName = A.class.getName().getBytes("UTF-8");
406-
//
407-
// ByteArrayOutputStream out = new ByteArrayOutputStream();
408-
//
409-
// byte[] begStream = new byte[] { (byte) 0xac, (byte) 0xed, // STREAM_MAGIC
410-
// (byte) 0x00, (byte) 0x05, // STREAM_VERSION
411-
// (byte) 0x73, // TC_OBJECT
412-
// (byte) 0x72, // TC_CLASSDESC
413-
// (byte) 0x00, // only first byte for C class name length
414-
// };
415-
//
416-
// out.write(begStream, 0, begStream.length);
417-
// out.write(cName.length); // second byte for C class name length
418-
// out.write(cName, 0, cName.length); // C class name
419-
//
420-
// byte[] midStream = new byte[] { (byte) 0x00, (byte) 0x00, (byte) 0x00,
421-
// (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
422-
// (byte) 0x21, // serialVersionUID = 33L
423-
// (byte) 0x02, // flags
424-
// (byte) 0x00, (byte) 0x00, // fields : none
425-
// (byte) 0x78, // TC_ENDBLOCKDATA
426-
// (byte) 0x72, // Super class for C: TC_CLASSDESC for A class
427-
// (byte) 0x00, // only first byte for A class name length
428-
// };
429-
//
430-
// out.write(midStream, 0, midStream.length);
431-
// out.write(aName.length); // second byte for A class name length
432-
// out.write(aName, 0, aName.length); // A class name
433-
//
434-
// byte[] endStream = new byte[] { (byte) 0x00, (byte) 0x00, (byte) 0x00,
435-
// (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
436-
// (byte) 0x0b, // serialVersionUID = 11L
437-
// (byte) 0x02, // flags
438-
// (byte) 0x00, (byte) 0x01, // fields
439-
//
440-
// (byte) 0x4c, // field description: type L (object)
441-
// (byte) 0x00, (byte) 0x04, // length
442-
// // field = 'name'
443-
// (byte) 0x6e, (byte) 0x61, (byte) 0x6d, (byte) 0x65,
444-
//
445-
// (byte) 0x74, // className1: TC_STRING
446-
// (byte) 0x00, (byte) 0x12, // length
447-
// //
448-
// (byte) 0x4c, (byte) 0x6a, (byte) 0x61, (byte) 0x76,
449-
// (byte) 0x61, (byte) 0x2f, (byte) 0x6c, (byte) 0x61,
450-
// (byte) 0x6e, (byte) 0x67, (byte) 0x2f, (byte) 0x53,
451-
// (byte) 0x74, (byte) 0x72, (byte) 0x69, (byte) 0x6e,
452-
// (byte) 0x67, (byte) 0x3b,
453-
//
454-
// (byte) 0x78, // TC_ENDBLOCKDATA
455-
// (byte) 0x70, // NULL super class for A class
456-
//
457-
// // classdata
458-
// (byte) 0x74, // TC_STRING
459-
// (byte) 0x00, (byte) 0x04, // length
460-
// (byte) 0x6e, (byte) 0x61, (byte) 0x6d, (byte) 0x65, // value
461-
// };
462-
//
463-
// out.write(endStream, 0, endStream.length);
464-
// out.flush();
465-
//
466-
// // read created serial. form
467-
// ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(
468-
// out.toByteArray()));
469-
// Object o = ois.readObject();
470-
// assertEquals(C.class, o.getClass());
471-
//
472-
// // Regression for HARMONY-846
473-
// assertNull(new ObjectInputStream() {}.readObject());
474-
// }
475-
392+
393+
/**
394+
* Tests the pull request #38 by @UruDev:
395+
* Add support for custom writeObject
396+
*/
397+
@Test
398+
public void testCustomWriteObject() throws Exception {
399+
CustomClass writer = new CustomClass();
400+
writer.start(oos);
401+
}
476402
}
477403

478404
class SuperAaaa implements Serializable {
479-
480-
/**
481-
*
482-
*/
483405
private static final long serialVersionUID = 1L;
484406
public boolean bool = true;
485407
public int integer = -1;
486408
public String superString = "Super!!";
487-
488409
}
489410

490411
class TestConcrete extends SuperAaaa implements Serializable {
491-
492-
/**
493-
*
494-
*/
495412
private static final long serialVersionUID = 1L;
496413
public String childString = "Child!!";
497414

498415
TestConcrete() {
499416
super();
500417
}
418+
}
419+
420+
//Custom writeObject section
421+
class CustomClass implements Serializable {
422+
private static final long serialVersionUID = 1;
423+
424+
public void start(ObjectOutputStream out) throws Exception {
425+
this.writeObject(out);
426+
}
427+
428+
private void writeObject(ObjectOutputStream out) throws IOException {
429+
CustomWriter custom = new CustomWriter(42);
430+
out.writeObject(custom);
431+
out.flush();
432+
}
433+
}
434+
435+
class RandomChild extends Random {
436+
private static final long serialVersionUID = 1;
437+
private int num = 1;
438+
private double doub = 4.5;
501439

440+
RandomChild(int seed) {
441+
super(seed);
442+
}
443+
}
444+
445+
class CustomWriter implements Serializable {
446+
protected RandomChild custom_obj = null;
447+
448+
CustomWriter(int seed) {
449+
custom_obj = new RandomChild(seed);
450+
}
451+
452+
private static final long serialVersionUID = 1;
453+
private static final int CURRENT_SERIAL_VERSION = 0;
454+
private void writeObject(ObjectOutputStream out) throws IOException {
455+
out.writeInt(CURRENT_SERIAL_VERSION);
456+
out.writeObject(custom_obj);
457+
}
502458
}

0 commit comments

Comments
 (0)