public class FieldAnnotationAwareSerializer<T>
extends com.esotericsoftware.kryo.serializers.FieldSerializer<T>
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.| Modifier and Type | Class and Description |
|---|---|
static class |
FieldAnnotationAwareSerializer.Factory
A factory for creating instances of
FieldAnnotationAwareSerializer. |
com.esotericsoftware.kryo.serializers.FieldSerializer.Bind, com.esotericsoftware.kryo.serializers.FieldSerializer.CachedField<X>, com.esotericsoftware.kryo.serializers.FieldSerializer.CachedFieldFactory, com.esotericsoftware.kryo.serializers.FieldSerializer.Optional| Constructor and Description |
|---|
FieldAnnotationAwareSerializer(com.esotericsoftware.kryo.Kryo kryo,
Class<?> type,
Collection<Class<? extends Annotation>> marked,
boolean disregarding)
Creates a new field annotation aware serializer.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
addAnnotation(Class<? extends Annotation> clazz)
Adds an annotation to the annotations that are considered by this serializer.
|
protected void |
rebuildCachedFields() |
boolean |
removeAnnotation(Class<? extends Annotation> clazz)
Removes an annotation to the annotations that are considered by this serializer.
|
compare, copy, create, createCopy, getCopyTransient, getField, getFields, getGenerics, getGenericsScope, getKryo, getType, getUseAsmEnabled, getUseMemRegions, initializeCachedFields, read, rebuildCachedFields, removeField, removeField, setCopyTransient, setFieldsAsAccessible, setFieldsCanBeNull, setFixedFieldTypes, setGenerics, setIgnoreSyntheticFields, setUseAsm, writegetAcceptsNull, isImmutable, setAcceptsNull, setImmutableclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitcomparing, comparing, comparingDouble, comparingInt, comparingLong, equals, naturalOrder, nullsFirst, nullsLast, reversed, reverseOrder, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLongpublic FieldAnnotationAwareSerializer(com.esotericsoftware.kryo.Kryo kryo,
Class<?> type,
Collection<Class<? extends Annotation>> marked,
boolean disregarding)
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.protected void rebuildCachedFields()
rebuildCachedFields in class com.esotericsoftware.kryo.serializers.FieldSerializer<T>public boolean addAnnotation(Class<? extends Annotation> clazz)
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.clazz - The annotation class to be added.true if the method call had an effect.public boolean removeAnnotation(Class<? extends Annotation> clazz)
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.clazz - The annotation class to be removed.true if the method call had an effect.Copyright © 2010–2015. All rights reserved.