Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ Many thanks to the contributors:
* Jason Spencer, Google LLC (@j8spencer)
* @guywithface
* Chris van Marle (@qistoph)
* Federico Alves (@UruDev)
6 changes: 3 additions & 3 deletions javaobj/v2/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
# ------------------------------------------------------------------------------


class ObjectTransformer:
class ObjectTransformer(object):
"""
Representation of an object transformer
"""
Expand Down Expand Up @@ -80,7 +80,7 @@ def load_array(self, reader, type_code, size):
return None

def load_custom_writeObject(self, parser, reader, name):
# type: (JavaStreamParser, DataStreamReader, str) -> Optional[list]
# type: (JavaStreamParser, DataStreamReader, str) -> Optional[JavaClassDesc]
"""
Reads content stored from a custom writeObject.

Expand All @@ -92,6 +92,6 @@ def load_custom_writeObject(self, parser, reader, name):
:param parser: The JavaStreamParser in use
:param reader: The data stream reader
:param name: The class description name
:return: An array with the parsed fields or None
:return: A JavaClassDesc instance or None
"""
return None
11 changes: 10 additions & 1 deletion javaobj/v2/beans.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ class ContentType(IntEnum):
BLOCKDATA = 6
EXCEPTIONSTATE = 7

class ClassDataType(IntEnum):
"""
Class data types
"""

NOWRCLASS = 0
WRCLASS = 1
EXTERNAL_CONTENTS = 2
OBJECT_ANNOTATION = 3


class ClassDataType(IntEnum):
"""
Expand Down Expand Up @@ -196,7 +206,6 @@ def __hash__(self):
def __eq__(self, other):
return self.value == other


class JavaField:
"""
Represents a field in a Java class description
Expand Down
22 changes: 12 additions & 10 deletions javaobj/v2/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,6 @@ def _do_classdesc(self, type_code):
handle = self._new_handle()
desc_flags = self.__reader.read_byte()
nb_fields = self.__reader.read_short()

if nb_fields < 0:
raise ValueError("Invalid field count: {0}".format(nb_fields))

Expand Down Expand Up @@ -395,6 +394,9 @@ def _do_classdesc(self, type_code):
class_desc.annotations = self._read_class_annotations(class_desc)
class_desc.super_class = self._read_classdesc()

if class_desc.super_class:
class_desc.super_class.is_super_class = True

# Store the reference to the parsed bean
self._set_handle(handle, class_desc)
return class_desc
Expand All @@ -405,7 +407,8 @@ def _do_classdesc(self, type_code):
# Reference to an already loading class description
previous = self._do_reference()
if not isinstance(previous, JavaClassDesc):
raise ValueError("Referenced object is not a class description")
raise ValueError(
"Referenced object is not a class description")
return previous
elif type_code == TerminalCode.TC_PROXYCLASSDESC:
# Proxy class description
Expand All @@ -421,6 +424,9 @@ def _do_classdesc(self, type_code):
class_desc.annotations = self._read_class_annotations()
class_desc.super_class = self._read_classdesc()

if class_desc.super_class:
class_desc.super_class.is_super_class = True

# Store the reference to the parsed bean
self._set_handle(handle, class_desc)
return class_desc
Expand Down Expand Up @@ -461,7 +467,6 @@ def _read_class_annotations(self, class_desc=None):
# Reset references
self._reset()
continue

java_object = self._read_content(type_code, True, class_desc)

if java_object is not None and java_object.is_exception:
Expand All @@ -481,6 +486,9 @@ def _create_instance(self, class_desc):
for transformer in self.__transformers:
instance = transformer.create_instance(class_desc)
if instance is not None:
if class_desc.name:
instance.is_external_instance = not self._is_default_supported(
class_desc.name)
return instance

return JavaInstance()
Expand Down Expand Up @@ -546,14 +554,8 @@ def _read_class_data(self, instance):
cd.data_type == ClassDataType.NOWRCLASS
or cd.data_type == ClassDataType.WRCLASS
):
read_custom_data = (
cd.data_type == ClassDataType.WRCLASS
and cd.is_super_class
and not self._is_default_supported(cd.name)
)
if (
read_custom_data
or cd.data_type == ClassDataType.WRCLASS
cd.data_type == ClassDataType.WRCLASS
and instance.is_external_instance
):
annotations[cd] = self._read_class_annotations(cd)
Expand Down
146 changes: 51 additions & 95 deletions tests/java/src/test/java/OneTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Set;
import java.util.TreeSet;
import java.util.Vector;
import java.util.Random;

import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
Expand Down Expand Up @@ -326,7 +327,7 @@ public void testTime() throws Exception {
ZonedDateTime.now(),
});
oos.flush();
}
}

