Package ch.njol.yggdrasil
Class SimpleClassSerializer.NonInstantiableClassSerializer<T>
java.lang.Object
ch.njol.yggdrasil.YggdrasilSerializer<T>
ch.njol.yggdrasil.SimpleClassSerializer<T>
ch.njol.yggdrasil.SimpleClassSerializer.NonInstantiableClassSerializer<T>
- Type Parameters:
T- the type of the class to serialize
- All Implemented Interfaces:
ClassResolver
- Enclosing class:
SimpleClassSerializer<T>
public abstract static class SimpleClassSerializer.NonInstantiableClassSerializer<T>
extends SimpleClassSerializer<T>
A simple serializer for classes that cannot be instantiated (e.g. abstract classes or interfaces).
The same as
SimpleClassSerializer, but overrides instantiation methods to prevent instantiation.
Only deserialization via deserialize(Fields) is supported. deserialize(Class, Fields) will
call that method internally. deserialize(Object, Fields) will throw an exception.
newInstance(Class) will always return null, and canBeInstantiated(Class) will always
return false.-
Nested Class Summary
Nested classes/interfaces inherited from class ch.njol.yggdrasil.SimpleClassSerializer
SimpleClassSerializer.NonInstantiableClassSerializer<T> -
Field Summary
Fields inherited from class ch.njol.yggdrasil.SimpleClassSerializer
id, type -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal booleancanBeInstantiated(Class<? extends T> type) Whether an instance of the given class can be dynamically created.protected abstract Tdeserialize(Fields fields) Used to deserialize objects that cannot be instantiated.<E extends T>
Edeserialize(Class<E> type, Fields fields) Deserializes an object.final voiddeserialize(T object, Fields fields) Deserializes an object.final <E extends T>
EnewInstance(Class<E> c) Creates a new instance of the given class.Methods inherited from class ch.njol.yggdrasil.SimpleClassSerializer
getClass, getIDMethods inherited from class ch.njol.yggdrasil.YggdrasilSerializer
serialize
-
Constructor Details
-
NonInstantiableClassSerializer
-
-
Method Details
-
canBeInstantiated
Description copied from class:YggdrasilSerializerWhether an instance of the given class can be dynamically created. If this method returns false,YggdrasilSerializer.newInstance(Class)andYggdrasilSerializer.deserialize(Object, Fields)will not be called for the given class, butYggdrasilSerializer.deserialize(Class, Fields)will be used instead, and having any reference to an object of the given class in its own fields' graph will cause Yggdrasil to throw an exception upon serialisation as no reference to the object will be available when deserializing the object. // TODO allow thisPlease note that you must not change the return value of this function ever - it is not saved in the stream.
- Overrides:
canBeInstantiatedin classYggdrasilSerializer<T>- Parameters:
type- The class to check- Returns:
- true by default
-
newInstance
Description copied from class:YggdrasilSerializerCreates a new instance of the given class.- Specified by:
newInstancein classYggdrasilSerializer<T>- Parameters:
c- The class as read from stream- Returns:
- A new instance of the given class. Must not be null if
YggdrasilSerializer.canBeInstantiated(Class)returned true.
-
deserialize
public final void deserialize(T object, Fields fields) throws StreamCorruptedException, NotSerializableException Description copied from class:YggdrasilSerializerDeserializes an object.Use fields.
setFields(o); to emulate the default behaviour.- Specified by:
deserializein classYggdrasilSerializer<T>- Parameters:
object- The object to deserialize as returned byYggdrasilSerializer.newInstance(Class).fields- The fields read from stream- Throws:
StreamCorruptedException- If deserialization failed because the data read from stream is incomplete or invalid.NotSerializableException
-
deserialize
public <E extends T> E deserialize(Class<E> type, Fields fields) throws StreamCorruptedException, NotSerializableException Description copied from class:YggdrasilSerializerDeserializes an object.- Overrides:
deserializein classYggdrasilSerializer<T>- Parameters:
type- The class to get an instance offields- The fields read from stream- Returns:
- An object representing the read fields. Must not be null (throw an exception instead).
- Throws:
StreamCorruptedException- If deserialization failed because the data read from stream is incomplete or invalid.NotSerializableException- If the class is not serializable
-
deserialize
protected abstract T deserialize(Fields fields) throws StreamCorruptedException, NotSerializableException Used to deserialize objects that cannot be instantiated.- Parameters:
fields- The Fields object that holds the information about the serialised object- Returns:
- The deserialized object. Must not be null (throw an exception instead).
- Throws:
StreamCorruptedExceptionNotSerializableException
-