Class FieldAnnotationAwareSerializer<T>


  • public class FieldAnnotationAwareSerializer<T>
    extends com.esotericsoftware.kryo.serializers.FieldSerializer<T>
    A kryo FieldSerializer that allows to exclusively include or exclude fields that are attributed with user-specific annotations. This can be for example useful when serializing beans that carry references to a dependency injection framework. As an example for Spring:

     
     Set<Class<? extends Annotation>> marks = new HashSet<>();
     marks.add(Autowired.class);
     SerializerFactory disregardingFactory = new FieldAnnotationAwareSerializer.Factory(marks, true);
     Kryo kryo = new Kryo();
     kryo.setDefaultSerializer(factory);
     
     

    The resulting Kryo instance would ignore all fields that are annotated with Spring's @Autowired annotation.

    Similarly, it is possible to created a serializer which does the opposite such that the resulting serializer would only serialize fields that are annotated with the specified annotations.

    Author:
    Rafael Winterhalter, Martin Grotzke
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  FieldAnnotationAwareSerializer.Factory
      A factory for creating instances of FieldAnnotationAwareSerializer.
      • Nested classes/interfaces inherited from class com.esotericsoftware.kryo.serializers.FieldSerializer

        com.esotericsoftware.kryo.serializers.FieldSerializer.Bind, com.esotericsoftware.kryo.serializers.FieldSerializer.CachedField, com.esotericsoftware.kryo.serializers.FieldSerializer.FieldSerializerConfig, com.esotericsoftware.kryo.serializers.FieldSerializer.NotNull, com.esotericsoftware.kryo.serializers.FieldSerializer.Optional
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean addAnnotation​(Class<? extends Annotation> clazz)
      Adds an annotation to the annotations that are considered by this serializer.
      boolean removeAnnotation​(Class<? extends Annotation> clazz)
      Removes an annotation to the annotations that are considered by this serializer.
      void updateFields()  
      • Methods inherited from class com.esotericsoftware.kryo.serializers.FieldSerializer

        copy, create, createCopy, getCopyFields, getField, getFields, getFieldSerializerConfig, getKryo, getType, initializeCachedFields, log, popTypeVariables, pushTypeVariables, read, removeField, removeField, write
      • Methods inherited from class com.esotericsoftware.kryo.Serializer

        getAcceptsNull, isImmutable, setAcceptsNull, setImmutable
    • Constructor Detail

      • FieldAnnotationAwareSerializer

        public FieldAnnotationAwareSerializer​(com.esotericsoftware.kryo.Kryo kryo,
                                              Class<?> type,
                                              Collection<Class<? extends Annotation>> marked,
                                              boolean disregarding)
        Creates a new field annotation aware serializer.
        Parameters:
        kryo - The Kryo instace.
        type - The type of the class being serialized.
        marked - The annotations this serializer considers for its serialization process. Be aware tha a serializer with disregarding set to false will never be able to serialize fields that are not annotated with any of these annotations since it is not possible to add fields to a FieldSerializer once it is created. See the documentation to addAnnotation(Class) and removeAnnotation(Class) for further information.
        disregarding - If true, the serializer will ignore all annotated fields, if set to false it will exclusively look at annotated fields.
    • Method Detail

      • updateFields

        public void updateFields()
        Overrides:
        updateFields in class com.esotericsoftware.kryo.serializers.FieldSerializer<T>
      • addAnnotation

        public boolean addAnnotation​(Class<? extends Annotation> clazz)
        Adds an annotation to the annotations that are considered by this serializer.

        Important: This will not have an effect if the serializer was configured to exclusively serialize annotated fields by setting disregarding to false. This is similar to the contract of this serializer's superclass FieldSerializer which does not allow to add fields that were formerly removed. If this was possible, instances that were serialized before this field was added could not longer be properly deserialized. In order to make this contract break explicit, you need to create a new instance of this serializer if you want to include new fields to a serializer that exclusively serializes annotated fields.

        Parameters:
        clazz - The annotation class to be added.
        Returns:
        true if the method call had an effect.
      • removeAnnotation

        public boolean removeAnnotation​(Class<? extends Annotation> clazz)
        Removes an annotation to the annotations that are considered by this serializer.

        Important: This will not have an effect if the serializer was configured to not serialize annotated fields by setting disregarding to true. This is similar to the contract of this serializer's superclass FieldSerializer which does not allow to add fields that were formerly removed. If this was possible, instances that were serialized before this field was added could not longer be properly deserialized. In order to make this contract break explicit, you need to create a new instance of this serializer if you want to include new fields to a serializer that ignores annotated fields for serialization.

        Parameters:
        clazz - The annotation class to be removed.
        Returns:
        true if the method call had an effect.