/**
* Tests th pull request #27 by @qistoph:
Expand Down Expand Up @@ -388,115 +389,70 @@ public void windowClosing(final WindowEvent e) {
});
}

// public void test_readObject() throws Exception {
// String s = "HelloWorld";
// oos.writeObject(s);
// oos.close();
// ois = new ObjectInputStream(new ByteArrayInputStream(bao.toByteArray()));
// assertEquals("Read incorrect Object value", s, ois.readObject());
// ois.close();
//
// // Regression for HARMONY-91
// // dynamically create serialization byte array for the next hierarchy:
// // - class A implements Serializable
// // - class C extends A
//
// byte[] cName = C.class.getName().getBytes("UTF-8");
// byte[] aName = A.class.getName().getBytes("UTF-8");
//
// ByteArrayOutputStream out = new ByteArrayOutputStream();
//
// byte[] begStream = new byte[] { (byte) 0xac, (byte) 0xed, // STREAM_MAGIC
// (byte) 0x00, (byte) 0x05, // STREAM_VERSION
// (byte) 0x73, // TC_OBJECT
// (byte) 0x72, // TC_CLASSDESC
// (byte) 0x00, // only first byte for C class name length
// };
//
// out.write(begStream, 0, begStream.length);
// out.write(cName.length); // second byte for C class name length
// out.write(cName, 0, cName.length); // C class name
//
// byte[] midStream = new byte[] { (byte) 0x00, (byte) 0x00, (byte) 0x00,
// (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
// (byte) 0x21, // serialVersionUID = 33L
// (byte) 0x02, // flags
// (byte) 0x00, (byte) 0x00, // fields : none
// (byte) 0x78, // TC_ENDBLOCKDATA
// (byte) 0x72, // Super class for C: TC_CLASSDESC for A class
// (byte) 0x00, // only first byte for A class name length
// };
//
// out.write(midStream, 0, midStream.length);
// out.write(aName.length); // second byte for A class name length
// out.write(aName, 0, aName.length); // A class name
//
// byte[] endStream = new byte[] { (byte) 0x00, (byte) 0x00, (byte) 0x00,
// (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
// (byte) 0x0b, // serialVersionUID = 11L
// (byte) 0x02, // flags
// (byte) 0x00, (byte) 0x01, // fields
//
// (byte) 0x4c, // field description: type L (object)
// (byte) 0x00, (byte) 0x04, // length
// // field = 'name'
// (byte) 0x6e, (byte) 0x61, (byte) 0x6d, (byte) 0x65,
//
// (byte) 0x74, // className1: TC_STRING
// (byte) 0x00, (byte) 0x12, // length
// //
// (byte) 0x4c, (byte) 0x6a, (byte) 0x61, (byte) 0x76,
// (byte) 0x61, (byte) 0x2f, (byte) 0x6c, (byte) 0x61,
// (byte) 0x6e, (byte) 0x67, (byte) 0x2f, (byte) 0x53,
// (byte) 0x74, (byte) 0x72, (byte) 0x69, (byte) 0x6e,
// (byte) 0x67, (byte) 0x3b,
//
// (byte) 0x78, // TC_ENDBLOCKDATA
// (byte) 0x70, // NULL super class for A class
//
// // classdata
// (byte) 0x74, // TC_STRING
// (byte) 0x00, (byte) 0x04, // length
// (byte) 0x6e, (byte) 0x61, (byte) 0x6d, (byte) 0x65, // value
// };
//
// out.write(endStream, 0, endStream.length);
// out.flush();
//
// // read created serial. form
// ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(
// out.toByteArray()));
// Object o = ois.readObject();
// assertEquals(C.class, o.getClass());
//
// // Regression for HARMONY-846
// assertNull(new ObjectInputStream() {}.readObject());
// }


/**
* Tests the pull request #38 by @UruDev:
* Add support for custom writeObject
*/
@Test
public void testCustomWriteObject() throws Exception {
CustomClass writer = new CustomClass();
writer.start(oos);
}
}

class SuperAaaa implements Serializable {

/**
*
*/
private static final long serialVersionUID = 1L;
public boolean bool = true;
public int integer = -1;
public String superString = "Super!!";

}

class TestConcrete extends SuperAaaa implements Serializable {

/**
*
*/
private static final long serialVersionUID = 1L;
public String childString = "Child!!";

TestConcrete() {
super();
}
}

//Custom writeObject section
class CustomClass implements Serializable {
private static final long serialVersionUID = 1;

public void start(ObjectOutputStream out) throws Exception {
this.writeObject(out);
}

private void writeObject(ObjectOutputStream out) throws IOException {
CustomWriter custom = new CustomWriter(42);
out.writeObject(custom);
out.flush();
}
}

class RandomChild extends Random {
private static final long serialVersionUID = 1;
private int num = 1;
private double doub = 4.5;

RandomChild(int seed) {
super(seed);
}
}

class CustomWriter implements Serializable {
protected RandomChild custom_obj = null;

CustomWriter(int seed) {
custom_obj = new RandomChild(seed);
}

private static final long serialVersionUID = 1;
private static final int CURRENT_SERIAL_VERSION = 0;
private void writeObject(ObjectOutputStream out) throws IOException {
out.writeInt(CURRENT_SERIAL_VERSION);
out.writeObject(custom_obj);
}
}
Loading