Class Serialization
java.lang.Object
org.drasyl.node.handler.serialization.Serialization
Holds a
Map
with all available Serializer
s and the classes each serializer should
be used for. This class is used by MessageSerializer
to (de)serialize message objects
when communicating with remote nodes.
Each serializer is applied for objects of the assigned class, all subclasses, and all implementations.
Each received message includes the class name of the content. Normally, we would have to
invoke the method Class.forName(String)
to load the associated class to find a serializer
for the class, any subclass, or implementation. However, this method invocation poses a security
risk because we cannot control the class name received from the remote node. For this reason,
when adding a Serializer
, the classpath is scanned in advance for subclasses and
implementations. These results will then later used when a message should be deserialized. This
approach allows us to load only classes with valid Serializer
s.
- See Also:
-
Field Summary
-
Constructor Summary
ConstructorDescriptionSerialization
(Map<String, Serializer> serializers, Map<Class<?>, String> bindings) -
Method Summary
Modifier and TypeMethodDescriptionvoid
addSerializer
(Class<?> clazz, Serializer serializer) Adds aserializer
as serializer for objects of typeclazz
.static void
Rebuilds the inheritance graph, but keeps the old values.findSerializerFor
(String clazzName) Returns the configuredSerializer
for the givenclazzName
.static void
noop()
This method does nothing.void
removeSerializer
(Class<?> clazz) Removes all serializers for objects of typeclazz
.void
removeSerializer
(Serializer serializer) Removes given serializer from
-
Field Details
-
NULL_SERIALIZER
-
-
Constructor Details
-
Serialization
-
-
Method Details
-
buildInheritanceGraph
public static void buildInheritanceGraph()Rebuilds the inheritance graph, but keeps the old values. -
findSerializerFor
Returns the configuredSerializer
for the givenclazzName
. The configuredSerializer
is used if the configured class `isAssignableFrom` from theclazz
, i.e. the configured class is a super class or implemented interface. In case of ambiguity it is primarily using the most specific configured class, and secondly the entry configured first.- Parameters:
clazzName
- name of class for which a serializer should be searched for- Returns:
- serializer for given clazz or
null
if nothing found
-
addSerializer
Adds aserializer
as serializer for objects of typeclazz
.- Parameters:
clazz
- class the serializer should be used forserializer
- the serializer
-
removeSerializer
Removes all serializers for objects of typeclazz
.- Parameters:
clazz
- class the serializer should be removed for
-
removeSerializer
Removes given serializer from- Parameters:
serializer
- the serializer that should be removed
-
noop
public static void noop()This method does nothing. However, it triggers the loading of the inheritance graph and thus allows the caller to eagerly load the inheritance graph.
-