Class Serializer<T>
- All Implemented Interfaces:
ClassResolver
- Direct Known Subclasses:
ConfigurationSerializer
,EnumSerializer
,YggdrasilSerializer
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected abstract boolean
Returns whether the class should be instantiated using its nullary constructor or not.boolean
canBeInstantiated(Class<? extends T> c)
Whether an instance of the given class can be dynamically created.protected T
deserialize(Fields fields)
Used to deserialise Bukkit objects and other stuff that cannot be instantiated, e.g.<E extends T>
Edeserialize(Class<E> c, Fields fields)
Deserialises an object.@Nullable T
deserialize(String s)
Deprecated.abstract void
deserialize(T o, Fields f)
Deserialises an object.Resolves a class by its ID.@Nullable String
Gets an ID for a Class.abstract boolean
Not currently used (everything happens on Bukkit's main thread).<E extends T>
@Nullable EnewInstance(Class<E> c)
Creates a new instance of the given class.abstract Fields
Serialises the given object.
-
Field Details
-
info
-
-
Constructor Details
-
Serializer
public Serializer()
-
-
Method Details
-
getClass
Description copied from interface:ClassResolver
Resolves a class by its ID.- Specified by:
getClass
in interfaceClassResolver
- Specified by:
getClass
in classYggdrasilSerializer<T>
- Parameters:
id
- The ID used when storing objects- Returns:
- The Class object that represents data with the given ID, or null if the ID does not belong to the implementor
-
getID
Description copied from interface:ClassResolver
Gets an ID for a Class. The ID is used to identify the type of a saved object.// TODO make sure that it's unique
- Parameters:
c
- The class to get the ID of- Returns:
- The ID of the given class, or null if this is not a class of the implementor
-
newInstance
Description copied from class:YggdrasilSerializer
Creates a new instance of the given class.- Specified by:
newInstance
in 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.
-
serialize
Serialises the given object.Use return new
Fields
(this); to emulate the default behaviour.This method must be thread-safe. Use
Task.callSync(Callable)
if you need to serialise on Bukkit's main thread.- Specified by:
serialize
in classYggdrasilSerializer<T>
- Parameters:
o
- The object to serialise- Returns:
- A Fields object representing the object's fields to serialise. Must not be null.
- Throws:
NotSerializableException
- If this object could not be serialised
-
deserialize
public abstract void deserialize(T o, Fields f) throws StreamCorruptedException, NotSerializableExceptionDescription copied from class:YggdrasilSerializer
Deserialises an object.Use fields.
setFields
(o); to emulate the default behaviour.- Specified by:
deserialize
in classYggdrasilSerializer<T>
- Parameters:
o
- The object to deserialise as returned byYggdrasilSerializer.newInstance(Class)
.f
- The fields read from stream- Throws:
StreamCorruptedException
- If deserialisation failed because the data read from stream is incomplete or invalid.NotSerializableException
-
mustSyncDeserialization
public abstract boolean mustSyncDeserialization()Not currently used (everything happens on Bukkit's main thread).- Returns:
- Whether deserialisation must be done on Bukkit's main thread.
-
canBeInstantiated
Description copied from class:YggdrasilSerializer
Whether 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 deserialising 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. // TODO clarify
- Overrides:
canBeInstantiated
in classYggdrasilSerializer<T>
- Parameters:
c
- The class to check- Returns:
- true by default
-
canBeInstantiated
protected abstract boolean canBeInstantiated()Returns whether the class should be instantiated using its nullary constructor or not. Return false if the class has no nullary constructor or if you do not have control over the source of the class (e.g. if it's from an API).You must override and use
deserialize(Fields)
if this method returns false (deserialize(Object, Fields)
will no be used anymore in this case). -
deserialize
public <E extends T> E deserialize(Class<E> c, Fields fields) throws StreamCorruptedException, NotSerializableExceptionDescription copied from class:YggdrasilSerializer
Deserialises an object.- Overrides:
deserialize
in classYggdrasilSerializer<T>
- Parameters:
c
- 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 deserialisation failed because the data read from stream is incomplete or invalid.NotSerializableException
- If the class is not serialisable
-
deserialize
Used to deserialise Bukkit objects and other stuff that cannot be instantiated, e.g. a plugin may and should not create a new instance ofWorld
, but useBukkit.getWorld(String)
to get an existing world object.- Parameters:
fields
- The Fields object that holds the information about the serialised object- Returns:
- The deserialised object. Must not be null (throw an exception instead).
- Throws:
StreamCorruptedException
- If the given data is invalid or incompleteNotSerializableException
-
deserialize
Deprecated.Deserialises an object from a string returned by this serializer or an earlier version thereof.This method should only return null if the input is invalid (i.e. not produced by
serialize(Object)
or an older version of that method)This method must only be called from Bukkit's main thread if
mustSyncDeserialization()
returned true.- Parameters:
s
-- Returns:
- The deserialised object or null if the input is invalid. An error message may be logged to specify the cause.
-