You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by sd...@apache.org on 2022/01/11 23:56:29 UTC
[ignite-3] 01/27: - manually squash via patch
This is an automated email from the ASF dual-hosted git repository.
sdanilov pushed a commit to branch uos-network
in repository https://gitbox.apache.org/repos/asf/ignite-3.git
commit 255d9a6f9735a3e708f4e1d2885732be58c6e70d
Author: Roman Puchkovskiy <ro...@gmail.com>
AuthorDate: Mon Jan 10 18:28:28 2022 +0400
- manually squash via patch
---
.../network/message/ClassDescriptorMessage.java | 23 +
.../network/message/FieldDescriptorMessage.java | 5 -
.../network/serialization/BuiltinType.java | 8 +-
.../network/serialization/ClassDescriptor.java | 80 +-
.../serialization/ClassDescriptorFactory.java | 104 +--
.../PerSessionSerializationService.java | 43 +-
.../network/serialization/Serialization.java | 33 +-
.../serialization/SerializedStreamCommands.java | 2 +-
.../serialization/SpecialSerializationMethods.java | 29 +
.../SpecialSerializationMethodsImpl.java | 89 ++-
.../marshal/ArbitraryObjectMarshaller.java | 112 ++-
.../marshal/BuiltInContainerMarshallers.java | 27 +-
.../serialization/marshal/BuiltInMarshalling.java | 87 +--
.../marshal/BuiltInNonContainerMarshallers.java | 11 +-
...eWriter.java => DefaultFieldsReaderWriter.java} | 23 +-
.../marshal/DefaultUserObjectMarshaller.java | 111 +--
.../marshal/ExternalizableMarshaller.java | 48 +-
.../serialization/marshal/MarshallingContext.java | 46 ++
.../serialization/marshal/ProtocolMarshalling.java | 73 ++
.../serialization/marshal/TypedValueWriter.java | 8 +-
.../serialization/marshal/UnmarshalException.java | 4 +
.../marshal/UnmarshallingContext.java | 46 ++
.../marshal/UosObjectInputStream.java | 174 +++++
.../marshal/UosObjectOutputStream.java | 171 +++++
.../network/serialization/marshal/ValueReader.java | 8 +-
.../network/serialization/marshal/ValueWriter.java | 8 +-
.../serialization/ClassDescriptorFactoryTest.java | 84 ++-
.../serialization/DefaultDescriptorsTest.java | 2 -
.../DefaultUserObjectMarshallerCommonTest.java | 48 ++
...erObjectMarshallerWithArbitraryObjectsTest.java | 11 +
...efaultUserObjectMarshallerWithBuiltinsTest.java | 14 +-
...UserObjectMarshallerWithExternalizableTest.java | 303 +++++++-
...ltUserObjectMarshallerWithSerializableTest.java | 809 +++++++++++++++++++++
.../network/serialization/marshal/Throwables.java} | 22 +-
34 files changed, 2353 insertions(+), 313 deletions(-)
diff --git a/modules/network/src/main/java/org/apache/ignite/internal/network/message/ClassDescriptorMessage.java b/modules/network/src/main/java/org/apache/ignite/internal/network/message/ClassDescriptorMessage.java
index 8cf7745..1bf857b 100644
--- a/modules/network/src/main/java/org/apache/ignite/internal/network/message/ClassDescriptorMessage.java
+++ b/modules/network/src/main/java/org/apache/ignite/internal/network/message/ClassDescriptorMessage.java
@@ -24,6 +24,7 @@ import org.apache.ignite.internal.network.serialization.Serialization;
import org.apache.ignite.internal.network.serialization.SerializationType;
import org.apache.ignite.network.NetworkMessage;
import org.apache.ignite.network.annotations.Transferable;
+import org.jetbrains.annotations.Nullable;
/** Message for the {@link ClassDescriptor}. */
@Transferable(NetworkMessageTypes.CLASS_DESCRIPTOR_MESSAGE)
@@ -43,6 +44,21 @@ public interface ClassDescriptorMessage extends NetworkMessage {
int descriptorId();
/**
+ * Super-class name.
+ *
+ * @see ClassDescriptor#superClassDescriptor()
+ */
+ @Nullable
+ String superClassName();
+
+ /**
+ * Super-class descriptor ID. {@link Integer#MIN_VALUE} if super-class is missing.
+ *
+ * @see ClassDescriptor#superClassDescriptor()
+ */
+ int superClassDescriptorId();
+
+ /**
* List of the class fields' descriptors.
*
* @see ClassDescriptor#fields()
@@ -64,6 +80,13 @@ public interface ClassDescriptorMessage extends NetworkMessage {
boolean hasSerializationOverride();
/**
+ * Has readObjectNoData().
+ *
+ * @see Serialization#hasReadObjectNoData()
+ */
+ boolean hasReadObjectNoData();
+
+ /**
* Has writeReplace.
*
* @see Serialization#hasWriteReplace()
diff --git a/modules/network/src/main/java/org/apache/ignite/internal/network/message/FieldDescriptorMessage.java b/modules/network/src/main/java/org/apache/ignite/internal/network/message/FieldDescriptorMessage.java
index 1409544..aa7a3ec 100644
--- a/modules/network/src/main/java/org/apache/ignite/internal/network/message/FieldDescriptorMessage.java
+++ b/modules/network/src/main/java/org/apache/ignite/internal/network/message/FieldDescriptorMessage.java
@@ -39,9 +39,4 @@ public interface FieldDescriptorMessage extends NetworkMessage {
* Field's class name.
*/
String className();
-
- /**
- * The name of The class in which this field is declared.
- */
- String declaringClassName();
}
diff --git a/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/BuiltinType.java b/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/BuiltinType.java
index 46d22e5..c27c595 100644
--- a/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/BuiltinType.java
+++ b/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/BuiltinType.java
@@ -80,9 +80,8 @@ public enum BuiltinType {
HASH_MAP(40, HashMap.class),
LINKED_HASH_MAP(41, LinkedHashMap.class),
BIT_SET(42, BitSet.class),
- NULL(43, Null.class),
- VOID(44, Void.class)
- // 45 is REFERENCE command, see SerializedStreamCommands#REFERENCE
+ NULL(43, Null.class)
+ // 44 is REFERENCE command, see SerializedStreamCommands#REFERENCE
;
/**
@@ -125,7 +124,7 @@ public enum BuiltinType {
}
/**
- * Creates a descriptor for this default type.
+ * Creates a descriptor for this built-in type.
*
* @return Descriptor.
*/
@@ -133,6 +132,7 @@ public enum BuiltinType {
return new ClassDescriptor(
clazz,
descriptorId,
+ null,
Collections.emptyList(),
new Serialization(SerializationType.BUILTIN)
);
diff --git a/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/ClassDescriptor.java b/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/ClassDescriptor.java
index 36e8752..322ffcf 100644
--- a/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/ClassDescriptor.java
+++ b/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/ClassDescriptor.java
@@ -20,18 +20,13 @@ package org.apache.ignite.internal.network.serialization;
import java.lang.reflect.Modifier;
import java.util.List;
import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
/**
* Class descriptor for the user object serialization.
*/
public class ClassDescriptor {
/**
- * Name of the class.
- */
- @NotNull
- private final String className;
-
- /**
* Class.
*/
@NotNull
@@ -43,7 +38,13 @@ public class ClassDescriptor {
private final int descriptorId;
/**
- * List of the class fields' descriptors.
+ * Superclass descriptor (might be missing).
+ */
+ @Nullable
+ private final ClassDescriptor superClassDescriptor;
+
+ /**
+ * List of the declared class fields' descriptors.
*/
@NotNull
private final List<FieldDescriptor> fields;
@@ -63,10 +64,16 @@ public class ClassDescriptor {
/**
* Constructor.
*/
- public ClassDescriptor(@NotNull Class<?> clazz, int descriptorId, @NotNull List<FieldDescriptor> fields, Serialization serialization) {
- this.className = clazz.getName();
+ public ClassDescriptor(
+ @NotNull Class<?> clazz,
+ int descriptorId,
+ @Nullable ClassDescriptor superClassDescriptor,
+ @NotNull List<FieldDescriptor> fields,
+ Serialization serialization
+ ) {
this.clazz = clazz;
this.descriptorId = descriptorId;
+ this.superClassDescriptor = superClassDescriptor;
this.fields = List.copyOf(fields);
this.serialization = serialization;
this.isFinal = Modifier.isFinal(clazz.getModifiers());
@@ -84,7 +91,37 @@ public class ClassDescriptor {
}
/**
- * Returns fields' descriptors.
+ * Returns descriptor of the superclass of the described class (might be {@code null}).
+ *
+ * @return descriptor of the superclass of the described class (might be {@code null})
+ */
+ @Nullable
+ public ClassDescriptor superClassDescriptor() {
+ return superClassDescriptor;
+ }
+
+ /**
+ * Returns ID of the superclass descriptor (might be {@code null}).
+ *
+ * @return ID of the superclass descriptor (might be {@code null})
+ */
+ @Nullable
+ public Integer superClassDescriptorId() {
+ return superClassDescriptor == null ? null : superClassDescriptor.descriptorId();
+ }
+
+ /**
+ * Returns name of the superclass (might be {@code null}).
+ *
+ * @return name of the superclass (might be {@code null})
+ */
+ @Nullable
+ public String superClassName() {
+ return superClassDescriptor == null ? null : superClassDescriptor.className();
+ }
+
+ /**
+ * Returns declared fields' descriptors.
*
* @return Fields' descriptors.
*/
@@ -100,7 +137,7 @@ public class ClassDescriptor {
*/
@NotNull
public String className() {
- return className;
+ return clazz.getName();
}
/**
@@ -169,6 +206,15 @@ public class ClassDescriptor {
}
/**
+ * Returns {@code true} if the described class has writeObject() and readObject() methods.
+ *
+ * @return {@code true} if the described class has writeObject() and readObject() methods
+ */
+ public boolean hasSerializationOverride() {
+ return serialization.hasSerializationOverride();
+ }
+
+ /**
* Returns {@code true} if the described class has {@code writeReplace()} method.
*
* @return {@code true} if the described class has {@code writeReplace()} method
@@ -227,8 +273,18 @@ public class ClassDescriptor {
@Override
public String toString() {
return "ClassDescriptor{"
- + "className='" + className + '\''
+ + "className='" + className() + '\''
+ ", descriptorId=" + descriptorId
+ '}';
}
+
+ /**
+ * Returns {@code true} if this descriptor describes same class as the given descriptor.
+ *
+ * @param other a descriptor to match against
+ * @return {@code true} if this descriptor describes same class as the given descriptor
+ */
+ public boolean describesSameClass(ClassDescriptor other) {
+ return other.clazz() == clazz();
+ }
}
diff --git a/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/ClassDescriptorFactory.java b/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/ClassDescriptorFactory.java
index 0a3a4a9..d8559e3 100644
--- a/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/ClassDescriptorFactory.java
+++ b/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/ClassDescriptorFactory.java
@@ -29,12 +29,10 @@ import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayDeque;
-import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Queue;
-import java.util.stream.Stream;
import org.apache.ignite.lang.IgniteException;
import org.jetbrains.annotations.Nullable;
@@ -44,6 +42,8 @@ import org.jetbrains.annotations.Nullable;
public class ClassDescriptorFactory {
/** Means that no serialization override is present; used for readability instead of {@code false}. */
private static final boolean NO_SERIALIZATION_OVERRIDE = false;
+ /** Means that no readObjectNoData() method is present; used for readability instead of {@code false}. */
+ private static final boolean NO_READ_OBJECT_NO_DATA = false;
/**
* Factory context.
@@ -129,10 +129,12 @@ public class ClassDescriptorFactory {
return new ClassDescriptor(
clazz,
descriptorId,
+ superClassDescriptor(clazz),
Collections.emptyList(),
new Serialization(
SerializationType.EXTERNALIZABLE,
NO_SERIALIZATION_OVERRIDE,
+ NO_READ_OBJECT_NO_DATA,
hasWriteReplace(clazz),
hasReadResolve(clazz)
)
@@ -140,6 +142,22 @@ public class ClassDescriptorFactory {
}
/**
+ * If the given class has a super-class (which is not Object), parses the super-class and registers the resulting descriptor.
+ *
+ * @param clazz class which super-class to parse
+ * @return descriptor of the super-class or {@code null} if the class has no super-class or the super-class is Object
+ */
+ private ClassDescriptor superClassDescriptor(Class<?> clazz) {
+ Class<?> superclass = clazz.getSuperclass();
+
+ if (superclass == null || superclass == Object.class) {
+ return null;
+ }
+
+ return create(superclass);
+ }
+
+ /**
* Checks if a class has a public no-arg constructor.
*
* @param clazz Class.
@@ -171,19 +189,33 @@ public class ClassDescriptorFactory {
* @return Class descriptor.
*/
private ClassDescriptor serializable(int descriptorId, Class<? extends Serializable> clazz) {
+ validateSerializationOverride(clazz);
+
return new ClassDescriptor(
clazz,
descriptorId,
+ superClassDescriptor(clazz),
fields(clazz),
new Serialization(
SerializationType.SERIALIZABLE,
hasOverrideSerialization(clazz),
+ hasReadObjectNoData(clazz),
hasWriteReplace(clazz),
hasReadResolve(clazz)
)
);
}
+ private void validateSerializationOverride(Class<? extends Serializable> clazz) {
+ boolean hasReadObject = hasReadObject(clazz);
+ boolean hasWriteObject = hasWriteObject(clazz);
+
+ if (hasWriteObject != hasReadObject) {
+ throw new IllegalArgumentException("Class must either have both writeObject() and readObject() methods or neither of them: "
+ + clazz.getName());
+ }
+ }
+
private boolean hasReadResolve(Class<? extends Serializable> clazz) {
return getReadResolve(clazz) != null;
}
@@ -193,11 +225,19 @@ public class ClassDescriptorFactory {
}
private boolean hasOverrideSerialization(Class<? extends Serializable> clazz) {
- Method writeObject = getWriteObject(clazz);
- Method readObject = getReadObject(clazz);
- Method readObjectNoData = getReadObjectNoData(clazz);
+ return hasWriteObject(clazz) && hasReadObject(clazz);
+ }
+
+ private boolean hasReadObject(Class<? extends Serializable> clazz) {
+ return getReadObject(clazz) != null;
+ }
- return writeObject != null && readObject != null && readObjectNoData != null;
+ private boolean hasWriteObject(Class<? extends Serializable> clazz) {
+ return getWriteObject(clazz) != null;
+ }
+
+ private boolean hasReadObjectNoData(Class<? extends Serializable> clazz) {
+ return getReadObjectNoData(clazz) != null;
}
/**
@@ -208,50 +248,23 @@ public class ClassDescriptorFactory {
* @return Class descriptor.
*/
private ClassDescriptor arbitrary(int descriptorId, Class<?> clazz) {
- return new ClassDescriptor(clazz, descriptorId, fields(clazz), new Serialization(SerializationType.ARBITRARY));
- }
-
- /**
- * Gets field descriptors of the class in the correct order (see {@link #classFields(Class)}. If a field's type
- * doesn't have an id yet, generates it.
- *
- * @param clazz Class.
- * @return List of field descriptor.
- */
- private List<FieldDescriptor> fields(Class<?> clazz) {
- List<Class<?>> lineage = lineage(clazz);
-
- return lineage.stream()
- .flatMap(this::classFields)
- .collect(toList());
- }
-
- /**
- * Returns the lineage (all the ancestors, from Object down the line, including the given class).
- *
- * @param clazz class from which to obtain lineage
- * @return ancestors from Object down the line, plus the given class itself
- */
- private List<Class<?>> lineage(Class<?> clazz) {
- List<Class<?>> classes = new ArrayList<>();
-
- Class<?> currentClass = clazz;
- while (currentClass != null) {
- classes.add(currentClass);
- currentClass = currentClass.getSuperclass();
- }
-
- Collections.reverse(classes);
- return classes;
+ return new ClassDescriptor(
+ clazz,
+ descriptorId,
+ superClassDescriptor(clazz),
+ fields(clazz),
+ new Serialization(SerializationType.ARBITRARY)
+ );
}
/**
- * Returns 'serializable' (i.e. non-static non-transient) declared fields of the given class sorted lexicographically by their names.
+ * Returns descriptors of 'serializable' (i.e. non-static non-transient) declared fields of the given class
+ * sorted lexicographically by their names.
*
* @param clazz class
- * @return properly sorted fields
+ * @return properly sorted field descriptors
*/
- private Stream<FieldDescriptor> classFields(Class<?> clazz) {
+ private List<FieldDescriptor> fields(Class<?> clazz) {
return Arrays.stream(clazz.getDeclaredFields())
.sorted(comparing(Field::getName))
.filter(field -> {
@@ -260,7 +273,8 @@ public class ClassDescriptorFactory {
// Ignore static and transient fields.
return !Modifier.isStatic(modifiers) && !Modifier.isTransient(modifiers);
})
- .map(field -> new FieldDescriptor(field, context.getId(field.getType())));
+ .map(field -> new FieldDescriptor(field, context.getId(field.getType())))
+ .collect(toList());
}
/**
diff --git a/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/PerSessionSerializationService.java b/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/PerSessionSerializationService.java
index d8a546c..fb92bd0 100644
--- a/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/PerSessionSerializationService.java
+++ b/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/PerSessionSerializationService.java
@@ -31,6 +31,7 @@ import org.apache.ignite.network.NetworkMessage;
import org.apache.ignite.network.serialization.MessageDeserializer;
import org.apache.ignite.network.serialization.MessageSerializer;
import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.TestOnly;
/**
@@ -41,6 +42,9 @@ public class PerSessionSerializationService {
/** Network messages factory. */
private static final NetworkMessagesFactory MSG_FACTORY = new NetworkMessagesFactory();
+ /** Integer value that is sent when there is no descriptor. */
+ private static final int NO_DESCRIPTOR_ID = Integer.MIN_VALUE;
+
/** Global serialization service. */
@NotNull
private final SerializationService serializationService;
@@ -114,7 +118,6 @@ public class PerSessionSerializationService {
.name(d.name())
.typeDescriptorId(d.typeDescriptorId())
.className(d.clazz().getName())
- .declaringClassName(d.declaringClass().getName())
.build();
})
.collect(toList());
@@ -126,14 +129,27 @@ public class PerSessionSerializationService {
.isFinal(descriptor.isFinal())
.serializationType(serialization.type().value())
.hasSerializationOverride(serialization.hasSerializationOverride())
+ .hasReadObjectNoData(serialization.hasReadObjectNoData())
.hasWriteReplace(serialization.hasWriteReplace())
.hasReadResolve(serialization.hasReadResolve())
.descriptorId(descriptor.descriptorId())
.className(descriptor.className())
+ .superClassDescriptorId(superClassDescriptorIdForMessage(descriptor))
+ .superClassName(descriptor.superClassName())
.build();
}).collect(toList());
}
+ private int superClassDescriptorIdForMessage(ClassDescriptor descriptor) {
+ Integer id = descriptor.superClassDescriptorId();
+
+ if (id == null) {
+ return NO_DESCRIPTOR_ID;
+ }
+
+ return id;
+ }
+
private void mergeDescriptors(List<ClassDescriptorMessage> remoteDescriptors) {
for (ClassDescriptorMessage clsMsg : remoteDescriptors) {
int clsDescriptorId = clsMsg.descriptorId();
@@ -164,7 +180,7 @@ public class PerSessionSerializationService {
ClassDescriptor localDescriptor = serializationService.getClassDescriptor(clsMsg.className());
List<FieldDescriptor> remoteFields = clsMsg.fields().stream()
- .map(this::fieldDescriptorFromMessage)
+ .map(fieldMsg -> fieldDescriptorFromMessage(fieldMsg, localDescriptor.clazz()))
.collect(toList());
SerializationType serializationType = SerializationType.getByValue(clsMsg.serializationType());
@@ -172,6 +188,7 @@ public class PerSessionSerializationService {
var serialization = new Serialization(
serializationType,
clsMsg.hasSerializationOverride(),
+ clsMsg.hasReadObjectNoData(),
clsMsg.hasWriteReplace(),
clsMsg.hasReadResolve()
);
@@ -179,6 +196,7 @@ public class PerSessionSerializationService {
ClassDescriptor remoteDescriptor = new ClassDescriptor(
localDescriptor.clazz(),
clsMsg.descriptorId(),
+ superClassDescriptor(clsMsg),
remoteFields,
serialization
);
@@ -186,9 +204,16 @@ public class PerSessionSerializationService {
return mergeDescriptor(localDescriptor, remoteDescriptor);
}
- private FieldDescriptor fieldDescriptorFromMessage(FieldDescriptorMessage fieldMsg) {
+ @Nullable
+ private ClassDescriptor superClassDescriptor(ClassDescriptorMessage clsMsg) {
+ if (clsMsg.superClassDescriptorId() == NO_DESCRIPTOR_ID) {
+ return null;
+ }
+ return getClassDescriptor(clsMsg.superClassDescriptorId(), clsMsg.superClassName());
+ }
+
+ private FieldDescriptor fieldDescriptorFromMessage(FieldDescriptorMessage fieldMsg, Class<?> declaringClass) {
int typeDescriptorId = fieldMsg.typeDescriptorId();
- Class<?> declaringClass = serializationService.getClassDescriptor(fieldMsg.declaringClassName()).clazz();
return new FieldDescriptor(fieldMsg.name(), getClass(typeDescriptorId, fieldMsg.className()), typeDescriptorId, declaringClass);
}
@@ -197,14 +222,18 @@ public class PerSessionSerializationService {
return remoteDescriptor;
}
- private Class<?> getClass(int descriptorId, String typeName) {
+ private ClassDescriptor getClassDescriptor(int descriptorId, String typeName) {
if (serializationService.shouldBeBuiltIn(descriptorId)) {
- return serializationService.getClassDescriptor(descriptorId).clazz();
+ return serializationService.getClassDescriptor(descriptorId);
} else {
- return serializationService.getClassDescriptor(typeName).clazz();
+ return serializationService.getClassDescriptor(typeName);
}
}
+ private Class<?> getClass(int descriptorId, String typeName) {
+ return getClassDescriptor(descriptorId, typeName).getClass();
+ }
+
@TestOnly
Map<Integer, ClassDescriptor> getDescriptorMapView() {
return descriptorMapView;
diff --git a/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/Serialization.java b/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/Serialization.java
index 0f58c70..19e247a 100644
--- a/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/Serialization.java
+++ b/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/Serialization.java
@@ -24,8 +24,10 @@ public class Serialization {
/** Serialization type. */
private final SerializationType type;
- /** Whether a Serializable has writeObject()/readObject()/readObjectNoData() methods. */
+ /** Whether a Serializable has writeObject() + readObject() methods. */
private final boolean hasSerializationOverride;
+ /** Whether a Serializable has readObjectNoData() method. */
+ private final boolean hasReadObjectNoData;
/** Whether a Serializable/Externalizable has writeReplace() method. */
private final boolean hasWriteReplace;
/** Whether a Serializable/Externalizable has readResolve() method. */
@@ -35,17 +37,25 @@ public class Serialization {
* Creates a new Serialization.
*
* @param type type
- * @param hasSerializationOverride whether a Serializable has writeObject()/readObject()/readObjectNoData() methods
+ * @param hasSerializationOverride whether a Serializable has writeObject() + readObject() methods
+ * @param hasReadObjectNoData whether a Serializable has readObjectNoData() method
* @param hasWriteReplace whether a Serializable/Externalizable has writeReplace() method
* @param hasReadResolve whether a Serializable/Externalizable has readResolve() method
*/
- public Serialization(SerializationType type, boolean hasSerializationOverride, boolean hasWriteReplace, boolean hasReadResolve) {
+ public Serialization(
+ SerializationType type,
+ boolean hasSerializationOverride,
+ boolean hasReadObjectNoData,
+ boolean hasWriteReplace,
+ boolean hasReadResolve
+ ) {
assert type == SerializationType.SERIALIZABLE
- || (type == SerializationType.EXTERNALIZABLE && !hasSerializationOverride)
+ || (type == SerializationType.EXTERNALIZABLE && !hasSerializationOverride && !hasReadObjectNoData)
|| (!hasSerializationOverride && !hasWriteReplace && !hasReadResolve);
this.type = type;
this.hasSerializationOverride = hasSerializationOverride;
+ this.hasReadObjectNoData = hasReadObjectNoData;
this.hasWriteReplace = hasWriteReplace;
this.hasReadResolve = hasReadResolve;
}
@@ -56,7 +66,7 @@ public class Serialization {
* @param type serialization type
*/
public Serialization(SerializationType type) {
- this(type, false, false, false);
+ this(type, false, false, false, false);
}
/**
@@ -69,15 +79,24 @@ public class Serialization {
}
/**
- * Returns whether serialization override (writeObject()/readObject()/readObjectNoData()) is present.
+ * Returns whether serialization override (writeObject() + readObject()) is present.
*
- * @return whether serialization override (writeObject()/readObject()/readObjectNoData()) is present
+ * @return whether serialization override (writeObject() + readObject()) is present
*/
public boolean hasSerializationOverride() {
return hasSerializationOverride;
}
/**
+ * Returns whether readObjectNoData() method is present.
+ *
+ * @return whether readObjectNoData() method is present
+ */
+ public boolean hasReadObjectNoData() {
+ return hasReadObjectNoData;
+ }
+
+ /**
* Returns whether writeReplace() method is present.
*
* @return whether writeReplace() method is present
diff --git a/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/SerializedStreamCommands.java b/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/SerializedStreamCommands.java
index 1299519..b5f1af3 100644
--- a/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/SerializedStreamCommands.java
+++ b/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/SerializedStreamCommands.java
@@ -25,7 +25,7 @@ public class SerializedStreamCommands {
/**
* Reference: an object that was already seen in the graph, so we relate to it by its ID instead of serializing it again.
*/
- public static final int REFERENCE = 45;
+ public static final int REFERENCE = 44;
private SerializedStreamCommands() {
}
diff --git a/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/SpecialSerializationMethods.java b/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/SpecialSerializationMethods.java
index 66ac74b..b9e6453 100644
--- a/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/SpecialSerializationMethods.java
+++ b/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/SpecialSerializationMethods.java
@@ -17,6 +17,9 @@
package org.apache.ignite.internal.network.serialization;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+
/**
* Encapsulates special serialization methods like writeReplace()/readResolve() for convenient invocation.
*/
@@ -48,4 +51,30 @@ public interface SpecialSerializationMethods {
* @throws SpecialMethodInvocationException if the invocation fails
*/
Object readResolve(Object object) throws SpecialMethodInvocationException;
+
+ /**
+ * Invokes {@code writeObject()} on the target object. Should only be used if the target descriptor supports
+ * the method (that is, its serialization type is
+ * {@link org.apache.ignite.internal.network.serialization.SerializationType#SERIALIZABLE} and the class
+ * actually has writeObject() method).
+ * If any of these conditions fail, a {@link NullPointerException} will be thrown.
+ *
+ * @param object target object on which to invoke the method
+ * @param stream stream to pass to the method
+ * @throws SpecialMethodInvocationException if the invocation fails
+ */
+ void writeObject(Object object, ObjectOutputStream stream) throws SpecialMethodInvocationException;
+
+ /**
+ * Invokes {@code readObject()} on the target object. Should only be used if the target descriptor supports
+ * the method (that is, its serialization type is
+ * {@link org.apache.ignite.internal.network.serialization.SerializationType#SERIALIZABLE} and the class
+ * actually has readObject() method).
+ * If any of these conditions fail, a {@link NullPointerException} will be thrown.
+ *
+ * @param object target object on which to invoke the method
+ * @param stream stream to pass to the method
+ * @throws SpecialMethodInvocationException if the invocation fails
+ */
+ void readObject(Object object, ObjectInputStream stream) throws SpecialMethodInvocationException;
}
diff --git a/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/SpecialSerializationMethodsImpl.java b/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/SpecialSerializationMethodsImpl.java
index 553c9dc..def5ff4 100644
--- a/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/SpecialSerializationMethodsImpl.java
+++ b/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/SpecialSerializationMethodsImpl.java
@@ -17,10 +17,11 @@
package org.apache.ignite.internal.network.serialization;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
-import java.lang.reflect.Method;
import java.util.Objects;
import org.jetbrains.annotations.Nullable;
@@ -36,6 +37,14 @@ class SpecialSerializationMethodsImpl implements SpecialSerializationMethods {
@Nullable
private final MethodHandle readResolveHandle;
+ /** MethodHandle that can be used to invoke writeObject() on the target class. */
+ @Nullable
+ private final MethodHandle writeObjectHandle;
+
+ /** MethodHandle that can be used to invoke readObject() on the target class. */
+ @Nullable
+ private final MethodHandle readObjectHandle;
+
/**
* Creates a new instance from the provided descriptor.
*
@@ -44,45 +53,47 @@ class SpecialSerializationMethodsImpl implements SpecialSerializationMethods {
public SpecialSerializationMethodsImpl(ClassDescriptor descriptor) {
writeReplaceHandle = descriptor.hasWriteReplace() ? writeReplaceHandle(descriptor) : null;
readResolveHandle = descriptor.hasReadResolve() ? readResolveHandle(descriptor) : null;
+ writeObjectHandle = descriptor.hasSerializationOverride() ? writeObjectHandle(descriptor) : null;
+ readObjectHandle = descriptor.hasSerializationOverride() ? readObjectHandle(descriptor) : null;
}
private static MethodHandle writeReplaceHandle(ClassDescriptor descriptor) {
- Method writeReplaceMethod = findWriteReplaceMethod(descriptor);
-
- return unreflect(writeReplaceMethod, MethodType.methodType(Object.class, Object.class), descriptor);
- }
-
- private static MethodHandle unreflect(Method method, MethodType methodType, ClassDescriptor descriptor) {
try {
return MethodHandles.privateLookupIn(descriptor.clazz(), MethodHandles.lookup())
- .unreflect(method)
- .asType(methodType);
- } catch (IllegalAccessException e) {
- throw new ReflectionException("Cannot unreflect", e);
+ .findVirtual(descriptor.clazz(), "writeReplace", MethodType.methodType(Object.class))
+ .asType(MethodType.methodType(Object.class, Object.class));
+ } catch (ReflectiveOperationException e) {
+ throw new ReflectionException("Cannot find writeReplace() in " + descriptor.clazz(), e);
}
}
- private static Method findWriteReplaceMethod(ClassDescriptor descriptor) {
+ private static MethodHandle readResolveHandle(ClassDescriptor descriptor) {
try {
- return descriptor.clazz().getDeclaredMethod("writeReplace");
- } catch (NoSuchMethodException e) {
- throw new ReflectionException("writeReplace() was not found on " + descriptor.clazz()
- + " even though the descriptor says the class has the method", e);
+ return MethodHandles.privateLookupIn(descriptor.clazz(), MethodHandles.lookup())
+ .findVirtual(descriptor.clazz(), "readResolve", MethodType.methodType(Object.class))
+ .asType(MethodType.methodType(Object.class, Object.class));
+ } catch (ReflectiveOperationException e) {
+ throw new ReflectionException("Cannot find readResolve() in " + descriptor.clazz(), e);
}
}
- private static MethodHandle readResolveHandle(ClassDescriptor descriptor) {
- Method readResolveMethod = findReadResolveMethod(descriptor);
-
- return unreflect(readResolveMethod, MethodType.methodType(Object.class, Object.class), descriptor);
+ private static MethodHandle writeObjectHandle(ClassDescriptor descriptor) {
+ try {
+ return MethodHandles.privateLookupIn(descriptor.clazz(), MethodHandles.lookup())
+ .findVirtual(descriptor.clazz(), "writeObject", MethodType.methodType(void.class, ObjectOutputStream.class))
+ .asType(MethodType.methodType(void.class, Object.class, ObjectOutputStream.class));
+ } catch (ReflectiveOperationException e) {
+ throw new ReflectionException("Cannot find writeObject() in " + descriptor.clazz(), e);
+ }
}
- private static Method findReadResolveMethod(ClassDescriptor descriptor) {
+ private static MethodHandle readObjectHandle(ClassDescriptor descriptor) {
try {
- return descriptor.clazz().getDeclaredMethod("readResolve");
- } catch (NoSuchMethodException e) {
- throw new ReflectionException("readResolve() was not found on " + descriptor.clazz()
- + " even though the descriptor says the class has the method", e);
+ return MethodHandles.privateLookupIn(descriptor.clazz(), MethodHandles.lookup())
+ .findVirtual(descriptor.clazz(), "readObject", MethodType.methodType(void.class, ObjectInputStream.class))
+ .asType(MethodType.methodType(void.class, Object.class, ObjectInputStream.class));
+ } catch (ReflectiveOperationException e) {
+ throw new ReflectionException("Cannot find readObject() in " + descriptor.clazz(), e);
}
}
@@ -113,4 +124,32 @@ class SpecialSerializationMethodsImpl implements SpecialSerializationMethods {
throw new SpecialMethodInvocationException("readResolve() invocation failed on " + object, e);
}
}
+
+ /** {@inheritDoc} */
+ @Override
+ public void writeObject(Object object, ObjectOutputStream stream) throws SpecialMethodInvocationException {
+ Objects.requireNonNull(writeObjectHandle);
+
+ try {
+ writeObjectHandle.invokeExact(object, stream);
+ } catch (Error e) {
+ throw e;
+ } catch (Throwable e) {
+ throw new SpecialMethodInvocationException("writeObject() invocation failed on " + object, e);
+ }
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public void readObject(Object object, ObjectInputStream stream) throws SpecialMethodInvocationException {
+ Objects.requireNonNull(readObjectHandle);
+
+ try {
+ readObjectHandle.invokeExact(object, stream);
+ } catch (Error e) {
+ throw e;
+ } catch (Throwable e) {
+ throw new SpecialMethodInvocationException("readObject() invocation failed on " + object, e);
+ }
+ }
}
diff --git a/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/ArbitraryObjectMarshaller.java b/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/ArbitraryObjectMarshaller.java
index cddc9cb..3ec8eeb 100644
--- a/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/ArbitraryObjectMarshaller.java
+++ b/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/ArbitraryObjectMarshaller.java
@@ -17,17 +17,22 @@
package org.apache.ignite.internal.network.serialization.marshal;
-import java.io.DataInput;
-import java.io.DataOutput;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
import org.apache.ignite.internal.network.serialization.ClassDescriptor;
import org.apache.ignite.internal.network.serialization.ClassIndexedDescriptors;
import org.apache.ignite.internal.network.serialization.FieldDescriptor;
+import org.apache.ignite.internal.network.serialization.SpecialMethodInvocationException;
/**
* (Un)marshals arbitrary objects (that is, objects that are not built-in nor serializable/externalizable).
*/
-class ArbitraryObjectMarshaller {
+class ArbitraryObjectMarshaller implements DefaultFieldsReaderWriter {
private final TypedValueWriter valueWriter;
private final ValueReader<Object> valueReader;
@@ -44,16 +49,71 @@ class ArbitraryObjectMarshaller {
);
}
- void writeArbitraryObject(Object object, ClassDescriptor descriptor, DataOutput output, MarshallingContext context)
+ void writeArbitraryObject(Object object, ClassDescriptor descriptor, DataOutputStream output, MarshallingContext context)
throws MarshalException, IOException {
- context.addUsedDescriptor(descriptor);
+ for (ClassDescriptor layer : lineage(descriptor)) {
+ writeArbitraryObjectLayer(object, layer, output, context);
+ }
+ }
+
+ /**
+ * Returns the lineage (all the ancestors, from the progenitor (excluding Object) down the line, including the given class).
+ *
+ * @param descriptor class from which to obtain lineage
+ * @return ancestors from the progenitor (excluding Object) down the line, plus the given class itself
+ */
+ private List<ClassDescriptor> lineage(ClassDescriptor descriptor) {
+ List<ClassDescriptor> descriptors = new ArrayList<>();
+
+ ClassDescriptor currentDesc = descriptor;
+ while (currentDesc != null) {
+ descriptors.add(currentDesc);
+ currentDesc = currentDesc.superClassDescriptor();
+ }
+ Collections.reverse(descriptors);
+
+ return descriptors;
+ }
+
+ private void writeArbitraryObjectLayer(Object object, ClassDescriptor layer, DataOutputStream output, MarshallingContext context)
+ throws IOException, MarshalException {
+ if (layer.hasSerializationOverride()) {
+ writeWithWriteObject(object, layer, output, context);
+ } else {
+ defaultWriteFields(object, layer, output, context);
+ }
+
+ context.addUsedDescriptor(layer);
+ }
+
+
+ private void writeWithWriteObject(Object object, ClassDescriptor descriptor, DataOutputStream output, MarshallingContext context)
+ throws IOException, MarshalException {
+ context.startWritingWithWriteObject(object, descriptor);
+
+ try {
+ // Do not close the stream yet!
+ UosObjectOutputStream oos = context.objectOutputStream(output, valueWriter, this);
+ descriptor.serializationMethods().writeObject(object, oos);
+ oos.flush();
+ } catch (SpecialMethodInvocationException e) {
+ throw new MarshalException("Cannot invoke writeObject()", e);
+ } finally {
+ context.endWritingWithWriteObject();
+ }
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public void defaultWriteFields(Object object, ClassDescriptor descriptor, DataOutputStream output, MarshallingContext context)
+ throws MarshalException, IOException {
for (FieldDescriptor fieldDescriptor : descriptor.fields()) {
writeField(object, fieldDescriptor, output, context);
}
}
- private void writeField(Object object, FieldDescriptor fieldDescriptor, DataOutput output, MarshallingContext context)
+ private void writeField(Object object, FieldDescriptor fieldDescriptor, DataOutputStream output, MarshallingContext context)
throws MarshalException, IOException {
Object fieldValue = fieldDescriptor.accessor().get(object);
@@ -68,7 +128,44 @@ class ArbitraryObjectMarshaller {
}
}
- void fillArbitraryObjectFrom(DataInput input, Object object, ClassDescriptor descriptor, UnmarshallingContext context)
+ void fillArbitraryObjectFrom(DataInputStream input, Object object, ClassDescriptor descriptor, UnmarshallingContext context)
+ throws IOException, UnmarshalException {
+ for (ClassDescriptor layer : lineage(descriptor)) {
+ fillArbitraryObjectLayerFrom(input, layer, object, context);
+ }
+ }
+
+ private void fillArbitraryObjectLayerFrom(DataInputStream input, ClassDescriptor layer, Object object, UnmarshallingContext context)
+ throws IOException, UnmarshalException {
+ if (layer.hasSerializationOverride()) {
+ fillObjectWithReadObjectFrom(input, object, layer, context);
+ } else {
+ defaultFillFieldsFrom(input, object, layer, context);
+ }
+ }
+
+ private void fillObjectWithReadObjectFrom(
+ DataInputStream input,
+ Object object,
+ ClassDescriptor descriptor,
+ UnmarshallingContext context
+ ) throws IOException, UnmarshalException {
+ context.startReadingWithReadObject(object, descriptor);
+
+ // Do not close the stream yet!
+ ObjectInputStream ois = context.objectInputStream(input, valueReader, this);
+ try {
+ descriptor.serializationMethods().readObject(object, ois);
+ } catch (SpecialMethodInvocationException e) {
+ throw new UnmarshalException("Cannot invoke readObject()", e);
+ } finally {
+ context.endReadingWithReadObject();
+ }
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public void defaultFillFieldsFrom(DataInputStream input, Object object, ClassDescriptor descriptor, UnmarshallingContext context)
throws IOException, UnmarshalException {
for (FieldDescriptor fieldDescriptor : descriptor.fields()) {
Object fieldValue = valueReader.read(input, context);
@@ -79,4 +176,5 @@ class ArbitraryObjectMarshaller {
private void setFieldValue(Object target, FieldDescriptor fieldDescriptor, Object value) {
fieldDescriptor.accessor().set(target, value);
}
+
}
diff --git a/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/BuiltInContainerMarshallers.java b/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/BuiltInContainerMarshallers.java
index 2f9a8d7..426e46a 100644
--- a/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/BuiltInContainerMarshallers.java
+++ b/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/BuiltInContainerMarshallers.java
@@ -20,7 +20,8 @@ package org.apache.ignite.internal.network.serialization.marshal;
import static java.util.Collections.singletonList;
import java.io.DataInput;
-import java.io.DataOutput;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
@@ -70,7 +71,7 @@ class BuiltInContainerMarshallers {
this.elementWriter = elementWriter;
}
- void writeGenericRefArray(Object[] array, ClassDescriptor arrayDescriptor, DataOutput output, MarshallingContext context)
+ void writeGenericRefArray(Object[] array, ClassDescriptor arrayDescriptor, DataOutputStream output, MarshallingContext context)
throws IOException, MarshalException {
output.writeUTF(array.getClass().getComponentType().getName());
writeCollection(Arrays.asList(array), arrayDescriptor, output, context);
@@ -80,12 +81,12 @@ class BuiltInContainerMarshallers {
return BuiltInMarshalling.preInstantiateGenericRefArray(input);
}
- <T> void fillGenericRefArray(DataInput input, T[] array, ValueReader<T> elementReader, UnmarshallingContext context)
+ <T> void fillGenericRefArray(DataInputStream input, T[] array, ValueReader<T> elementReader, UnmarshallingContext context)
throws IOException, UnmarshalException {
BuiltInMarshalling.fillGenericRefArray(input, array, elementReader, context);
}
- void writeBuiltInCollection(Collection<?> object, ClassDescriptor descriptor, DataOutput output, MarshallingContext context)
+ void writeBuiltInCollection(Collection<?> object, ClassDescriptor descriptor, DataOutputStream output, MarshallingContext context)
throws IOException, MarshalException {
if (supportsAsMutableBuiltInCollection(descriptor)) {
writeCollection(object, descriptor, output, context);
@@ -111,7 +112,7 @@ class BuiltInContainerMarshallers {
private void writeCollection(
Collection<?> collection,
ClassDescriptor collectionDescriptor,
- DataOutput output,
+ DataOutputStream output,
MarshallingContext context
) throws IOException, MarshalException {
context.addUsedDescriptor(collectionDescriptor);
@@ -124,7 +125,7 @@ class BuiltInContainerMarshallers {
return (ValueWriter<T>) elementWriter;
}
- private void writeSingletonList(List<?> list, ClassDescriptor listDescriptor, DataOutput output, MarshallingContext context)
+ private void writeSingletonList(List<?> list, ClassDescriptor listDescriptor, DataOutputStream output, MarshallingContext context)
throws MarshalException, IOException {
assert list.size() == 1;
@@ -174,7 +175,7 @@ class BuiltInContainerMarshallers {
}
<T, C extends Collection<T>> void fillBuiltInCollectionFrom(
- DataInput input,
+ DataInputStream input,
C collection,
ClassDescriptor collectionDescriptor,
ValueReader<T> elementReader,
@@ -189,7 +190,7 @@ class BuiltInContainerMarshallers {
BuiltInMarshalling.fillCollectionFrom(input, collection, elementReader, context);
}
- void writeBuiltInMap(Map<?, ?> map, ClassDescriptor mapDescriptor, DataOutput output, MarshallingContext context)
+ void writeBuiltInMap(Map<?, ?> map, ClassDescriptor mapDescriptor, DataOutputStream output, MarshallingContext context)
throws MarshalException, IOException {
if (!supportsAsBuiltInMap(mapDescriptor)) {
throw new IllegalStateException("Marshalling of " + mapDescriptor.clazz() + " is not supported, but it's marked as a built-in");
@@ -197,13 +198,7 @@ class BuiltInContainerMarshallers {
context.addUsedDescriptor(mapDescriptor);
- BuiltInMarshalling.writeMap(
- map,
- output,
- valueWriter(),
- valueWriter(),
- context
- );
+ BuiltInMarshalling.writeMap(map, output, valueWriter(), valueWriter(), context);
}
private boolean supportsAsBuiltInMap(ClassDescriptor mapDescriptor) {
@@ -238,7 +233,7 @@ class BuiltInContainerMarshallers {
}
<K, V, M extends Map<K, V>> void fillBuiltInMapFrom(
- DataInput input,
+ DataInputStream input,
M map,
ValueReader<K> keyReader,
ValueReader<V> valueReader,
diff --git a/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/BuiltInMarshalling.java b/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/BuiltInMarshalling.java
index cb12b8a..3aebf83 100644
--- a/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/BuiltInMarshalling.java
+++ b/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/BuiltInMarshalling.java
@@ -18,9 +18,13 @@
package org.apache.ignite.internal.network.serialization.marshal;
import static java.util.Collections.singletonList;
+import static org.apache.ignite.internal.network.serialization.marshal.ProtocolMarshalling.readLength;
+import static org.apache.ignite.internal.network.serialization.marshal.ProtocolMarshalling.writeLength;
import java.io.DataInput;
+import java.io.DataInputStream;
import java.io.DataOutput;
+import java.io.DataOutputStream;
import java.io.IOException;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
@@ -101,30 +105,26 @@ class BuiltInMarshalling {
}
static void writeByteArray(byte[] array, DataOutput output) throws IOException {
- output.writeInt(array.length);
- for (byte b : array) {
- output.writeByte(b);
- }
+ writeLength(array.length, output);
+ output.write(array);
}
static byte[] readByteArray(DataInput input) throws IOException {
- int length = input.readInt();
+ int length = readLength(input);
byte[] array = new byte[length];
- for (int i = 0; i < length; i++) {
- array[i] = input.readByte();
- }
+ input.readFully(array);
return array;
}
static void writeShortArray(short[] array, DataOutput output) throws IOException {
- output.writeInt(array.length);
+ writeLength(array.length, output);
for (short sh : array) {
output.writeShort(sh);
}
}
static short[] readShortArray(DataInput input) throws IOException {
- int length = input.readInt();
+ int length = readLength(input);
short[] array = new short[length];
for (int i = 0; i < length; i++) {
array[i] = input.readShort();
@@ -133,14 +133,14 @@ class BuiltInMarshalling {
}
static void writeIntArray(int[] array, DataOutput output) throws IOException {
- output.writeInt(array.length);
+ writeLength(array.length, output);
for (int sh : array) {
output.writeInt(sh);
}
}
static int[] readIntArray(DataInput input) throws IOException {
- int length = input.readInt();
+ int length = readLength(input);
int[] array = new int[length];
for (int i = 0; i < length; i++) {
array[i] = input.readInt();
@@ -149,14 +149,14 @@ class BuiltInMarshalling {
}
static void writeFloatArray(float[] array, DataOutput output) throws IOException {
- output.writeInt(array.length);
+ writeLength(array.length, output);
for (float sh : array) {
output.writeFloat(sh);
}
}
static float[] readFloatArray(DataInput input) throws IOException {
- int length = input.readInt();
+ int length = readLength(input);
float[] array = new float[length];
for (int i = 0; i < length; i++) {
array[i] = input.readFloat();
@@ -165,14 +165,14 @@ class BuiltInMarshalling {
}
static void writeLongArray(long[] array, DataOutput output) throws IOException {
- output.writeInt(array.length);
+ writeLength(array.length, output);
for (long sh : array) {
output.writeLong(sh);
}
}
static long[] readLongArray(DataInput input) throws IOException {
- int length = input.readInt();
+ int length = readLength(input);
long[] array = new long[length];
for (int i = 0; i < length; i++) {
array[i] = input.readLong();
@@ -181,14 +181,14 @@ class BuiltInMarshalling {
}
static void writeDoubleArray(double[] array, DataOutput output) throws IOException {
- output.writeInt(array.length);
+ writeLength(array.length, output);
for (double sh : array) {
output.writeDouble(sh);
}
}
static double[] readDoubleArray(DataInput input) throws IOException {
- int length = input.readInt();
+ int length = readLength(input);
double[] array = new double[length];
for (int i = 0; i < length; i++) {
array[i] = input.readDouble();
@@ -197,14 +197,14 @@ class BuiltInMarshalling {
}
static void writeBooleanArray(boolean[] array, DataOutput output) throws IOException {
- output.writeInt(array.length);
+ writeLength(array.length, output);
for (boolean sh : array) {
output.writeBoolean(sh);
}
}
static boolean[] readBooleanArray(DataInput input) throws IOException {
- int length = input.readInt();
+ int length = readLength(input);
boolean[] array = new boolean[length];
for (int i = 0; i < length; i++) {
array[i] = input.readBoolean();
@@ -213,14 +213,14 @@ class BuiltInMarshalling {
}
static void writeCharArray(char[] array, DataOutput output) throws IOException {
- output.writeInt(array.length);
+ writeLength(array.length, output);
for (char sh : array) {
output.writeChar(sh);
}
}
static char[] readCharArray(DataInput input) throws IOException {
- int length = input.readInt();
+ int length = readLength(input);
char[] array = new char[length];
for (int i = 0; i < length; i++) {
array[i] = input.readChar();
@@ -270,17 +270,17 @@ class BuiltInMarshalling {
}
}
- static <T> void writeRefArray(T[] array, DataOutput output, ValueWriter<T> valueWriter, MarshallingContext context)
+ static <T> void writeRefArray(T[] array, DataOutputStream output, ValueWriter<T> valueWriter, MarshallingContext context)
throws IOException, MarshalException {
- output.writeInt(array.length);
+ writeLength(array.length, output);
for (T object : array) {
valueWriter.write(object, output, context);
}
}
- static <T> T[] readRefArray(DataInput input, IntFunction<T[]> arrayFactory, ValueReader<T> valueReader, UnmarshallingContext context)
+ static <T> T[] readRefArray(DataInputStream input, IntFunction<T[]> arrayFactory, ValueReader<T> valueReader, UnmarshallingContext context)
throws IOException, UnmarshalException {
- int length = input.readInt();
+ int length = readLength(input);
T[] array = arrayFactory.apply(length);
fillRefArrayFrom(input, array, valueReader, context);
@@ -288,7 +288,7 @@ class BuiltInMarshalling {
return array;
}
- private static <T> void fillRefArrayFrom(DataInput input, T[] array, ValueReader<T> valueReader, UnmarshallingContext context)
+ private static <T> void fillRefArrayFrom(DataInputStream input, T[] array, ValueReader<T> valueReader, UnmarshallingContext context)
throws IOException, UnmarshalException {
for (int i = 0; i < array.length; i++) {
array[i] = valueReader.read(input, context);
@@ -308,42 +308,42 @@ class BuiltInMarshalling {
return arrayFactory.apply(length);
}
- static <T> void fillGenericRefArray(DataInput input, T[] array, ValueReader<T> elementReader, UnmarshallingContext context)
+ static <T> void fillGenericRefArray(DataInputStream input, T[] array, ValueReader<T> elementReader, UnmarshallingContext context)
throws IOException, UnmarshalException {
fillRefArrayFrom(input, array, elementReader, context);
}
- static void writeStringArray(String[] array, DataOutput output, MarshallingContext context) throws IOException, MarshalException {
+ static void writeStringArray(String[] array, DataOutputStream output, MarshallingContext context) throws IOException, MarshalException {
writeRefArray(array, output, stringWriter, context);
}
- static String[] readStringArray(DataInput input, UnmarshallingContext context) throws IOException, UnmarshalException {
+ static String[] readStringArray(DataInputStream input, UnmarshallingContext context) throws IOException, UnmarshalException {
return readRefArray(input, stringArrayFactory, stringReader, context);
}
- static void writeBigDecimalArray(BigDecimal[] array, DataOutput output, MarshallingContext context)
+ static void writeBigDecimalArray(BigDecimal[] array, DataOutputStream output, MarshallingContext context)
throws IOException, MarshalException {
writeRefArray(array, output, bigDecimalWriter, context);
}
- static BigDecimal[] readBigDecimalArray(DataInput input, UnmarshallingContext context) throws IOException, UnmarshalException {
+ static BigDecimal[] readBigDecimalArray(DataInputStream input, UnmarshallingContext context) throws IOException, UnmarshalException {
return readRefArray(input, bigDecimalArrayFactory, bigDecimalReader, context);
}
- static void writeEnumArray(Enum<?>[] array, DataOutput output, MarshallingContext context) throws IOException, MarshalException {
+ static void writeEnumArray(Enum<?>[] array, DataOutputStream output, MarshallingContext context) throws IOException, MarshalException {
output.writeUTF(array.getClass().getComponentType().getName());
writeRefArray(array, output, enumWriter, context);
}
- static Enum<?>[] readEnumArray(DataInput input, UnmarshallingContext context) throws IOException, UnmarshalException {
+ static Enum<?>[] readEnumArray(DataInputStream input, UnmarshallingContext context) throws IOException, UnmarshalException {
String enumClassName = input.readUTF();
Class<? extends Enum<?>> enumClass = enumClass(enumClassName);
return readRefArray(input, len -> (Enum<?>[]) Array.newInstance(enumClass, len), enumReader, context);
}
- static <T> void writeCollection(Collection<T> collection, DataOutput output, ValueWriter<T> valueWriter, MarshallingContext context)
+ static <T> void writeCollection(Collection<T> collection, DataOutputStream output, ValueWriter<T> valueWriter, MarshallingContext context)
throws IOException, MarshalException {
- output.writeInt(collection.size());
+ writeLength(collection.size(), output);
for (T object : collection) {
valueWriter.write(object, output, context);
@@ -351,12 +351,12 @@ class BuiltInMarshalling {
}
static <T, C extends Collection<T>> void fillCollectionFrom(
- DataInput input,
+ DataInputStream input,
C collection,
ValueReader<T> valueReader,
UnmarshallingContext context
) throws IOException, UnmarshalException {
- int length = input.readInt();
+ int length = readLength(input);
for (int i = 0; i < length; i++) {
collection.add(valueReader.read(input, context));
@@ -369,7 +369,7 @@ class BuiltInMarshalling {
}
static <T, C extends Collection<T>> void fillSingletonCollectionFrom(
- DataInput input,
+ DataInputStream input,
C collection,
ValueReader<T> elementReader,
UnmarshallingContext context
@@ -385,12 +385,12 @@ class BuiltInMarshalling {
static <K, V> void writeMap(
Map<K, V> map,
- DataOutput output,
+ DataOutputStream output,
ValueWriter<K> keyWriter,
ValueWriter<V> valueWriter,
MarshallingContext context
) throws IOException, MarshalException {
- output.writeInt(map.size());
+ writeLength(map.size(), output);
for (Map.Entry<K, V> entry : map.entrySet()) {
keyWriter.write(entry.getKey(), output, context);
@@ -399,13 +399,14 @@ class BuiltInMarshalling {
}
static <K, V, M extends Map<K, V>> void fillMapFrom(
- DataInput input,
+ DataInputStream input,
M map,
ValueReader<K> keyReader,
ValueReader<V> valueReader,
UnmarshallingContext context
) throws IOException, UnmarshalException {
- int length = input.readInt();
+ int length = readLength(input);
+
for (int i = 0; i < length; i++) {
map.put(keyReader.read(input, context), valueReader.read(input, context));
}
diff --git a/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/BuiltInNonContainerMarshallers.java b/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/BuiltInNonContainerMarshallers.java
index 7cd0758..ada6102 100644
--- a/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/BuiltInNonContainerMarshallers.java
+++ b/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/BuiltInNonContainerMarshallers.java
@@ -18,7 +18,9 @@
package org.apache.ignite.internal.network.serialization.marshal;
import java.io.DataInput;
+import java.io.DataInputStream;
import java.io.DataOutput;
+import java.io.DataOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.BitSet;
@@ -67,7 +69,6 @@ class BuiltInNonContainerMarshallers {
addSingle(map, Enum[].class, BuiltInMarshalling::writeEnumArray, BuiltInMarshalling::readEnumArray);
addSingle(map, BitSet.class, BuiltInMarshalling::writeBitSet, BuiltInMarshalling::readBitSet);
addSingle(map, Null.class, (obj, output) -> {}, input -> null);
- addSingle(map, Void.class, (obj, output) -> {}, input -> null);
return Map.copyOf(map);
}
@@ -127,7 +128,7 @@ class BuiltInNonContainerMarshallers {
return builtInMarshallers.containsKey(classToCheck);
}
- void writeBuiltIn(Object object, ClassDescriptor descriptor, DataOutput output, MarshallingContext context)
+ void writeBuiltIn(Object object, ClassDescriptor descriptor, DataOutputStream output, MarshallingContext context)
throws IOException, MarshalException {
BuiltInMarshaller<?> builtInMarshaller = findBuiltInMarshaller(descriptor);
@@ -136,7 +137,7 @@ class BuiltInNonContainerMarshallers {
context.addUsedDescriptor(descriptor);
}
- Object readBuiltIn(ClassDescriptor descriptor, DataInput input, UnmarshallingContext context) throws IOException, UnmarshalException {
+ Object readBuiltIn(ClassDescriptor descriptor, DataInputStream input, UnmarshallingContext context) throws IOException, UnmarshalException {
BuiltInMarshaller<?> builtinMarshaller = findBuiltInMarshaller(descriptor);
return builtinMarshaller.unmarshal(input, context);
}
@@ -160,11 +161,11 @@ class BuiltInNonContainerMarshallers {
this.reader = reader;
}
- private void marshal(Object object, DataOutput output, MarshallingContext context) throws IOException, MarshalException {
+ private void marshal(Object object, DataOutputStream output, MarshallingContext context) throws IOException, MarshalException {
writer.write(valueRefClass.cast(object), output, context);
}
- private Object unmarshal(DataInput input, UnmarshallingContext context) throws IOException, UnmarshalException {
+ private Object unmarshal(DataInputStream input, UnmarshallingContext context) throws IOException, UnmarshalException {
return reader.read(input, context);
}
}
diff --git a/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/ValueWriter.java b/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/DefaultFieldsReaderWriter.java
similarity index 62%
copy from modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/ValueWriter.java
copy to modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/DefaultFieldsReaderWriter.java
index fe18d79..d0ec6b3 100644
--- a/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/ValueWriter.java
+++ b/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/DefaultFieldsReaderWriter.java
@@ -17,21 +17,18 @@
package org.apache.ignite.internal.network.serialization.marshal;
-import java.io.DataOutput;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
import java.io.IOException;
+import org.apache.ignite.internal.network.serialization.ClassDescriptor;
/**
- * Knows how to write a value to a {@link DataOutput}.
+ *
*/
-interface ValueWriter<T> {
- /**
- * Writes the given value to a {@link DataOutput}.
- *
- * @param value value to write
- * @param output where to write to
- * @param context marshalling context
- * @throws IOException if an I/O problem occurs
- * @throws MarshalException if another problem occurs
- */
- void write(T value, DataOutput output, MarshallingContext context) throws IOException, MarshalException;
+interface DefaultFieldsReaderWriter {
+ void defaultWriteFields(Object object, ClassDescriptor descriptor, DataOutputStream output, MarshallingContext context)
+ throws MarshalException, IOException;
+
+ void defaultFillFieldsFrom(DataInputStream input, Object object, ClassDescriptor descriptor, UnmarshallingContext context)
+ throws IOException, UnmarshalException;
}
diff --git a/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/DefaultUserObjectMarshaller.java b/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/DefaultUserObjectMarshaller.java
index 146ada9..3a16ff7 100644
--- a/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/DefaultUserObjectMarshaller.java
+++ b/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/DefaultUserObjectMarshaller.java
@@ -52,8 +52,8 @@ public class DefaultUserObjectMarshaller implements UserObjectMarshaller {
private final BuiltInContainerMarshallers builtInContainerMarshallers = new BuiltInContainerMarshallers(
(obj, out, ctx) -> marshalToOutput(obj, objectClass(obj), out, ctx)
);
- private final ExternalizableMarshaller externalizableMarshaller = new ExternalizableMarshaller();
private final ArbitraryObjectMarshaller arbitraryObjectMarshaller;
+ private final ExternalizableMarshaller externalizableMarshaller;
/**
* Constructor.
@@ -70,6 +70,8 @@ public class DefaultUserObjectMarshaller implements UserObjectMarshaller {
this::marshalToOutput,
this::unmarshalFromInput
);
+
+ externalizableMarshaller = new ExternalizableMarshaller(this::unmarshalFromInput, this::marshalToOutput, arbitraryObjectMarshaller);
}
public MarshalledObject marshal(@Nullable Object object) throws MarshalException {
@@ -91,7 +93,7 @@ public class DefaultUserObjectMarshaller implements UserObjectMarshaller {
return new MarshalledObject(baos.toByteArray(), context.usedDescriptors());
}
- private void marshalToOutput(@Nullable Object object, Class<?> declaredClass, DataOutput output, MarshallingContext context)
+ private void marshalToOutput(@Nullable Object object, Class<?> declaredClass, DataOutputStream output, MarshallingContext context)
throws MarshalException, IOException {
assert declaredClass != null;
assert object == null
@@ -102,7 +104,9 @@ public class DefaultUserObjectMarshaller implements UserObjectMarshaller {
throwIfMarshallingNotSupported(object);
- DescribedObject writeReplaced = applyWriteReplaceIfNeeded(object, declaredClass);
+ ClassDescriptor originalDescriptor = obtainOriginalDescriptor(object, declaredClass);
+
+ DescribedObject writeReplaced = applyWriteReplaceIfNeeded(object, originalDescriptor);
if (canParticipateInCycles(writeReplaced.descriptor)) {
Integer maybeRefId = context.rememberAsSeen(writeReplaced.object);
@@ -126,6 +130,16 @@ public class DefaultUserObjectMarshaller implements UserObjectMarshaller {
return !builtInNonContainerMarshallers.supports(descriptor.clazz());
}
+ private ClassDescriptor obtainOriginalDescriptor(@Nullable Object object, Class<?> declaredClass) {
+ // object class is not a subclass of the declared class for primitives
+ // for enums we don't need the specific classes at all
+ Class<?> classToQueryForOriginalDescriptor = isInstanceOfSubclass(object, declaredClass)
+ && !(object instanceof Enum)
+ ? object.getClass() : declaredClass;
+
+ return getOrCreateDescriptor(classToQueryForOriginalDescriptor);
+ }
+
private boolean objectIsMemberOfEnumWithAnonymousClassesForMembers(Object object, Class<?> declaredClass) {
return declaredClass.isEnum() && object.getClass().getSuperclass() == declaredClass;
}
@@ -162,22 +176,19 @@ public class DefaultUserObjectMarshaller implements UserObjectMarshaller {
return false;
}
- private DescribedObject applyWriteReplaceIfNeeded(@Nullable Object originalObject, Class<?> declaredClass) throws MarshalException {
- // object class is not a subclass of the declared class for primitives
- // for enums we don't need the specific classes at all
- Class<?> classToQueryForOriginalDescriptor = isInstanceOfSubclass(originalObject, declaredClass)
- && !(originalObject instanceof Enum)
- ? originalObject.getClass() : declaredClass;
-
- final ClassDescriptor originalDescriptor = getOrCreateDescriptor(classToQueryForOriginalDescriptor);
+ private DescribedObject applyWriteReplaceIfNeeded(@Nullable Object objectBefore, ClassDescriptor descriptorBefore)
+ throws MarshalException {
+ if (!descriptorBefore.supportsWriteReplace()) {
+ return new DescribedObject(objectBefore, descriptorBefore);
+ }
- if (originalDescriptor.supportsWriteReplace()) {
- Object objectToWrite = applyWriteReplace(originalObject, originalDescriptor);
- ClassDescriptor descriptorToUse = getOrCreateDescriptor(objectToWrite, objectClass(objectToWrite));
+ Object replacedObject = applyWriteReplace(objectBefore, descriptorBefore);
+ ClassDescriptor replacementDescriptor = getOrCreateDescriptor(replacedObject, objectClass(replacedObject));
- return new DescribedObject(objectToWrite, descriptorToUse);
+ if (descriptorBefore.describesSameClass(replacementDescriptor)) {
+ return new DescribedObject(replacedObject, replacementDescriptor);
} else {
- return new DescribedObject(originalObject, originalDescriptor);
+ return applyWriteReplaceIfNeeded(replacedObject, replacementDescriptor);
}
}
@@ -195,11 +206,7 @@ public class DefaultUserObjectMarshaller implements UserObjectMarshaller {
}
private ClassDescriptor getOrCreateDescriptor(@Nullable Object object, Class<?> declaredClass) {
- assert object != null || declaredClass == Void.class || declaredClass == Null.class;
-
- if (declaredClass == Void.class) {
- return descriptorRegistry.getRequiredDescriptor(Void.class);
- }
+ assert object != null || declaredClass == Null.class;
if (object == null) {
return descriptorRegistry.getNullDescriptor();
@@ -237,18 +244,18 @@ public class DefaultUserObjectMarshaller implements UserObjectMarshaller {
private void writeReference(int referenceId, DataOutput output) throws IOException {
writeDescriptorOrCommandId(SerializedStreamCommands.REFERENCE, output);
- writeReferenceId(referenceId, output);
+ ProtocolMarshalling.writeObjectId(referenceId, output);
}
- private void marshalCycleable(DescribedObject describedObject, DataOutput output, MarshallingContext context)
+ private void marshalCycleable(DescribedObject describedObject, DataOutputStream output, MarshallingContext context)
throws IOException, MarshalException {
writeDescriptorId(describedObject.descriptor, output);
- writeReferenceId(context.referenceId(describedObject.object), output);
+ ProtocolMarshalling.writeObjectId(context.referenceId(describedObject.object), output);
writeObject(describedObject.object, describedObject.descriptor, output, context);
}
- private void marshalNonCycleable(DescribedObject describedObject, DataOutput output, MarshallingContext context)
+ private void marshalNonCycleable(DescribedObject describedObject, DataOutputStream output, MarshallingContext context)
throws IOException, MarshalException {
writeDescriptorId(describedObject.descriptor, output);
@@ -260,14 +267,10 @@ public class DefaultUserObjectMarshaller implements UserObjectMarshaller {
}
private void writeDescriptorOrCommandId(int id, DataOutput output) throws IOException {
- output.writeInt(id);
- }
-
- private void writeReferenceId(int referenceId, DataOutput output) throws IOException {
- output.writeInt(referenceId);
+ ProtocolMarshalling.writeDescriptorOrCommandId(id, output);
}
- private void writeObject(@Nullable Object object, ClassDescriptor descriptor, DataOutput output, MarshallingContext context)
+ private void writeObject(@Nullable Object object, ClassDescriptor descriptor, DataOutputStream output, MarshallingContext context)
throws IOException, MarshalException {
if (isBuiltInNonContainer(descriptor)) {
builtInNonContainerMarshallers.writeBuiltIn(object, descriptor, output, context);
@@ -307,14 +310,18 @@ public class DefaultUserObjectMarshaller implements UserObjectMarshaller {
public <T> T unmarshal(byte[] bytes, IdIndexedDescriptors mergedDescriptors) throws UnmarshalException {
try (var bais = new ByteArrayInputStream(bytes); var dis = new DataInputStream(bais)) {
UnmarshallingContext context = new UnmarshallingContext(bais, mergedDescriptors);
- return unmarshalFromInput(dis, context);
+ T result = unmarshalFromInput(dis, context);
+
+ throwIfExcessiveBytesRemain(dis);
+
+ return result;
} catch (IOException e) {
throw new UnmarshalException("Cannot unmarshal", e);
}
}
- private <T> T unmarshalFromInput(DataInput input, UnmarshallingContext context) throws IOException, UnmarshalException {
- int commandOrDescriptorId = readDescriptorOrCommandId(input);
+ private <T> T unmarshalFromInput(DataInputStream input, UnmarshallingContext context) throws IOException, UnmarshalException {
+ int commandOrDescriptorId = ProtocolMarshalling.readDescriptorOrCommandId(input);
if (commandOrDescriptorId == SerializedStreamCommands.REFERENCE) {
// TODO: IGNITE-16165 - make sure readResolve() is applied correctly when we exit early due to reading a reference
return unmarshalReference(input, context);
@@ -332,31 +339,23 @@ public class DefaultUserObjectMarshaller implements UserObjectMarshaller {
return resolvedObject;
}
- private int readDescriptorOrCommandId(DataInput input) throws IOException {
- return input.readInt();
- }
-
private <T> T unmarshalReference(DataInput input, UnmarshallingContext context) throws IOException {
- int referenceId = input.readInt();
- return context.dereference(referenceId);
+ int objectId = ProtocolMarshalling.readObjectId(input);
+ return context.dereference(objectId);
}
- private Object readCycleable(DataInput input, UnmarshallingContext context, ClassDescriptor descriptor)
+ private Object readCycleable(DataInputStream input, UnmarshallingContext context, ClassDescriptor descriptor)
throws IOException, UnmarshalException {
- int referenceId = readReferenceId(input);
+ int objectId = ProtocolMarshalling.readObjectId(input);
Object preInstantiatedObject = preInstantiate(descriptor, input, context);
- context.registerReference(referenceId, preInstantiatedObject);
+ context.registerReference(objectId, preInstantiatedObject);
fillObjectFrom(input, preInstantiatedObject, descriptor, context);
return preInstantiatedObject;
}
- private int readReferenceId(DataInput input) throws IOException {
- return input.readInt();
- }
-
private Object preInstantiate(ClassDescriptor descriptor, DataInput input, UnmarshallingContext context)
throws IOException, UnmarshalException {
if (isBuiltInNonContainer(descriptor)) {
@@ -378,7 +377,7 @@ public class DefaultUserObjectMarshaller implements UserObjectMarshaller {
return builtInContainerMarshallers.preInstantiateGenericRefArray(input);
}
- private void fillObjectFrom(DataInput input, Object preInstantiatedObject, ClassDescriptor descriptor, UnmarshallingContext context)
+ private void fillObjectFrom(DataInputStream input, Object preInstantiatedObject, ClassDescriptor descriptor, UnmarshallingContext context)
throws UnmarshalException, IOException {
if (isBuiltInNonContainer(descriptor)) {
throw new IllegalStateException("Cannot fill " + descriptor.clazz() + ", this is a programmatic error");
@@ -389,14 +388,14 @@ public class DefaultUserObjectMarshaller implements UserObjectMarshaller {
} else if (isArray(descriptor)) {
fillGenericRefArrayFrom(input, (Object[]) preInstantiatedObject, context);
} else if (descriptor.isExternalizable()) {
- externalizableMarshaller.fillExternalizableFrom(input, (Externalizable) preInstantiatedObject);
+ externalizableMarshaller.fillExternalizableFrom(input, (Externalizable) preInstantiatedObject, context);
} else {
arbitraryObjectMarshaller.fillArbitraryObjectFrom(input, preInstantiatedObject, descriptor, context);
}
}
private void fillBuiltInCollectionFrom(
- DataInput input,
+ DataInputStream input,
Collection<?> preInstantiatedObject,
ClassDescriptor descriptor,
UnmarshallingContext context
@@ -405,7 +404,7 @@ public class DefaultUserObjectMarshaller implements UserObjectMarshaller {
}
private void fillBuiltInMapFrom(
- DataInput input,
+ DataInputStream input,
Map<?, ?> preInstantiatedObject,
UnmarshallingContext context
) throws UnmarshalException, IOException {
@@ -417,13 +416,13 @@ public class DefaultUserObjectMarshaller implements UserObjectMarshaller {
);
}
- private void fillGenericRefArrayFrom(DataInput input, Object[] array, UnmarshallingContext context)
+ private void fillGenericRefArrayFrom(DataInputStream input, Object[] array, UnmarshallingContext context)
throws IOException, UnmarshalException {
builtInContainerMarshallers.fillGenericRefArray(input, array, this::unmarshalFromInput, context);
}
@Nullable
- private Object readObject(DataInput input, ClassDescriptor descriptor, UnmarshallingContext context)
+ private Object readObject(DataInputStream input, ClassDescriptor descriptor, UnmarshallingContext context)
throws IOException, UnmarshalException {
if (isBuiltInNonContainer(descriptor)) {
return builtInNonContainerMarshallers.readBuiltIn(descriptor, input, context);
@@ -448,6 +447,12 @@ public class DefaultUserObjectMarshaller implements UserObjectMarshaller {
}
}
+ private void throwIfExcessiveBytesRemain(DataInputStream dis) throws IOException, UnmarshalException {
+ if (dis.available() > 0) {
+ throw new UnmarshalException("After reading a value, " + dis.available() + " excessive byte(s) still remain");
+ }
+ }
+
private static class DescribedObject {
@Nullable
private final Object object;
diff --git a/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/ExternalizableMarshaller.java b/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/ExternalizableMarshaller.java
index 6ace075..f52d6bc 100644
--- a/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/ExternalizableMarshaller.java
+++ b/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/ExternalizableMarshaller.java
@@ -17,39 +17,44 @@
package org.apache.ignite.internal.network.serialization.marshal;
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-import java.io.DataInput;
-import java.io.DataOutput;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInputStream;
-import java.io.ObjectOutputStream;
import org.apache.ignite.internal.network.serialization.ClassDescriptor;
/**
* (Um)marshalling specific to EXTERNALIZABLE serialization type.
*/
class ExternalizableMarshaller {
+ private final ValueReader<Object> valueReader;
+ private final TypedValueWriter typedValueWriter;
+ private final DefaultFieldsReaderWriter defaultFieldsReaderWriter;
+
private final NoArgConstructorInstantiation instantiation = new NoArgConstructorInstantiation();
- void writeExternalizable(Externalizable externalizable, ClassDescriptor descriptor, DataOutput output, MarshallingContext context)
- throws IOException {
- byte[] externalizableBytes = externalize(externalizable);
+ ExternalizableMarshaller(ValueReader<Object> valueReader, TypedValueWriter typedValueWriter, DefaultFieldsReaderWriter defaultFieldsReaderWriter) {
+ this.valueReader = valueReader;
+ this.typedValueWriter = typedValueWriter;
+ this.defaultFieldsReaderWriter = defaultFieldsReaderWriter;
+ }
- output.writeInt(externalizableBytes.length);
- output.write(externalizableBytes);
+ void writeExternalizable(Externalizable externalizable, ClassDescriptor descriptor, DataOutputStream output, MarshallingContext context)
+ throws IOException {
+ externalizeTo(externalizable, output, context);
context.addUsedDescriptor(descriptor);
}
- private byte[] externalize(Externalizable externalizable) throws IOException {
- var baos = new ByteArrayOutputStream();
- try (var oos = new ObjectOutputStream(baos)) {
- externalizable.writeExternal(oos);
- }
+ private void externalizeTo(Externalizable externalizable, DataOutputStream output, MarshallingContext context)
+ throws IOException {
+ context.endWritingWithWriteObject();
- return baos.toByteArray();
+ // Do not close the stream yet!
+ UosObjectOutputStream oos = context.objectOutputStream(output, typedValueWriter, defaultFieldsReaderWriter);
+ externalizable.writeExternal(oos);
+ oos.flush();
}
@SuppressWarnings("unchecked")
@@ -61,12 +66,13 @@ class ExternalizableMarshaller {
}
}
- <T extends Externalizable> void fillExternalizableFrom(DataInput input, T object) throws IOException, UnmarshalException {
- int length = input.readInt();
- byte[] bytes = new byte[length];
- input.readFully(bytes);
+ <T extends Externalizable> void fillExternalizableFrom(DataInputStream input, T object, UnmarshallingContext context)
+ throws IOException, UnmarshalException {
+ context.endReadingWithReadObject();
- try (var ois = new ObjectInputStream(new ByteArrayInputStream(bytes))) {
+ // Do not close the stream yet!
+ ObjectInputStream ois = context.objectInputStream(input, valueReader, defaultFieldsReaderWriter);
+ try {
object.readExternal(ois);
} catch (ClassNotFoundException e) {
throw new UnmarshalException("Cannot unmarshal due to a missing class", e);
diff --git a/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/MarshallingContext.java b/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/MarshallingContext.java
index 5989190..30683f9 100644
--- a/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/MarshallingContext.java
+++ b/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/MarshallingContext.java
@@ -19,6 +19,9 @@ package org.apache.ignite.internal.network.serialization.marshal;
import static java.util.Collections.unmodifiableSet;
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.io.NotActiveException;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.Map;
@@ -36,6 +39,11 @@ class MarshallingContext {
private int nextRefId = 0;
+ private Object objectCurrentlyWrittenWithWriteObject;
+ private ClassDescriptor descriptorOfObjectCurrentlyWrittenWithWriteObject;
+
+ private UosObjectOutputStream objectOutputStream;
+
public void addUsedDescriptor(ClassDescriptor descriptor) {
usedDescriptors.add(descriptor);
}
@@ -87,4 +95,42 @@ class MarshallingContext {
return refId;
}
+
+ public Object objectCurrentlyWrittenWithWriteObject() throws NotActiveException {
+ if (objectCurrentlyWrittenWithWriteObject == null) {
+ throw new NotActiveException("not in call to writeObject");
+ }
+
+ return objectCurrentlyWrittenWithWriteObject;
+ }
+
+ public ClassDescriptor descriptorOfObjectCurrentlyWrittenWithWriteObject() {
+ if (descriptorOfObjectCurrentlyWrittenWithWriteObject == null) {
+ throw new IllegalStateException("No object is currently being written");
+ }
+
+ return descriptorOfObjectCurrentlyWrittenWithWriteObject;
+ }
+
+ public void startWritingWithWriteObject(Object object, ClassDescriptor descriptor) {
+ objectCurrentlyWrittenWithWriteObject = object;
+ descriptorOfObjectCurrentlyWrittenWithWriteObject = descriptor;
+ }
+
+ public void endWritingWithWriteObject() {
+ objectCurrentlyWrittenWithWriteObject = null;
+ descriptorOfObjectCurrentlyWrittenWithWriteObject = null;
+ }
+
+ UosObjectOutputStream objectOutputStream(
+ DataOutputStream output,
+ TypedValueWriter valueWriter,
+ DefaultFieldsReaderWriter defaultFieldsReaderWriter
+ ) throws IOException {
+ if (objectOutputStream == null) {
+ objectOutputStream = new UosObjectOutputStream(output, valueWriter, defaultFieldsReaderWriter, this);
+ }
+
+ return objectOutputStream;
+ }
}
diff --git a/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/ProtocolMarshalling.java b/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/ProtocolMarshalling.java
new file mode 100644
index 0000000..c4bc2f3
--- /dev/null
+++ b/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/ProtocolMarshalling.java
@@ -0,0 +1,73 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.network.serialization.marshal;
+
+import java.io.DataInput;
+import java.io.DataOutput;
+import java.io.IOException;
+
+/**
+ * Protocol-wide elements marshalling.
+ */
+class ProtocolMarshalling {
+ static void writeDescriptorOrCommandId(int id, DataOutput output) throws IOException {
+ output.writeInt(id);
+ }
+
+ static int readDescriptorOrCommandId(DataInput input) throws IOException {
+ return input.readInt();
+ }
+
+ static void writeObjectId(int id, DataOutput output) throws IOException {
+ output.writeInt(id);
+ }
+
+ static int readObjectId(DataInput input) throws IOException {
+ return input.readInt();
+ }
+
+
+ static void writeLength(int length, DataOutput output) throws IOException {
+ writeUnsignedInt(length, output);
+ }
+
+ private static void writeUnsignedInt(int value, DataOutput output) throws IOException {
+ if (value < 0) {
+ throw new IllegalArgumentException(value + " is negative" );
+ }
+
+ output.writeInt(value);
+ }
+
+ static int readLength(DataInput input) throws IOException {
+ return readUnsignedInt(input);
+ }
+
+ private static int readUnsignedInt(DataInput input) throws IOException {
+ int value = input.readInt();
+
+ if (value < 0) {
+ throw new IllegalStateException(value + " is negative");
+ }
+
+ return value;
+ }
+
+ private ProtocolMarshalling() {
+ }
+}
diff --git a/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/TypedValueWriter.java b/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/TypedValueWriter.java
index 294bc4e..2edaa3c 100644
--- a/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/TypedValueWriter.java
+++ b/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/TypedValueWriter.java
@@ -17,15 +17,15 @@
package org.apache.ignite.internal.network.serialization.marshal;
-import java.io.DataOutput;
+import java.io.DataOutputStream;
import java.io.IOException;
/**
- * Writes objects to a {@link DataOutput} taking their original (for example, declared) types into consideration.
+ * Writes objects to a {@link DataOutputStream} taking their original (for example, declared) types into consideration.
*/
interface TypedValueWriter {
/**
- * Writes the given object to the {@link DataOutput}.
+ * Writes the given object to the {@link DataOutputStream}.
*
* @param object object to write
* @param declaredClass the original class of the object (i.e. {@code byte.class} for {@code byte})
@@ -34,6 +34,6 @@ interface TypedValueWriter {
* @throws IOException if an I/O problem occurs
* @throws MarshalException if another problem occurs
*/
- void write(Object object, Class<?> declaredClass, DataOutput output, MarshallingContext context)
+ void write(Object object, Class<?> declaredClass, DataOutputStream output, MarshallingContext context)
throws IOException, MarshalException;
}
diff --git a/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/UnmarshalException.java b/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/UnmarshalException.java
index 9e7b624..19dec95 100644
--- a/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/UnmarshalException.java
+++ b/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/UnmarshalException.java
@@ -23,6 +23,10 @@ import org.apache.ignite.lang.IgniteInternalCheckedException;
* Thrown when unmarshalling fails.
*/
public class UnmarshalException extends IgniteInternalCheckedException {
+ public UnmarshalException(String msg) {
+ super(msg);
+ }
+
public UnmarshalException(String message, Throwable cause) {
super(message, cause);
}
diff --git a/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/UnmarshallingContext.java b/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/UnmarshallingContext.java
index 94d7325..9dd093d 100644
--- a/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/UnmarshallingContext.java
+++ b/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/UnmarshallingContext.java
@@ -18,6 +18,9 @@
package org.apache.ignite.internal.network.serialization.marshal;
import java.io.ByteArrayInputStream;
+import java.io.DataInputStream;
+import java.io.IOException;
+import java.io.NotActiveException;
import java.util.HashMap;
import java.util.Map;
import org.apache.ignite.internal.network.serialization.ClassDescriptor;
@@ -33,6 +36,11 @@ class UnmarshallingContext implements IdIndexedDescriptors {
private final Map<Integer, Object> refsToObjects = new HashMap<>();
+ private Object objectCurrentlyReadWithReadObject;
+ private ClassDescriptor descriptorOfObjectCurrentlyReadWithReadObject;
+
+ private UosObjectInputStream objectInputStream;
+
public UnmarshallingContext(ByteArrayInputStream source, IdIndexedDescriptors descriptors) {
this.source = source;
this.descriptors = descriptors;
@@ -66,4 +74,42 @@ class UnmarshallingContext implements IdIndexedDescriptors {
public void resetSourceToMark() {
source.reset();
}
+
+ public Object objectCurrentlyReadWithReadObject() throws NotActiveException {
+ if (objectCurrentlyReadWithReadObject == null) {
+ throw new NotActiveException("not in call to readObject");
+ }
+
+ return objectCurrentlyReadWithReadObject;
+ }
+
+ public ClassDescriptor descriptorOfObjectCurrentlyReadWithReadObject() {
+ if (descriptorOfObjectCurrentlyReadWithReadObject == null) {
+ throw new IllegalStateException("No object is currently being read with readObject()");
+ }
+
+ return descriptorOfObjectCurrentlyReadWithReadObject;
+ }
+
+ public void startReadingWithReadObject(Object object, ClassDescriptor descriptor) {
+ objectCurrentlyReadWithReadObject = object;
+ descriptorOfObjectCurrentlyReadWithReadObject = descriptor;
+ }
+
+ public void endReadingWithReadObject() {
+ objectCurrentlyReadWithReadObject = null;
+ descriptorOfObjectCurrentlyReadWithReadObject = null;
+ }
+
+ UosObjectInputStream objectInputStream(
+ DataInputStream input,
+ ValueReader<Object> valueReader,
+ DefaultFieldsReaderWriter defaultFieldsReaderWriter
+ ) throws IOException {
+ if (objectInputStream == null) {
+ objectInputStream = new UosObjectInputStream(input, valueReader, defaultFieldsReaderWriter, this);
+ }
+
+ return objectInputStream;
+ }
}
diff --git a/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/UosObjectInputStream.java b/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/UosObjectInputStream.java
new file mode 100644
index 0000000..853562e
--- /dev/null
+++ b/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/UosObjectInputStream.java
@@ -0,0 +1,174 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.network.serialization.marshal;
+
+import java.io.DataInputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+
+/**
+ *
+ */
+class UosObjectInputStream extends ObjectInputStream {
+ private final DataInputStream input;
+ private final ValueReader<Object> valueReader;
+ private final DefaultFieldsReaderWriter defaultFieldsReaderWriter;
+ private final UnmarshallingContext context;
+
+ UosObjectInputStream(DataInputStream input, ValueReader<Object> valueReader, DefaultFieldsReaderWriter defaultFieldsReaderWriter, UnmarshallingContext context) throws IOException {
+ this.input = input;
+ this.valueReader = valueReader;
+ this.defaultFieldsReaderWriter = defaultFieldsReaderWriter;
+ this.context = context;
+ }
+
+ @Override
+ public int read() throws IOException {
+ return input.read();
+ }
+
+ @SuppressWarnings("NullableProblems")
+ @Override
+ public int read(byte[] buf) throws IOException {
+ return input.read(buf);
+ }
+
+ @Override
+ public int read(byte[] buf, int off, int len) throws IOException {
+ return input.read(buf, off, len);
+ }
+
+ @Override
+ public byte readByte() throws IOException {
+ return input.readByte();
+ }
+
+ @Override
+ public short readShort() throws IOException {
+ return input.readShort();
+ }
+
+ @Override
+ public int readInt() throws IOException {
+ return input.readInt();
+ }
+
+ @Override
+ public long readLong() throws IOException {
+ return input.readLong();
+ }
+
+ @Override
+ public float readFloat() throws IOException {
+ return input.readFloat();
+ }
+
+ @Override
+ public double readDouble() throws IOException {
+ return input.readDouble();
+ }
+
+ @Override
+ public char readChar() throws IOException {
+ return input.readChar();
+ }
+
+ @Override
+ public boolean readBoolean() throws IOException {
+ return input.readBoolean();
+ }
+
+ @Override
+ public String readUTF() throws IOException {
+ return input.readUTF();
+ }
+
+ @Override
+ public int readUnsignedByte() throws IOException {
+ return input.readUnsignedByte();
+ }
+
+ @Override
+ public int readUnsignedShort() throws IOException {
+ return input.readUnsignedShort();
+ }
+
+ @Override
+ public void readFully(byte[] buf) throws IOException {
+ input.readFully(buf);
+ }
+
+ @Override
+ public void readFully(byte[] buf, int off, int len) throws IOException {
+ input.readFully(buf, off, len);
+ }
+
+ @Override
+ public String readLine() throws IOException {
+ return input.readLine();
+ }
+
+ @Override
+ protected Object readObjectOverride() throws IOException {
+ return doReadObject();
+ }
+
+ private Object doReadObject() throws IOException {
+ try {
+ return valueReader.read(input, context);
+ } catch (UnmarshalException e) {
+ // TODO: IGNITE-16165 - pass exception correctly
+ throw new RuntimeException("Cannot read", e);
+ }
+ }
+
+ @Override
+ public Object readUnshared() throws IOException {
+ // TODO: IGNITE-16165 - implement 'unshared' logic?
+ return doReadObject();
+ }
+
+ @Override
+ public void defaultReadObject() throws IOException {
+ try {
+ defaultFieldsReaderWriter.defaultFillFieldsFrom(
+ input,
+ context.objectCurrentlyReadWithReadObject(),
+ context.descriptorOfObjectCurrentlyReadWithReadObject(),
+ context
+ );
+ } catch (UnmarshalException e) {
+ // TODO: IGNITE-16165 - pass exception correctly
+ throw new RuntimeException("Cannot read", e);
+ }
+ }
+
+ @Override
+ public int available() throws IOException {
+ return input.available();
+ }
+
+ @Override
+ public int skipBytes(int len) throws IOException {
+ return input.skipBytes(len);
+ }
+
+ @Override
+ public void close() throws IOException {
+ }
+}
diff --git a/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/UosObjectOutputStream.java b/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/UosObjectOutputStream.java
new file mode 100644
index 0000000..1ce3b28
--- /dev/null
+++ b/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/UosObjectOutputStream.java
@@ -0,0 +1,171 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.network.serialization.marshal;
+
+import static org.apache.ignite.internal.network.serialization.marshal.ObjectClass.objectClass;
+
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.io.ObjectOutputStream;
+
+/**
+ *
+ */
+class UosObjectOutputStream extends ObjectOutputStream {
+ private final DataOutputStream output;
+ private final TypedValueWriter valueWriter;
+ private final DefaultFieldsReaderWriter defaultFieldsReaderWriter;
+ private final MarshallingContext context;
+
+ UosObjectOutputStream(
+ DataOutputStream output,
+ TypedValueWriter valueWriter,
+ DefaultFieldsReaderWriter defaultFieldsReaderWriter,
+ MarshallingContext context
+ ) throws IOException {
+ this.output = output;
+ this.valueWriter = valueWriter;
+ this.defaultFieldsReaderWriter = defaultFieldsReaderWriter;
+ this.context = context;
+ }
+
+ @Override
+ public void write(int val) throws IOException {
+ output.write(val);
+ }
+
+ @Override
+ public void write(byte[] buf) throws IOException {
+ output.write(buf);
+ }
+
+ @Override
+ public void write(byte[] buf, int off, int len) throws IOException {
+ output.write(buf, off, len);
+ }
+
+ @Override
+ public void writeByte(int val) throws IOException {
+ output.writeByte(val);
+ }
+
+ @Override
+ public void writeShort(int val) throws IOException {
+ output.writeShort(val);
+ }
+
+ @Override
+ public void writeInt(int val) throws IOException {
+ output.writeInt(val);
+ }
+
+ @Override
+ public void writeLong(long val) throws IOException {
+ output.writeLong(val);
+ }
+
+ @Override
+ public void writeFloat(float val) throws IOException {
+ output.writeFloat(val);
+ }
+
+ @Override
+ public void writeDouble(double val) throws IOException {
+ output.writeDouble(val);
+ }
+
+ @Override
+ public void writeChar(int val) throws IOException {
+ output.writeChar(val);
+ }
+
+ @Override
+ public void writeBoolean(boolean val) throws IOException {
+ output.writeBoolean(val);
+ }
+
+ @Override
+ public void writeBytes(String str) throws IOException {
+ output.writeBytes(str);
+ }
+
+ @Override
+ public void writeChars(String str) throws IOException {
+ output.writeChars(str);
+ }
+
+ @Override
+ public void writeUTF(String str) throws IOException {
+ output.writeUTF(str);
+ }
+
+ @Override
+ protected void writeObjectOverride(Object obj) throws IOException {
+ writeObject0(obj);
+ }
+
+ private void writeObject0(Object obj) throws IOException {
+ try {
+ valueWriter.write(obj, objectClass(obj), output, context);
+ } catch (MarshalException e) {
+ // TODO: IGNITE-16165 - pass exception correctly
+ throw new RuntimeException("Cannot write", e);
+ }
+ }
+
+ @Override
+ public void writeUnshared(Object obj) throws IOException {
+ // TODO: IGNITE-16165 - implement 'unshared' logic?
+ writeObject0(obj);
+ }
+
+ @Override
+ public void defaultWriteObject() throws IOException {
+ try {
+ defaultFieldsReaderWriter.defaultWriteFields(
+ context.objectCurrentlyWrittenWithWriteObject(),
+ context.descriptorOfObjectCurrentlyWrittenWithWriteObject(),
+ output,
+ context
+ );
+ } catch (MarshalException e) {
+ // TODO: IGNITE-16165 - pass exception correctly
+ throw new RuntimeException("Cannot write", e);
+ }
+ }
+
+ @Override
+ public void useProtocolVersion(int version) {
+ // no op
+ }
+
+ @Override
+ public void reset() throws IOException {
+ // TODO: IGNITE-16165 - erase information about references?
+ }
+
+ @Override
+ public void flush() throws IOException {
+ output.flush();
+ }
+
+ @Override
+ public void close() throws IOException {
+ flush();
+ }
+}
diff --git a/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/ValueReader.java b/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/ValueReader.java
index 184f56f..1ab25a1 100644
--- a/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/ValueReader.java
+++ b/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/ValueReader.java
@@ -17,15 +17,15 @@
package org.apache.ignite.internal.network.serialization.marshal;
-import java.io.DataInput;
+import java.io.DataInputStream;
import java.io.IOException;
/**
- * Knows how to read a value from a {@link DataInput}.
+ * Knows how to read a value from a {@link DataInputStream}.
*/
interface ValueReader<T> {
/**
- * Reads the next value from a {@link DataInput}.
+ * Reads the next value from a {@link DataInputStream}.
*
* @param input from where to read
* @param context unmarshalling context
@@ -33,5 +33,5 @@ interface ValueReader<T> {
* @throws IOException if an I/O problem occurs
* @throws UnmarshalException if another problem (like {@link ClassNotFoundException}) occurs
*/
- T read(DataInput input, UnmarshallingContext context) throws IOException, UnmarshalException;
+ T read(DataInputStream input, UnmarshallingContext context) throws IOException, UnmarshalException;
}
diff --git a/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/ValueWriter.java b/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/ValueWriter.java
index fe18d79..f7b2ede 100644
--- a/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/ValueWriter.java
+++ b/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/ValueWriter.java
@@ -17,15 +17,15 @@
package org.apache.ignite.internal.network.serialization.marshal;
-import java.io.DataOutput;
+import java.io.DataOutputStream;
import java.io.IOException;
/**
- * Knows how to write a value to a {@link DataOutput}.
+ * Knows how to write a value to a {@link DataOutputStream}.
*/
interface ValueWriter<T> {
/**
- * Writes the given value to a {@link DataOutput}.
+ * Writes the given value to a {@link DataOutputStream}.
*
* @param value value to write
* @param output where to write to
@@ -33,5 +33,5 @@ interface ValueWriter<T> {
* @throws IOException if an I/O problem occurs
* @throws MarshalException if another problem occurs
*/
- void write(T value, DataOutput output, MarshallingContext context) throws IOException, MarshalException;
+ void write(T value, DataOutputStream output, MarshallingContext context) throws IOException, MarshalException;
}
diff --git a/modules/network/src/test/java/org/apache/ignite/internal/network/serialization/ClassDescriptorFactoryTest.java b/modules/network/src/test/java/org/apache/ignite/internal/network/serialization/ClassDescriptorFactoryTest.java
index 332097e..73b651e 100644
--- a/modules/network/src/test/java/org/apache/ignite/internal/network/serialization/ClassDescriptorFactoryTest.java
+++ b/modules/network/src/test/java/org/apache/ignite/internal/network/serialization/ClassDescriptorFactoryTest.java
@@ -21,7 +21,10 @@ import static org.apache.ignite.internal.network.serialization.SerializationType
import static org.apache.ignite.internal.network.serialization.SerializationType.EXTERNALIZABLE;
import static org.apache.ignite.internal.network.serialization.SerializationType.SERIALIZABLE;
import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.nullValue;
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@@ -445,31 +448,94 @@ public class ClassDescriptorFactoryTest {
}
@Test
- void shouldSortArbitraryObjectFieldsByClassHierarchyAndLexicographicallyByFieldName() {
- ClassDescriptor descriptor = factory.create(ArbitraryWithFieldNameClashAndOrderPermutation.class);
+ void shouldOnlyConsiderDeclaredFields() {
+ ClassDescriptor descriptor = factory.create(Child.class);
+
+ assertThat(descriptor.fields(), hasSize(1));
assertThat(descriptor.fields().get(0).clazz(), is(String.class));
- assertThat(descriptor.fields().get(0).name(), is("value"));
+ assertThat(descriptor.fields().get(0).name(), is("childValue"));
+ }
+
+ @Test
+ void shouldSortArbitraryObjectFieldsLexicographicallyByFieldName() {
+ ClassDescriptor descriptor = factory.create(ClassWithFieldOrderPermutation.class);
+
+ assertThat(descriptor.fields().get(0).clazz(), is(int.class));
+ assertThat(descriptor.fields().get(0).name(), is("apple"));
assertThat(descriptor.fields().get(1).clazz(), is(int.class));
- assertThat(descriptor.fields().get(1).name(), is("apple"));
+ assertThat(descriptor.fields().get(1).name(), is("banana"));
assertThat(descriptor.fields().get(2).clazz(), is(int.class));
- assertThat(descriptor.fields().get(2).name(), is("banana"));
+ assertThat(descriptor.fields().get(2).name(), is("value"));
+ }
+
+ @SuppressWarnings("ConstantConditions")
+ @Test
+ void detectsSuperClass() {
+ ClassDescriptor descriptor = factory.create(Child.class);
- assertThat(descriptor.fields().get(3).clazz(), is(int.class));
- assertThat(descriptor.fields().get(3).name(), is("value"));
+ assertThat(descriptor.superClassDescriptor().clazz(), is(Parent.class));
+ }
+
+ @Test
+ void detectsSuperClassAsNullIfTheSuperClassIsObject() {
+ ClassDescriptor descriptor = factory.create(ExtendsObject.class);
+
+ assertThat(descriptor.superClassDescriptor(), is(nullValue()));
+ }
+
+ @Test
+ void registersSuperClassDescriptorOnParsingSubClass() {
+ factory.create(Child.class);
+
+ assertDoesNotThrow(() -> context.getRequiredDescriptor(Parent.class));
+ }
+
+ @Test
+ void failsWhenTryingToParseSerializableWithWriteObjectButWithoutReadObject() {
+ Throwable ex = assertThrows(IllegalArgumentException.class, () -> factory.create(SerializableWithWriteObjectOnly.class));
+ assertThat(ex.getMessage(), is("Class must either have both writeObject() and readObject() methods or neither of them: "
+ + SerializableWithWriteObjectOnly.class.getName()));
+ }
+
+ @Test
+ void failsWhenTryingToParseSerializableWithReadObjectButWithoutWriteObject() {
+ Throwable ex = assertThrows(IllegalArgumentException.class, () -> factory.create(SerializableWithReadObjectOnly.class));
+ assertThat(ex.getMessage(), is("Class must either have both writeObject() and readObject() methods or neither of them: "
+ + SerializableWithReadObjectOnly.class.getName()));
}
- @SuppressWarnings("unused")
private static class Parent {
+ @SuppressWarnings("unused")
private String value;
}
+ private static class Child extends Parent {
+ @SuppressWarnings("unused")
+ private String childValue;
+ }
+
@SuppressWarnings("unused")
- private static class ArbitraryWithFieldNameClashAndOrderPermutation extends Parent {
+ private static class ClassWithFieldOrderPermutation {
private int value;
private int banana;
private int apple;
}
+
+ private static class SerializableWithWriteObjectOnly implements Serializable {
+ private void writeObject(ObjectOutputStream oos) {
+ // no-op
+ }
+ }
+
+ private static class SerializableWithReadObjectOnly implements Serializable {
+ private void readObject(ObjectInputStream ois) {
+ // no-op
+ }
+ }
+
+ private static class ExtendsObject {
+ }
}
diff --git a/modules/network/src/test/java/org/apache/ignite/internal/network/serialization/DefaultDescriptorsTest.java b/modules/network/src/test/java/org/apache/ignite/internal/network/serialization/DefaultDescriptorsTest.java
index 5b3bb16..1faf851 100644
--- a/modules/network/src/test/java/org/apache/ignite/internal/network/serialization/DefaultDescriptorsTest.java
+++ b/modules/network/src/test/java/org/apache/ignite/internal/network/serialization/DefaultDescriptorsTest.java
@@ -61,7 +61,6 @@ import static org.apache.ignite.internal.network.serialization.BuiltinType.SINGL
import static org.apache.ignite.internal.network.serialization.BuiltinType.STRING;
import static org.apache.ignite.internal.network.serialization.BuiltinType.STRING_ARRAY;
import static org.apache.ignite.internal.network.serialization.BuiltinType.UUID;
-import static org.apache.ignite.internal.network.serialization.BuiltinType.VOID;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
@@ -119,6 +118,5 @@ public class DefaultDescriptorsTest {
assertEquals(41, LINKED_HASH_MAP.descriptorId());
assertEquals(42, BIT_SET.descriptorId());
assertEquals(43, NULL.descriptorId());
- assertEquals(44, VOID.descriptorId());
}
}
diff --git a/modules/network/src/test/java/org/apache/ignite/internal/network/serialization/marshal/DefaultUserObjectMarshallerCommonTest.java b/modules/network/src/test/java/org/apache/ignite/internal/network/serialization/marshal/DefaultUserObjectMarshallerCommonTest.java
new file mode 100644
index 0000000..78f36f3
--- /dev/null
+++ b/modules/network/src/test/java/org/apache/ignite/internal/network/serialization/marshal/DefaultUserObjectMarshallerCommonTest.java
@@ -0,0 +1,48 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.network.serialization.marshal;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import java.util.Arrays;
+import org.apache.ignite.internal.network.serialization.ClassDescriptorFactory;
+import org.apache.ignite.internal.network.serialization.ClassDescriptorFactoryContext;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Common tests for {@link DefaultUserObjectMarshaller}.
+ */
+class DefaultUserObjectMarshallerCommonTest {
+ private final ClassDescriptorFactoryContext descriptorRegistry = new ClassDescriptorFactoryContext();
+ private final ClassDescriptorFactory descriptorFactory = new ClassDescriptorFactory(descriptorRegistry);
+
+ private final DefaultUserObjectMarshaller marshaller = new DefaultUserObjectMarshaller(descriptorRegistry, descriptorFactory);
+
+ @Test
+ void marshalsAndUnmarshalsBareObject() throws Exception {
+ MarshalledObject marshalled = marshaller.marshal(null);
+
+ byte[] tooManyBytes = Arrays.copyOf(marshalled.bytes(), marshalled.bytes().length + 1);
+
+ UnmarshalException ex = assertThrows(UnmarshalException.class, () -> marshaller.unmarshal(tooManyBytes, descriptorRegistry));
+ assertThat(ex.getMessage(), is("After reading a value, 1 excessive byte(s) still remain"));
+ }
+
+}
diff --git a/modules/network/src/test/java/org/apache/ignite/internal/network/serialization/marshal/DefaultUserObjectMarshallerWithArbitraryObjectsTest.java b/modules/network/src/test/java/org/apache/ignite/internal/network/serialization/marshal/DefaultUserObjectMarshallerWithArbitraryObjectsTest.java
index 51201ab..1075041 100644
--- a/modules/network/src/test/java/org/apache/ignite/internal/network/serialization/marshal/DefaultUserObjectMarshallerWithArbitraryObjectsTest.java
+++ b/modules/network/src/test/java/org/apache/ignite/internal/network/serialization/marshal/DefaultUserObjectMarshallerWithArbitraryObjectsTest.java
@@ -20,6 +20,7 @@ package org.apache.ignite.internal.network.serialization.marshal;
import static java.util.Collections.singletonList;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.hasItems;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
@@ -103,6 +104,16 @@ class DefaultUserObjectMarshallerWithArbitraryObjectsTest {
}
@Test
+ void usesDescriptorsOfAllAncestors() throws Exception {
+ MarshalledObject marshalled = marshaller.marshal(new SimpleChild("answer", 42));
+
+ assertThat(marshalled.usedDescriptors(), hasItems(
+ descriptorRegistry.getRequiredDescriptor(Parent.class),
+ descriptorRegistry.getRequiredDescriptor(SimpleChild.class)
+ ));
+ }
+
+ @Test
void marshalsAndUnmarshalsClassInstancesHavingNestedArbitraryObjects() throws Exception {
MarshalledObject marshalled = marshaller.marshal(new WithArbitraryClassField(new Simple(42)));
diff --git a/modules/network/src/test/java/org/apache/ignite/internal/network/serialization/marshal/DefaultUserObjectMarshallerWithBuiltinsTest.java b/modules/network/src/test/java/org/apache/ignite/internal/network/serialization/marshal/DefaultUserObjectMarshallerWithBuiltinsTest.java
index 937986f..80f47ed 100644
--- a/modules/network/src/test/java/org/apache/ignite/internal/network/serialization/marshal/DefaultUserObjectMarshallerWithBuiltinsTest.java
+++ b/modules/network/src/test/java/org/apache/ignite/internal/network/serialization/marshal/DefaultUserObjectMarshallerWithBuiltinsTest.java
@@ -94,12 +94,12 @@ class DefaultUserObjectMarshallerWithBuiltinsTest {
void marshalsBareObjectWithCorrectDescriptorIdInMarshalledRepresentation() throws Exception {
MarshalledObject marshalled = marshaller.marshal(new Object());
- assertThat(readType(marshalled), is(BuiltinType.BARE_OBJECT.descriptorId()));
+ assertThat(readDescriptorId(marshalled), is(BuiltinType.BARE_OBJECT.descriptorId()));
}
- private int readType(MarshalledObject marshalled) throws IOException {
+ private int readDescriptorId(MarshalledObject marshalled) throws IOException {
try (var dis = new DataInputStream(new ByteArrayInputStream(marshalled.bytes()))) {
- return dis.readInt();
+ return ProtocolMarshalling.readDescriptorOrCommandId(dis);
}
}
@@ -129,8 +129,7 @@ class DefaultUserObjectMarshallerWithBuiltinsTest {
Object unmarshalled = marshaller.unmarshal(marshalled.bytes(), descriptorRegistry);
assertThat(unmarshalled, is(equalTo(typeValue.value)));
- if (typeValue.builtinType != BuiltinType.VOID && typeValue.builtinType != BuiltinType.NULL
- && typeValue.value.getClass().isArray()) {
+ if (typeValue.builtinType != BuiltinType.NULL && typeValue.value.getClass().isArray()) {
assertThat(unmarshalled, is(notNullValue()));
assertThat(unmarshalled.getClass().getComponentType(), is(typeValue.value.getClass().getComponentType()));
}
@@ -200,8 +199,7 @@ class DefaultUserObjectMarshallerWithBuiltinsTest {
BuiltinType.ENUM_ARRAY
),
builtInTypeValue(BitSet.valueOf(new long[]{42, 43}), BitSet.class, BuiltinType.BIT_SET),
- builtInTypeValue(null, Null.class, BuiltinType.NULL),
- builtInTypeValue(null, Void.class, BuiltinType.VOID)
+ builtInTypeValue(null, Null.class, BuiltinType.NULL)
).map(Arguments::of);
}
@@ -253,7 +251,7 @@ class DefaultUserObjectMarshallerWithBuiltinsTest {
void marshalsBuiltInTypesWithCorrectDescriptorIdsInMarshalledRepresentation(BuiltInTypeValue typeValue) throws Exception {
MarshalledObject marshalled = marshaller.marshal(typeValue.value, typeValue.valueClass);
- assertThat(readType(marshalled), is(equalTo(typeValue.builtinType.descriptorId())));
+ assertThat(readDescriptorId(marshalled), is(equalTo(typeValue.builtinType.descriptorId())));
}
static Stream<Arguments> builtInTypes() {
diff --git a/modules/network/src/test/java/org/apache/ignite/internal/network/serialization/marshal/DefaultUserObjectMarshallerWithExternalizableTest.java b/modules/network/src/test/java/org/apache/ignite/internal/network/serialization/marshal/DefaultUserObjectMarshallerWithExternalizableTest.java
index 8eb0b46..88c24eb 100644
--- a/modules/network/src/test/java/org/apache/ignite/internal/network/serialization/marshal/DefaultUserObjectMarshallerWithExternalizableTest.java
+++ b/modules/network/src/test/java/org/apache/ignite/internal/network/serialization/marshal/DefaultUserObjectMarshallerWithExternalizableTest.java
@@ -17,16 +17,26 @@
package org.apache.ignite.internal.network.serialization.marshal;
+import static org.apache.ignite.internal.network.serialization.marshal.Throwables.causalChain;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.hasItem;
+import static org.hamcrest.Matchers.hasProperty;
+import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import java.io.ByteArrayInputStream;
+import java.io.DataInputStream;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
+import java.io.ObjectInputStream;
import java.io.ObjectOutput;
+import java.io.ObjectOutputStream;
+import java.io.Serializable;
import java.util.Set;
import org.apache.ignite.internal.network.serialization.ClassDescriptor;
import org.apache.ignite.internal.network.serialization.ClassDescriptorFactory;
@@ -56,13 +66,16 @@ class DefaultUserObjectMarshallerWithExternalizableTest {
@Test
void marshalsAndUnmarshalsExternalizable() throws Exception {
- MarshalledObject marshalled = marshaller.marshal(new SimpleExternalizable(42));
-
- SimpleExternalizable unmarshalled = unmarshalNonNull(marshalled);
+ SimpleExternalizable unmarshalled = marshalAndUnmarshalNonNull(new SimpleExternalizable(42));
assertThat(unmarshalled.intValue, is(42));
}
+ private <T> T marshalAndUnmarshalNonNull(Object object) throws MarshalException, UnmarshalException {
+ MarshalledObject marshalled = marshaller.marshal(object);
+ return unmarshalNonNull(marshalled);
+ }
+
private <T> T unmarshalNonNull(MarshalledObject marshalled) throws UnmarshalException {
T unmarshalled = marshaller.unmarshal(marshalled.bytes(), descriptorRegistry);
@@ -73,27 +86,21 @@ class DefaultUserObjectMarshallerWithExternalizableTest {
@Test
void appliesWriteReplaceOnExternalizable() throws Exception {
- MarshalledObject marshalled = marshaller.marshal(new ExternalizableWithWriteReplace(42));
-
- SimpleExternalizable unmarshalled = unmarshalNonNull(marshalled);
+ SimpleExternalizable unmarshalled = marshalAndUnmarshalNonNull(new ExternalizableWithWriteReplace(42));
assertThat(unmarshalled.intValue, is(equalTo(42 + WRITE_REPLACE_INCREMENT)));
}
@Test
void appliesReadResolveOnExternalizable() throws Exception {
- MarshalledObject marshalled = marshaller.marshal(new ExternalizableWithReadResolve(42));
-
- SimpleExternalizable unmarshalled = unmarshalNonNull(marshalled);
+ SimpleExternalizable unmarshalled = marshalAndUnmarshalNonNull(new ExternalizableWithReadResolve(42));
assertThat(unmarshalled.intValue, is(equalTo(42 + READ_RESOLVE_INCREMENT)));
}
@Test
void appliesBothWriteReplaceAndReadResolveOnExternalizable() throws Exception {
- MarshalledObject marshalled = marshaller.marshal(new ExternalizableWithWriteReplaceReadResolve(42));
-
- SimpleExternalizable unmarshalled = unmarshalNonNull(marshalled);
+ SimpleExternalizable unmarshalled = marshalAndUnmarshalNonNull(new ExternalizableWithWriteReplaceReadResolve(42));
assertThat(unmarshalled.intValue, is(equalTo(42 + WRITE_REPLACE_INCREMENT + READ_RESOLVE_INCREMENT)));
}
@@ -132,6 +139,99 @@ class DefaultUserObjectMarshallerWithExternalizableTest {
assertThat(unmarshalled, is(nullValue()));
}
+ @Test
+ void appliesWriteReplaceOnExternalizableRecursively() throws Exception {
+ Object result = marshalAndUnmarshalNonNull(new ExternalizableWithWriteReplaceChain1(0));
+
+ assertThat(result, is(instanceOf(Integer.class)));
+ assertThat(result, is(3));
+ }
+
+ @Test
+ void stopsApplyingWriteReplaceOnExternalizableWhenReplacementIsInstanceOfSameClass() throws Exception {
+ ExternalizableWithWriteReplaceWithSameClass result = marshalAndUnmarshalNonNull(new ExternalizableWithWriteReplaceWithSameClass(0));
+
+ assertThat(result.intValue, is(1));
+ }
+
+ @Test
+ void causesInfiniteRecursionOnExternalizableWithIndirectWriteReplaceCycle() {
+ assertThrows(StackOverflowError.class, () -> marshalAndUnmarshalNonNull(new ExternalizableWithWriteReplaceCycle1(0)));
+ }
+
+ /**
+ * Java Serialization applies writeReplace() repeatedly, but it only applies readResolve() once.
+ * So we are emulating this behavior.
+ *
+ * @throws Exception if something goes wrong
+ */
+ @Test
+ void onlyAppliesFirstReadResolveOnExternalizable() throws Exception {
+ Object result = marshalAndUnmarshalNonNull(new ExternalizableWithReadResolveChain1(0));
+
+ assertThat(result, is(instanceOf(ExternalizableWithReadResolveChain2.class)));
+ }
+
+ @Test
+ void defaultWriteObjectShouldFailInsideWriteExternal() {
+ MarshalException ex = assertThrows(
+ MarshalException.class,
+ () -> marshaller.marshal(new ExternalizableWithDefaultWriteObjectCallInWriteObjectMethod())
+ );
+ assertThat(causalChain(ex), hasItem(hasProperty("message", equalTo("not in call to writeObject"))));
+ }
+
+ @Test
+ void defaultReadObjectShouldFailInsideReadExternal() {
+ UnmarshalException ex = assertThrows(
+ UnmarshalException.class,
+ () -> marshalAndUnmarshalNonNull(new ExternalizableWithDefaultReadObjectCallInReadObjectMethod())
+ );
+ assertThat(causalChain(ex), hasItem(hasProperty("message", equalTo("not in call to readObject"))));
+ }
+
+ @Test
+ void defaultWriteObjectShouldFailInsideWriteExternalInsideWriteObject() {
+ var payload = new ExternalizableWithDefaultWriteObjectCallInWriteObjectMethod();
+ var object = new SerializableWithDefaultReadWriteObjectCallInReadWriteOverride(payload);
+
+ MarshalException ex = assertThrows(MarshalException.class, () -> marshaller.marshal(object));
+ assertThat(causalChain(ex), hasItem(hasProperty("message", equalTo("not in call to writeObject"))));
+ }
+
+ @Test
+ void defaultReadObjectShouldFailInsideReadExternalInsideReadObject() {
+ var payload = new ExternalizableWithDefaultReadObjectCallInReadObjectMethod();
+ var object = new SerializableWithDefaultReadWriteObjectCallInReadWriteOverride(payload);
+
+ UnmarshalException ex = assertThrows(UnmarshalException.class, () -> marshalAndUnmarshalNonNull(object));
+ assertThat(causalChain(ex), hasItem(hasProperty("message", equalTo("not in call to readObject"))));
+ }
+
+ @Test
+ void writingObjectInsideWriteExternalMarshalsTheObjectInOurFormat() throws Exception {
+ MarshalledObject marshalled = marshaller.marshal(new ExternalizableWritingAndReadingObject(new IntHolder(42)));
+
+ DataInputStream dis = new DataInputStream(new ByteArrayInputStream(marshalled.bytes()));
+ ProtocolMarshalling.readDescriptorOrCommandId(dis);
+ ProtocolMarshalling.readObjectId(dis);
+
+ byte[] externalBytes = dis.readAllBytes();
+
+ IntHolder nested = marshaller.unmarshal(externalBytes, descriptorRegistry);
+ assertThat(nested, is(notNullValue()));
+ assertThat(nested.value, is(42));
+ }
+
+ @Test
+ void marshalsAndUnmarshalsExternalizableWritingReadingObjectInsideWriteReadExternal() throws Exception {
+ ExternalizableWritingAndReadingObject object = new ExternalizableWritingAndReadingObject(new IntHolder(42));
+
+ ExternalizableWritingAndReadingObject unmarshalled = marshalAndUnmarshalNonNull(object);
+
+ assertThat(unmarshalled.intHolder.value, is(42));
+ }
+
/**
* An {@link Externalizable} that does not have {@code writeReplace()}/{@code readResolve()} methods.
*/
@@ -145,11 +245,13 @@ class DefaultUserObjectMarshallerWithExternalizableTest {
this.intValue = intValue;
}
+ /** {@inheritDoc} */
@Override
public void writeExternal(ObjectOutput out) throws IOException {
out.writeInt(-intValue);
}
+ /** {@inheritDoc} */
@Override
public void readExternal(ObjectInput in) throws IOException {
intValue = -in.readInt();
@@ -178,7 +280,7 @@ class DefaultUserObjectMarshallerWithExternalizableTest {
}
private Object readResolve() {
- return new ExternalizableWithWriteReplaceReadResolve(intValue + 1_000);
+ return new ExternalizableWithReadResolve(intValue + 1_000);
}
}
@@ -213,11 +315,13 @@ class DefaultUserObjectMarshallerWithExternalizableTest {
return new SimpleExternalizable(intValue);
}
+ /** {@inheritDoc} */
@Override
public void writeExternal(ObjectOutput out) throws IOException {
out.writeInt(intValue);
}
+ /** {@inheritDoc} */
@Override
public void readExternal(ObjectInput in) throws IOException {
intValue = in.readInt();
@@ -249,4 +353,177 @@ class DefaultUserObjectMarshallerWithExternalizableTest {
return null;
}
}
+
+ private static class ExternalizableWithWriteReplaceChain1 extends SimpleExternalizable {
+ public ExternalizableWithWriteReplaceChain1() {
+ }
+
+ public ExternalizableWithWriteReplaceChain1(int value) {
+ super(value);
+ }
+
+ private Object writeReplace() {
+ return new ExternalizableWithWriteReplaceChain2(intValue + 1);
+ }
+ }
+
+ private static class ExternalizableWithWriteReplaceChain2 extends SimpleExternalizable {
+ public ExternalizableWithWriteReplaceChain2() {
+ }
+
+ public ExternalizableWithWriteReplaceChain2(int value) {
+ super(value);
+ }
+
+ private Object writeReplace() {
+ return intValue + 2;
+ }
+ }
+
+ private static class ExternalizableWithWriteReplaceWithSameClass extends SimpleExternalizable {
+ public ExternalizableWithWriteReplaceWithSameClass() {
+ }
+
+ public ExternalizableWithWriteReplaceWithSameClass(int value) {
+ super(value);
+ }
+
+ private Object writeReplace() {
+ return new ExternalizableWithWriteReplaceWithSameClass(intValue + 1);
+ }
+ }
+
+ private static class ExternalizableWithWriteReplaceCycle1 extends SimpleExternalizable {
+ public ExternalizableWithWriteReplaceCycle1() {
+ }
+
+ public ExternalizableWithWriteReplaceCycle1(int intValue) {
+ super(intValue);
+ }
+
+ private Object writeReplace() {
+ return new ExternalizableWithWriteReplaceCycle2(intValue);
+ }
+ }
+
+ private static class ExternalizableWithWriteReplaceCycle2 extends SimpleExternalizable {
+ public ExternalizableWithWriteReplaceCycle2() {
+ }
+
+ public ExternalizableWithWriteReplaceCycle2(int intValue) {
+ super(intValue);
+ }
+
+ private Object writeReplace() {
+ return new ExternalizableWithWriteReplaceCycle1(intValue);
+ }
+ }
+
+ private static class ExternalizableWithReadResolveChain1 extends SimpleExternalizable {
+ public ExternalizableWithReadResolveChain1() {
+ }
+
+ public ExternalizableWithReadResolveChain1(int value) {
+ super(value);
+ }
+
+ private Object readResolve() {
+ return new ExternalizableWithReadResolveChain2(intValue + 1);
+ }
+ }
+
+ private static class ExternalizableWithReadResolveChain2 extends SimpleExternalizable {
+ public ExternalizableWithReadResolveChain2() {
+ }
+
+ public ExternalizableWithReadResolveChain2(int value) {
+ super(value);
+ }
+
+ private Object readResolve() {
+ return intValue + 2;
+ }
+ }
+
+ private static class ExternalizableWithDefaultWriteObjectCallInWriteObjectMethod implements Externalizable {
+ public ExternalizableWithDefaultWriteObjectCallInWriteObjectMethod() {
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public void writeExternal(ObjectOutput out) throws IOException {
+ ObjectOutputStream stream = (ObjectOutputStream) out;
+ stream.defaultWriteObject();
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public void readExternal(ObjectInput in) {
+ // no op
+ }
+ }
+
+ private static class ExternalizableWithDefaultReadObjectCallInReadObjectMethod implements Externalizable {
+ public ExternalizableWithDefaultReadObjectCallInReadObjectMethod() {
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public void writeExternal(ObjectOutput out) {
+ // no op
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
+ ObjectInputStream stream = (ObjectInputStream) in;
+ stream.defaultReadObject();
+ }
+ }
+
+ private static class SerializableWithDefaultReadWriteObjectCallInReadWriteOverride implements Serializable {
+ @SuppressWarnings({"FieldCanBeLocal", "unused"})
+ private final Object object;
+
+ public SerializableWithDefaultReadWriteObjectCallInReadWriteOverride(Object object) {
+ this.object = object;
+ }
+
+ private void writeObject(ObjectOutputStream stream) throws IOException {
+ stream.defaultWriteObject();
+ }
+
+ private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
+ stream.defaultReadObject();
+ }
+ }
+
+ private static class ExternalizableWritingAndReadingObject implements Externalizable {
+ private IntHolder intHolder;
+
+ public ExternalizableWritingAndReadingObject() {
+ }
+
+ public ExternalizableWritingAndReadingObject(IntHolder intHolder) {
+ this.intHolder = intHolder;
+ }
+
+ @Override
+ public void writeExternal(ObjectOutput out) throws IOException {
+ out.writeObject(intHolder);
+ }
+
+ @Override
+ public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
+ intHolder = (IntHolder) in.readObject();
+ }
+ }
+
+ private static class IntHolder {
+ private final int value;
+
+ private IntHolder(int value) {
+ this.value = value;
+ }
+ }
}
diff --git a/modules/network/src/test/java/org/apache/ignite/internal/network/serialization/marshal/DefaultUserObjectMarshallerWithSerializableTest.java b/modules/network/src/test/java/org/apache/ignite/internal/network/serialization/marshal/DefaultUserObjectMarshallerWithSerializableTest.java
new file mode 100644
index 0000000..97e86e7
--- /dev/null
+++ b/modules/network/src/test/java/org/apache/ignite/internal/network/serialization/marshal/DefaultUserObjectMarshallerWithSerializableTest.java
@@ -0,0 +1,809 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.network.serialization.marshal;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.hasItem;
+import static org.hamcrest.Matchers.instanceOf;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.lessThan;
+import static org.hamcrest.Matchers.not;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.hamcrest.Matchers.nullValue;
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.DataInput;
+import java.io.DataInputStream;
+import java.io.Externalizable;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.Serializable;
+import java.util.Objects;
+import java.util.Set;
+import java.util.function.Consumer;
+import java.util.stream.Stream;
+import org.apache.ignite.internal.network.serialization.BuiltinType;
+import org.apache.ignite.internal.network.serialization.ClassDescriptor;
+import org.apache.ignite.internal.network.serialization.ClassDescriptorFactory;
+import org.apache.ignite.internal.network.serialization.ClassDescriptorFactoryContext;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+
+/**
+ * Tests for how {@link DefaultUserObjectMarshaller} handles {@link java.io.Serializable}s (but not {@link Externalizable}s).
+ */
+class DefaultUserObjectMarshallerWithSerializableTest {
+ private final ClassDescriptorFactoryContext descriptorRegistry = new ClassDescriptorFactoryContext();
+ private final ClassDescriptorFactory descriptorFactory = new ClassDescriptorFactory(descriptorRegistry);
+
+ private final DefaultUserObjectMarshaller marshaller = new DefaultUserObjectMarshaller(descriptorRegistry, descriptorFactory);
+
+ private static final int WRITE_REPLACE_INCREMENT = 1_000_000;
+ private static final int READ_RESOLVE_INCREMENT = 1_000;
+
+ private static final int WRITE_OBJECT_INCREMENT = 10;
+ private static final int READ_OBJECT_INCREMENT = 100;
+
+ private static final int CHILD_WRITE_OBJECT_INCREMENT = 3;
+ private static final int CHILD_READ_OBJECT_INCREMENT = 6;
+
+ /** This is static so that writeObject()/readObject() can easily find it. */
+ private static ReaderAndWriter<?> readerAndWriter;
+
+ /** Static access to the marshaller (for using in parameterized tests) */
+ private static UserObjectMarshaller staticMarshaller;
+ /** Static access to the registry (for using in parameterized tests) */
+ private static ClassDescriptorFactoryContext staticDescriptorRegistry;
+
+ @BeforeEach
+ void initStatics() {
+ staticMarshaller = marshaller;
+ staticDescriptorRegistry = descriptorRegistry;
+ }
+
+ @Test
+ void marshalsAndUnmarshalsSerializable() throws Exception {
+ SimpleSerializable unmarshalled = marshalAndUnmarshalNonNull(new SimpleSerializable(42));
+
+ assertThat(unmarshalled.intValue, is(42));
+ }
+
+ private <T> T marshalAndUnmarshalNonNull(Object object) throws MarshalException, UnmarshalException {
+ MarshalledObject marshalled = marshaller.marshal(object);
+ return unmarshalNonNull(marshalled);
+ }
+
+ private <T> T unmarshalNonNull(MarshalledObject marshalled) throws UnmarshalException {
+ T unmarshalled = marshaller.unmarshal(marshalled.bytes(), descriptorRegistry);
+
+ assertThat(unmarshalled, is(notNullValue()));
+
+ return unmarshalled;
+ }
+
+ @Test
+ void appliesWriteReplaceOnSerializable() throws Exception {
+ SimpleSerializable unmarshalled = marshalAndUnmarshalNonNull(new SerializableWithWriteReplace(42));
+
+ assertThat(unmarshalled.intValue, is(equalTo(42 + WRITE_REPLACE_INCREMENT)));
+ }
+
+ @Test
+ void appliesReadResolveOnSerializable() throws Exception {
+ SimpleSerializable unmarshalled = marshalAndUnmarshalNonNull(new SerializableWithReadResolve(42));
+
+ assertThat(unmarshalled.intValue, is(equalTo(42 + READ_RESOLVE_INCREMENT)));
+ }
+
+ @Test
+ void appliesBothWriteReplaceAndReadResolveOnSerializable() throws Exception {
+ SimpleSerializable unmarshalled = marshalAndUnmarshalNonNull(new SerializableWithWriteReplaceReadResolve(42));
+
+ assertThat(unmarshalled.intValue, is(equalTo(42 + WRITE_REPLACE_INCREMENT + READ_RESOLVE_INCREMENT)));
+ }
+
+ @Test
+ void usesDescriptorOfReplacementWhenSerializableIsReplacedWithSomethingDifferent() throws Exception {
+ MarshalledObject marshalled = marshaller.marshal(new SerializableWithReplaceWithSimple(42));
+
+ ClassDescriptor originalDescriptor = descriptorRegistry.getRequiredDescriptor(SerializableWithReplaceWithSimple.class);
+ assertThat(marshalled.usedDescriptors(), not(hasItem(originalDescriptor)));
+
+ ClassDescriptor replacementDescriptor = descriptorRegistry.getRequiredDescriptor(SimpleSerializable.class);
+ assertThat(marshalled.usedDescriptors(), hasItem(replacementDescriptor));
+ }
+
+ @Test
+ void marshalsSerializableWithReplaceWithNull() throws Exception {
+ MarshalledObject marshalled = marshaller.marshal(new SerializableWithReplaceWithNull(42));
+
+ SimpleSerializable unmarshalled = marshaller.unmarshal(marshalled.bytes(), descriptorRegistry);
+
+ assertThat(unmarshalled, is(nullValue()));
+ }
+
+ @Test
+ void onlyUsesDescriptorOfNullWhenSerializableIsReplacedWithNull() throws Exception {
+ MarshalledObject marshalled = marshaller.marshal(new SerializableWithReplaceWithNull(42));
+
+ ClassDescriptor replacementDescriptor = descriptorRegistry.getNullDescriptor();
+ assertThat(marshalled.usedDescriptors(), equalTo(Set.of(replacementDescriptor)));
+ }
+
+ @Test
+ void unmarshalsSerializableWithResolveWithNull() throws Exception {
+ MarshalledObject marshalled = marshaller.marshal(new SerializableWithResolveWithNull(42));
+
+ SimpleSerializable unmarshalled = marshaller.unmarshal(marshalled.bytes(), descriptorRegistry);
+
+ assertThat(unmarshalled, is(nullValue()));
+ }
+
+ @Test
+ void appliesWriteReplaceOnExternalizableRecursively() throws Exception {
+ Object result = marshalAndUnmarshalNonNull(new SerializableWithWriteReplaceChain1(0));
+
+ assertThat(result, is(instanceOf(Integer.class)));
+ assertThat(result, is(3));
+ }
+
+ @Test
+ void stopsApplyingWriteReplaceOnExternalizableWhenReplacementIsInstanceOfSameClass() throws Exception {
+ SerializableWithWriteReplaceWithSameClass result = marshalAndUnmarshalNonNull(new SerializableWithWriteReplaceWithSameClass(0));
+
+ assertThat(result.intValue, is(1));
+ }
+
+ @Test
+ void causesInfiniteRecursionOnExternalizableWithIndirectWriteReplaceCycle() {
+ assertThrows(StackOverflowError.class, () -> marshalAndUnmarshalNonNull(new SerializableWithWriteReplaceCycle1(0)));
+ }
+
+ /**
+ * Java Serialization applies writeReplace() repeatedly, but it only applies readResolve() once.
+ * So we are emulating this behavior.
+ *
+ * @throws Exception if something goes wrong
+ */
+ @Test
+ void onlyAppliesFirstReadResolveOnExternalizable() throws Exception {
+ Object result = marshalAndUnmarshalNonNull(new SerializableWithReadResolveChain1(0));
+
+ assertThat(result, is(instanceOf(SerializableWithReadResolveChain2.class)));
+ }
+
+ @Test
+ void usesWriteObjectAndReadObject() throws Exception {
+ SerializableWithWriteReadOverride result = marshalAndUnmarshalNonNull(new SerializableWithWriteReadOverride(42));
+
+ assertThat(result.value, is(42 + WRITE_OBJECT_INCREMENT + READ_OBJECT_INCREMENT));
+ }
+
+ @Test
+ void doesNotWriteDefaultFieldValuesDataIfWriteReadOverrideIsPresent() throws Exception {
+ SerializableWithNoOpWriteReadOverride result = marshalAndUnmarshalNonNull(new SerializableWithNoOpWriteReadOverride(42));
+
+ assertThat(result.value, is(0));
+ }
+
+ @ParameterizedTest
+ @MethodSource("readWriteSpecs")
+ <T> void objectOutputStreamFromWriteObjectWritesUsingOurFormat(ReadWriteSpec<T> spec) throws Exception {
+ readerAndWriter = new ReaderAndWriter<>(spec.writer, spec.reader);
+
+ WithCustomizableOverride<T> original = new WithCustomizableOverride<>();
+ MarshalledObject marshalled = marshaller.marshal(original);
+
+ byte[] overrideBytes = readOverrideBytes(marshalled);
+ T overrideValue = spec.parseOverrideValue(overrideBytes);
+
+ spec.assertUnmarshalledValue(overrideValue);
+ }
+
+ @ParameterizedTest
+ @MethodSource("readWriteSpecs")
+ <T> void supportsReadsAndWritesInWriteObjectAndReadObject(ReadWriteSpec<T> spec) throws Exception {
+ readerAndWriter = new ReaderAndWriter<>(spec.writer, spec.reader);
+
+ WithCustomizableOverride<T> original = new WithCustomizableOverride<>();
+ WithCustomizableOverride<T> unmarshalled = marshalAndUnmarshalNonNull(original);
+
+ spec.assertUnmarshalledValue(unmarshalled.value);
+ }
+
+ private byte[] readOverrideBytes(MarshalledObject marshalled) throws IOException {
+ DataInputStream dis = new DataInputStream(new ByteArrayInputStream(marshalled.bytes()));
+
+ ProtocolMarshalling.readDescriptorOrCommandId(dis);
+ ProtocolMarshalling.readObjectId(dis);
+
+ return dis.readAllBytes();
+ }
+
+ private void assertThatDrained(DataInputStream dis) throws IOException {
+ assertThat(dis.read(), is(lessThan(0)));
+ }
+
+ private static Stream<Arguments> readWriteSpecs() {
+ return Stream.of(
+ // the following test perfect pairs like writeByte()/readByte()
+ new ReadWriteSpec<>("data byte", oos -> oos.writeByte(42), DataInput::readByte, (byte) 42),
+ new ReadWriteSpec<>("short", oos -> oos.writeShort(42), DataInput::readShort, (short) 42),
+ new ReadWriteSpec<>("int", oos -> oos.writeInt(42), DataInput::readInt, 42),
+ new ReadWriteSpec<>("long", oos -> oos.writeLong(42), DataInput::readLong, 42L),
+ new ReadWriteSpec<>("float", oos -> oos.writeFloat(42.0f), DataInput::readFloat, 42.0f),
+ new ReadWriteSpec<>("double", oos -> oos.writeDouble(42.0), DataInput::readDouble, 42.0),
+ new ReadWriteSpec<>("char", oos -> oos.writeChar('a'), DataInput::readChar, 'a'),
+ new ReadWriteSpec<>("boolean", oos -> oos.writeBoolean(true), DataInput::readBoolean, true),
+ new ReadWriteSpec<>("stream byte", oos -> oos.write(42), ObjectInputStream::read, DataInputStream::read, 42),
+ new ReadWriteSpec<>("byte array", oos -> oos.write(new byte[]{42, 43}), is -> readBytes(is, 2), is -> readBytes(is, 2), new byte[]{42, 43}),
+ new ReadWriteSpec<>("byte array range", oos -> oos.write(new byte[]{42, 43}, 0, 2), is -> readRange(is, 2), is -> readRange(is, 2), new byte[]{42, 43}),
+ new ReadWriteSpec<>("UTF", oos -> oos.writeUTF("Привет"), DataInput::readUTF, "Привет"),
+ new ReadWriteSpec<>("object", oos -> oos.writeObject(new SimpleNonSerializable(42)), ObjectInputStream::readObject, DefaultUserObjectMarshallerWithSerializableTest::consumeAndUnmarshal, new SimpleNonSerializable(42)),
+ new ReadWriteSpec<>("unshared", oos -> oos.writeUnshared(new SimpleNonSerializable(42)), ObjectInputStream::readUnshared, DefaultUserObjectMarshallerWithSerializableTest::consumeAndUnmarshal, new SimpleNonSerializable(42)),
+ // the following test writing methods only (readers are just to help testing them)
+ new ReadWriteSpec<>("writeBytes", oos -> oos.writeBytes("abc"), input -> readBytesFully(input, 3), "abc".getBytes()),
+ new ReadWriteSpec<>("writeChars", oos -> oos.writeChars("a"), DataInput::readChar, 'a'),
+ // the following test reading methods only (writers are just to help testing them)
+ new ReadWriteSpec<>("readFully", oos -> oos.write(new byte[]{42, 43}), input -> readBytesFully(input, 2), new byte[]{42, 43}),
+ new ReadWriteSpec<>("readFully range", oos -> oos.write(new byte[]{42, 43}), input -> readBytesRangeFully(input, 2), new byte[]{42, 43}),
+ new ReadWriteSpec<>("readUnsignedByte", oos -> oos.writeByte(42), DataInput::readUnsignedByte, 42),
+ new ReadWriteSpec<>("readUnsignedShort", oos -> oos.writeShort(42), DataInput::readUnsignedShort, 42),
+ new ReadWriteSpec<>("readAllBytes", oos -> oos.write(new byte[]{42, 43}), InputStream::readAllBytes, InputStream::readAllBytes, new byte[]{42, 43}),
+ new ReadWriteSpec<>("readNBytes", oos -> oos.write(new byte[]{42, 43}), ois -> ois.readNBytes(2), dis -> readBytesFully(dis, 2), new byte[]{42, 43}),
+ new ReadWriteSpec<>("readNBytes range", oos -> oos.write(new byte[]{42, 43}), ois -> readNBytesRange(ois, 2), dis -> readNBytesRange(dis, 2), new byte[]{42, 43})
+ ).map(Arguments::of);
+ }
+
+ // TODO: IGNITE-16240 - implement putFields()/writeFields()
+ // TODO: IGNITE-16240 - implement readFields()
+
+ private static byte[] readBytes(InputStream is, int count) throws IOException {
+ byte[] bytes = new byte[count];
+ int read = is.read(bytes);
+ assertThat(read, is(count));
+ return bytes;
+ }
+
+ private static byte[] readRange(InputStream is, int count) throws IOException {
+ byte[] bytes = new byte[count];
+ int read = is.read(bytes, 0, count);
+ assertThat(read, is(count));
+ return bytes;
+ }
+
+ private static byte[] readBytesFully(DataInput is, int count) throws IOException {
+ byte[] bytes = new byte[count];
+ is.readFully(bytes);
+ return bytes;
+ }
+
+ private static byte[] readBytesRangeFully(DataInput is, int count) throws IOException {
+ byte[] bytes = new byte[count];
+ is.readFully(bytes, 0, count);
+ return bytes;
+ }
+
+ private static byte[] readNBytesRange(InputStream is, int count) throws IOException {
+ byte[] bytes = new byte[count];
+ is.readNBytes(bytes, 0, count);
+ return bytes;
+ }
+
+ private static <T> T consumeAndUnmarshal(DataInputStream stream) throws IOException {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ stream.transferTo(baos);
+
+ try {
+ return staticMarshaller.unmarshal(baos.toByteArray(), staticDescriptorRegistry);
+ } catch (UnmarshalException e) {
+ throw new RuntimeException("Unmarshalling failed", e);
+ }
+ }
+
+ @Test
+ void supportsFlushInsideWriteObject() {
+ readerAndWriter = new ReaderAndWriter<>(ObjectOutputStream::flush, ois -> null);
+
+ WithCustomizableOverride<?> original = new WithCustomizableOverride<>();
+
+ assertDoesNotThrow(() -> marshalAndUnmarshalNonNull(original));
+ }
+
+ @Test
+ void supportsResetInsideWriteObject() {
+ readerAndWriter = new ReaderAndWriter<>(ObjectOutputStream::reset, ois -> null);
+
+ WithCustomizableOverride<?> original = new WithCustomizableOverride<>();
+
+ assertDoesNotThrow(() -> marshalAndUnmarshalNonNull(original));
+ }
+
+ @Test
+ void supportsUseProtocolVersionInsideWriteObject() {
+ readerAndWriter = new ReaderAndWriter<>(oos -> oos.useProtocolVersion(1), ois -> null);
+
+ WithCustomizableOverride<?> original = new WithCustomizableOverride<>();
+
+ assertDoesNotThrow(() -> marshalAndUnmarshalNonNull(original));
+ }
+
+ @Test
+ void supportsSkipInsideReadObject() throws Exception {
+ readerAndWriter = new ReaderAndWriter<>(oos -> oos.write(new byte[]{42, 43}), ois -> {
+ assertThat(ois.skip(1), is(1L));
+ return ois.readByte();
+ });
+
+ WithCustomizableOverride<?> original = new WithCustomizableOverride<>();
+
+ WithCustomizableOverride<Byte> unmarshalled = marshalAndUnmarshalNonNull(original);
+ assertThat(unmarshalled.value, is((byte) 43));
+ }
+
+ @Test
+ void supportsSkipBytesInsideReadObject() throws Exception {
+ readerAndWriter = new ReaderAndWriter<>(oos -> oos.write(new byte[]{42, 43}), ois -> {
+ assertThat(ois.skipBytes(1), is(1));
+ return ois.readByte();
+ });
+
+ WithCustomizableOverride<?> original = new WithCustomizableOverride<>();
+
+ WithCustomizableOverride<Byte> unmarshalled = marshalAndUnmarshalNonNull(original);
+ assertThat(unmarshalled.value, is((byte) 43));
+ }
+
+ @Test
+ void supportsAvailableInsideReadObject() {
+ readerAndWriter = new ReaderAndWriter<>(oos -> {}, ObjectInputStream::available);
+
+ WithCustomizableOverride<?> original = new WithCustomizableOverride<>();
+
+ assertDoesNotThrow(() -> marshalAndUnmarshalNonNull(original));
+ }
+
+ @Test
+ void supportsMarkAndResetInsideReadObject() {
+ readerAndWriter = new ReaderAndWriter<>(oos -> {}, ois -> {
+ //noinspection ResultOfMethodCallIgnored
+ assertFalse(ois.markSupported());
+ ois.mark(1);
+ try {
+ ois.reset();
+ } catch (IOException e) {
+ // ignore mark/reset not supported
+ }
+ return null;
+ });
+
+ WithCustomizableOverride<?> original = new WithCustomizableOverride<>();
+
+ assertDoesNotThrow(() -> marshalAndUnmarshalNonNull(original));
+ }
+
+ @Test
+ void defaultWriteObjectFromWriteObjectWritesUsingOurFormat() throws Exception {
+ WithOverrideStillUsingDefaultMechanism original = new WithOverrideStillUsingDefaultMechanism(42);
+ MarshalledObject marshalled = marshaller.marshal(original);
+
+ byte[] overrideBytes = readOverrideBytes(marshalled);
+ DataInputStream dis = new DataInputStream(new ByteArrayInputStream(overrideBytes));
+
+ assertThat(ProtocolMarshalling.readDescriptorOrCommandId(dis), is(BuiltinType.INT.descriptorId()));
+ assertThat(dis.readInt(), is(42));
+ assertThatDrained(dis);
+ }
+
+ @Test
+ void marshalsAndUnmarshalsSerializableWithReadWriteObjectUsingDefaultMechanism() throws Exception {
+ WithOverrideStillUsingDefaultMechanism result = marshalAndUnmarshalNonNull(new WithOverrideStillUsingDefaultMechanism(42));
+
+ assertThat(result.value, is(42));
+ }
+
+ @Test
+ void marshalsAndUnmarshalsNestedSerializablesWithReadWriteObjectUsingDefaultMechanism() throws Exception {
+ NestHostUsingDefaultMechanism result = marshalAndUnmarshalNonNull(
+ new NestHostUsingDefaultMechanism(42, new NestedUsingDefaultMechanism(100))
+ );
+
+ assertThat(result.value, is(42));
+ assertThat(result.nested.value, is(100));
+ }
+
+ @Test
+ void supportsWriteObjectAndReadObjectInHierarchy() throws Exception {
+ SubclassWithWriteReadOverride result = marshalAndUnmarshalNonNull(new SubclassWithWriteReadOverride(42));
+
+ assertThat(((SerializableWithWriteReadOverride) result).value, is(42 + WRITE_OBJECT_INCREMENT + READ_OBJECT_INCREMENT));
+ assertThat(result.childValue, is(42 + CHILD_WRITE_OBJECT_INCREMENT + CHILD_READ_OBJECT_INCREMENT));
+ }
+
+ /**
+ * An {@link Serializable} that does not have {@code writeReplace()}/{@code readResolve()} methods or other customizations.
+ */
+ private static class SimpleSerializable implements Serializable {
+ int intValue;
+
+ public SimpleSerializable(int intValue) {
+ this.intValue = intValue;
+ }
+ }
+
+ private static class SerializableWithWriteReplace extends SimpleSerializable {
+ public SerializableWithWriteReplace(int intValue) {
+ super(intValue);
+ }
+
+ private Object writeReplace() {
+ return new SerializableWithWriteReplace(intValue + 1_000_000);
+ }
+ }
+
+ private static class SerializableWithReadResolve extends SimpleSerializable {
+ public SerializableWithReadResolve(int intValue) {
+ super(intValue);
+ }
+
+ private Object readResolve() {
+ return new SerializableWithReadResolve(intValue + 1_000);
+ }
+ }
+
+ private static class SerializableWithWriteReplaceReadResolve extends SimpleSerializable {
+ public SerializableWithWriteReplaceReadResolve(int intValue) {
+ super(intValue);
+ }
+
+ private Object writeReplace() {
+ return new SerializableWithWriteReplaceReadResolve(intValue + 1_000_000);
+ }
+
+ private Object readResolve() {
+ return new SerializableWithWriteReplaceReadResolve(intValue + 1_000);
+ }
+ }
+
+ private static class SerializableWithReplaceWithSimple implements Serializable {
+ private final int intValue;
+
+ public SerializableWithReplaceWithSimple(int intValue) {
+ this.intValue = intValue;
+ }
+
+ private Object writeReplace() {
+ return new SimpleSerializable(intValue);
+ }
+ }
+
+ private static class SerializableWithReplaceWithNull extends SimpleSerializable {
+ public SerializableWithReplaceWithNull(int intValue) {
+ super(intValue);
+ }
+
+ private Object writeReplace() {
+ return null;
+ }
+ }
+
+ private static class SerializableWithResolveWithNull extends SimpleSerializable {
+ public SerializableWithResolveWithNull(int intValue) {
+ super(intValue);
+ }
+
+ private Object readResolve() {
+ return null;
+ }
+ }
+
+ private static class SerializableWithWriteReplaceChain1 extends SimpleSerializable {
+ public SerializableWithWriteReplaceChain1(int value) {
+ super(value);
+ }
+
+ private Object writeReplace() {
+ return new SerializableWithWriteReplaceChain2(intValue + 1);
+ }
+ }
+
+ private static class SerializableWithWriteReplaceChain2 extends SimpleSerializable {
+ public SerializableWithWriteReplaceChain2(int value) {
+ super(value);
+ }
+
+ private Object writeReplace() {
+ return intValue + 2;
+ }
+ }
+
+ private static class SerializableWithWriteReplaceWithSameClass extends SimpleSerializable {
+ public SerializableWithWriteReplaceWithSameClass(int value) {
+ super(value);
+ }
+
+ private Object writeReplace() {
+ return new SerializableWithWriteReplaceWithSameClass(intValue + 1);
+ }
+ }
+
+ private static class SerializableWithWriteReplaceCycle1 extends SimpleSerializable {
+ public SerializableWithWriteReplaceCycle1(int intValue) {
+ super(intValue);
+ }
+
+ private Object writeReplace() {
+ return new SerializableWithWriteReplaceCycle2(intValue);
+ }
+ }
+
+ private static class SerializableWithWriteReplaceCycle2 extends SimpleSerializable {
+ public SerializableWithWriteReplaceCycle2(int intValue) {
+ super(intValue);
+ }
+
+ private Object writeReplace() {
+ return new SerializableWithWriteReplaceCycle1(intValue);
+ }
+ }
+
+ private static class SerializableWithReadResolveChain1 extends SimpleSerializable {
+ public SerializableWithReadResolveChain1(int value) {
+ super(value);
+ }
+
+ private Object readResolve() {
+ return new SerializableWithReadResolveChain2(intValue + 1);
+ }
+ }
+
+ private static class SerializableWithReadResolveChain2 extends SimpleSerializable {
+ public SerializableWithReadResolveChain2(int value) {
+ super(value);
+ }
+
+ private Object readResolve() {
+ return intValue + 2;
+ }
+ }
+
+ private static class SerializableWithWriteReadOverride implements Serializable {
+ private int value;
+
+ public SerializableWithWriteReadOverride() {
+ }
+
+ public SerializableWithWriteReadOverride(int value) {
+ this.value = value;
+ }
+
+ private void writeObject(ObjectOutputStream oos) throws IOException {
+ oos.writeInt(value + WRITE_OBJECT_INCREMENT);
+ }
+
+ private void readObject(ObjectInputStream ois) throws IOException {
+ value = ois.readInt() + READ_OBJECT_INCREMENT;
+ }
+ }
+
+ private static class SerializableWithNoOpWriteReadOverride implements Serializable {
+ private int value;
+
+ public SerializableWithNoOpWriteReadOverride() {
+ }
+
+ public SerializableWithNoOpWriteReadOverride(int value) {
+ this.value = value;
+ }
+
+ private void writeObject(ObjectOutputStream oos) throws IOException {
+ // no-op
+ }
+
+ private void readObject(ObjectInputStream ois) throws IOException {
+ // no-op
+ }
+ }
+
+ private interface ContentsWriter {
+ void writeTo(ObjectOutputStream stream) throws IOException;
+ }
+
+ private interface ContentsReader<T> {
+ T readFrom(ObjectInputStream stream) throws IOException, ClassNotFoundException;
+ }
+
+ private interface DataReader<T> {
+ T readFrom(DataInputStream stream) throws IOException;
+ }
+
+ private interface InputReader<T> {
+ T readFrom(DataInput input) throws IOException;
+ }
+
+ private static class ReaderAndWriter<T> {
+ private final ContentsWriter writer;
+ private final ContentsReader<T> reader;
+
+ private ReaderAndWriter(ContentsWriter writer, ContentsReader<T> reader) {
+ this.writer = writer;
+ this.reader = reader;
+ }
+ }
+
+ private static class ReadWriteSpec<T> {
+ private final String name;
+ private final ContentsWriter writer;
+ private final ContentsReader<T> reader;
+ private final DataReader<T> dataReader;
+ private final Consumer<T> valueAsserter;
+
+ private ReadWriteSpec(String name, ContentsWriter writer, InputReader<T> inputReader, T expectedValue) {
+ this(name, writer, inputReader::readFrom, inputReader::readFrom, expectedValue);
+ }
+
+ private ReadWriteSpec(String name, ContentsWriter writer, ContentsReader<T> reader, DataReader<T> dataReader, T expectedValue) {
+ this(name, writer, reader, dataReader, actual -> assertThat(actual, is(expectedValue)));
+ }
+
+ private ReadWriteSpec(String name, ContentsWriter writer, ContentsReader<T> reader, DataReader<T> dataReader, Consumer<T> valueAsserter) {
+ this.name = name;
+ this.writer = writer;
+ this.reader = reader;
+ this.dataReader = dataReader;
+ this.valueAsserter = valueAsserter;
+ }
+
+ private T parseOverrideValue(byte[] overrideBytes) throws IOException {
+ DataInputStream dis = new DataInputStream(new ByteArrayInputStream(overrideBytes));
+ return dataReader.readFrom(dis);
+ }
+
+ private void assertUnmarshalledValue(T value) {
+ valueAsserter.accept(value);
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public String toString() {
+ return "ReadWriteSpec (" + name + ")";
+ }
+ }
+
+ private static class WithCustomizableOverride<T> implements Serializable {
+ private T value;
+
+ private void writeObject(ObjectOutputStream oos) throws IOException {
+ readerAndWriter.writer.writeTo(oos);
+ }
+
+ @SuppressWarnings("unchecked")
+ private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
+ value = (T) readerAndWriter.reader.readFrom(ois);
+ }
+ }
+
+ private static class SimpleNonSerializable {
+ private int value;
+
+ @SuppressWarnings("unused")
+ public SimpleNonSerializable() {
+ }
+
+ public SimpleNonSerializable(int value) {
+ this.value = value;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
+ SimpleNonSerializable that = (SimpleNonSerializable) o;
+ return value == that.value;
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(value);
+ }
+ }
+
+ private static class WithOverrideStillUsingDefaultMechanism implements Serializable {
+ private final int value;
+
+ private WithOverrideStillUsingDefaultMechanism(int value) {
+ this.value = value;
+ }
+
+ private void writeObject(ObjectOutputStream stream) throws IOException {
+ stream.defaultWriteObject();
+ }
+
+ private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
+ stream.defaultReadObject();
+ }
+ }
+
+ private static class NestedUsingDefaultMechanism implements Serializable {
+ private final int value;
+
+ private NestedUsingDefaultMechanism(int value) {
+ this.value = value;
+ }
+
+ private void writeObject(ObjectOutputStream stream) throws IOException {
+ stream.defaultWriteObject();
+ }
+
+ private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
+ stream.defaultReadObject();
+ }
+ }
+
+ private static class NestHostUsingDefaultMechanism implements Serializable {
+ private final int value;
+ private final NestedUsingDefaultMechanism nested;
+
+ private NestHostUsingDefaultMechanism(int value, NestedUsingDefaultMechanism nested) {
+ this.value = value;
+ this.nested = nested;
+ }
+
+ private void writeObject(ObjectOutputStream stream) throws IOException {
+ stream.defaultWriteObject();
+ }
+
+ private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
+ stream.defaultReadObject();
+ }
+ }
+
+ private static class SubclassWithWriteReadOverride extends SerializableWithWriteReadOverride {
+ private int childValue;
+
+ public SubclassWithWriteReadOverride() {
+ }
+
+ public SubclassWithWriteReadOverride(int value) {
+ super(value);
+ this.childValue = value;
+ }
+
+ private void writeObject(ObjectOutputStream oos) throws IOException {
+ oos.writeInt(childValue + CHILD_WRITE_OBJECT_INCREMENT);
+ }
+
+ private void readObject(ObjectInputStream ois) throws IOException {
+ childValue = ois.readInt() + CHILD_READ_OBJECT_INCREMENT;
+ }
+ }
+}
diff --git a/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/UnmarshalException.java b/modules/network/src/test/java/org/apache/ignite/internal/network/serialization/marshal/Throwables.java
similarity index 67%
copy from modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/UnmarshalException.java
copy to modules/network/src/test/java/org/apache/ignite/internal/network/serialization/marshal/Throwables.java
index 9e7b624..9b548bb 100644
--- a/modules/network/src/main/java/org/apache/ignite/internal/network/serialization/marshal/UnmarshalException.java
+++ b/modules/network/src/test/java/org/apache/ignite/internal/network/serialization/marshal/Throwables.java
@@ -17,13 +17,25 @@
package org.apache.ignite.internal.network.serialization.marshal;
-import org.apache.ignite.lang.IgniteInternalCheckedException;
+import java.util.ArrayList;
+import java.util.List;
/**
- * Thrown when unmarshalling fails.
+ * Throwable-related utilities.
*/
-public class UnmarshalException extends IgniteInternalCheckedException {
- public UnmarshalException(String message, Throwable cause) {
- super(message, cause);
+class Throwables {
+ static List<Throwable> causalChain(Throwable th) {
+ List<Throwable> lineage = new ArrayList<>();
+
+ Throwable current = th;
+ while (current != null) {
+ lineage.add(current);
+ current = current.getCause();
+ }
+
+ return List.copyOf(lineage);
+ }
+
+ private Throwables() {
}
}