Class FieldSerializer<T, C extends FieldSerializerConfig>
- All Implemented Interfaces:
Comparator<FieldSerializer.CachedField>
- Direct Known Subclasses:
CompatibleFieldSerializer, TaggedFieldSerializer, VersionFieldSerializer
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic @interfaceUsed to annotate fields with a specific Kryo serializer.static classControls how a field will be serialized.static interfacestatic interfacestatic @interfaceIndicates a field should be ignored when its declaring class is registered unless thecontexthas a value set for the specified key. -
Field Summary
FieldsModifier and TypeFieldDescription(package private) Objectprivate FieldSerializerAnnotationsUtil(package private) static FieldSerializer.CachedFieldFactory(package private) final Classprotected final Cprivate FieldSerializer.CachedField[]private Class[]Concrete classes passed as values for type variablesprivate Genericsprivate FieldSerializerGenericsUtilprivate boolean(package private) final Kryo(package private) static FieldSerializer.CachedFieldFactoryprotected HashSet<FieldSerializer.CachedField> private FieldSerializer.CachedField[](package private) final Class(package private) final TypeVariable[]type variables declared for this typeprivate booleanIf set, this serializer tries to use a variable length encoding for int and long fields -
Constructor Summary
ConstructorsConstructorDescriptionFieldSerializer(Kryo kryo, Class type) FieldSerializer(Kryo kryo, Class type, Class[] generics) FieldSerializer(Kryo kryo, Class type, Class[] generics, C config) -
Method Summary
Modifier and TypeMethodDescriptionbuildValidFields(boolean transientFields, List<Field> allFields, ObjectMap context, IntArray useAsm) buildValidFieldsFromCachedFields(FieldSerializer.CachedField[] cachedFields, IntArray useAsm) intReturns a copy of the specified object.protected TUsed byread(Kryo, Input, Class)to create the new object.private voidcreateCachedFields(IntArray useAsm, List<Field> validFields, List<FieldSerializer.CachedField> cachedFields, int baseIndex) protected TcreateCopy(Kryo kryo, T original) Used bycopy(Kryo, Object)to create the new object.protected StringgetCachedFieldName(FieldSerializer.CachedField cachedField) booleanAllows specific fields to be optimized.Get all fields controlled by this FieldSerializerClass[]Get generic type parameters of the class controlled by this serializer.(package private) final GenericsgetKryo()booleanGet all transient fields controlled by this FieldSerializergetType()protected void(package private) FieldSerializer.CachedFieldnewCachedField(Field field, int fieldIndex, int accessIndex) (package private) FieldSerializer.CachedFieldnewMatchingCachedField(Field field, int accessIndex, Class fieldClass, Type fieldGenericType, Class[] fieldGenerics) Reads bytes and returns a new object of the specified concrete type.protected voidCalled when the list of cached fields must be rebuilt.protected voidrebuildCachedFields(boolean minorRebuild) Rebuilds the list of cached fields.voidremoveField(FieldSerializer.CachedField removeField) Removes a field so that it won't be serialized.voidremoveField(String fieldName) Removes a field so that it won't be serialized.voidsetCopyTransient(boolean setCopyTransient) voidsetFieldsAsAccessible(boolean setFieldsAsAccessible) Controls which fields are serialized.voidsetFieldsCanBeNull(boolean fieldsCanBeNull) Sets the default value forFieldSerializer.CachedField.setCanBeNull(boolean).voidsetFixedFieldTypes(boolean fixedFieldTypes) Sets the default value forFieldSerializer.CachedField.setClass(Class)to the field's declared type.voidsetGenerics(Kryo kryo, Class[] generics) Sets the generic types of the field or method this serializer will be used for on the next call to read or write.voidsetIgnoreSyntheticFields(boolean ignoreSyntheticFields) Controls if synthetic fields are serialized.voidsetOptimizedGenerics(boolean setOptimizedGenerics) Controls if the serialization of generics should be optimized for smaller size.voidsetSerializeTransient(boolean setSerializeTransient) voidThis method can be called for different fields having the same type.Methods inherited from class Serializer
getAcceptsNull, isImmutable, setAcceptsNull, setImmutableMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface Comparator
equals, reversed, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLong
-
Field Details
-
kryo
-
type
-
typeParameters
type variables declared for this type -
componentType
-
config
-
fields
-
transientFields
-
removedFields
-
access
Object access -
genericsUtil
-
annotationsUtil
-
generics
Concrete classes passed as values for type variables -
genericsScope
-
varIntsEnabled
private boolean varIntsEnabledIf set, this serializer tries to use a variable length encoding for int and long fields -
hasObjectFields
private boolean hasObjectFields -
asmFieldFactory
-
objectFieldFactory
-
-
Constructor Details
-
FieldSerializer
-
FieldSerializer
-
FieldSerializer
-
-
Method Details
-
rebuildCachedFields
protected void rebuildCachedFields()Called when the list of cached fields must be rebuilt. This is done any time settings are changed that affect which fields will be used. It is called from the constructor for FieldSerializer, but not for subclasses. Subclasses must call this from their constructor. -
rebuildCachedFields
protected void rebuildCachedFields(boolean minorRebuild) Rebuilds the list of cached fields.- Parameters:
minorRebuild- if set, processing due to changes in generic type parameters will be optimized
-
buildValidFieldsFromCachedFields
private List<Field> buildValidFieldsFromCachedFields(FieldSerializer.CachedField[] cachedFields, IntArray useAsm) -
buildValidFields
-
createCachedFields
private void createCachedFields(IntArray useAsm, List<Field> validFields, List<FieldSerializer.CachedField> cachedFields, int baseIndex) -
setGenerics
Description copied from class:SerializerSets the generic types of the field or method this serializer will be used for on the next call to read or write. Subsequent calls to read and write must not use this generic type information. The default implementation does nothing. Subclasses may use the information provided to this method for more efficient serialization, eg to use the same type for all items in a list.- Overrides:
setGenericsin classSerializer<T>- Parameters:
generics- Some (but never all) elements may be null if there is no generic type information at that index.
-
getGenerics
Get generic type parameters of the class controlled by this serializer.- Returns:
- generic type parameters or null, if there are none.
-
initializeCachedFields
protected void initializeCachedFields() -
newCachedField
-
newMatchingCachedField
FieldSerializer.CachedField newMatchingCachedField(Field field, int accessIndex, Class fieldClass, Type fieldGenericType, Class[] fieldGenerics) -
getAsmFieldFactory
-
getObjectFieldFactory
-
compare
- Specified by:
comparein interfaceComparator<T>
-
setFieldsCanBeNull
public void setFieldsCanBeNull(boolean fieldsCanBeNull) Sets the default value forFieldSerializer.CachedField.setCanBeNull(boolean). Calling this method resets thecached fields.- Parameters:
fieldsCanBeNull- False if none of the fields are null. Saves 0-1 byte per field. True if it is not known (default).
-
setFieldsAsAccessible
public void setFieldsAsAccessible(boolean setFieldsAsAccessible) Controls which fields are serialized. Calling this method resets thecached fields.- Parameters:
setFieldsAsAccessible- If true, all non-transient fields (inlcuding private fields) will be serialized andset as accessibleif necessary (default). If false, only fields in the public API will be serialized.
-
setIgnoreSyntheticFields
public void setIgnoreSyntheticFields(boolean ignoreSyntheticFields) Controls if synthetic fields are serialized. Default is true. Calling this method resets thecached fields.- Parameters:
ignoreSyntheticFields- If true, only non-synthetic fields will be serialized.
-
setFixedFieldTypes
public void setFixedFieldTypes(boolean fixedFieldTypes) Sets the default value forFieldSerializer.CachedField.setClass(Class)to the field's declared type. This allows FieldSerializer to be more efficient, since it knows field values will not be a subclass of their declared type. Default is false. Calling this method resets thecached fields. -
setCopyTransient
public void setCopyTransient(boolean setCopyTransient) -
setSerializeTransient
public void setSerializeTransient(boolean setSerializeTransient) -
setOptimizedGenerics
public void setOptimizedGenerics(boolean setOptimizedGenerics) Controls if the serialization of generics should be optimized for smaller size.Important: This setting changes the serialized representation, so that data can be deserialized only with if this setting is the same as it was for serialization.
- Parameters:
setOptimizedGenerics- If true, the serialization of generics will be optimize for smaller size (default: false)
-
write
This method can be called for different fields having the same type. Even though the raw type is the same, if the type is generic, it could happen that different concrete classes are used to instantiate it. Therefore, in case of different instantiation parameters, the fields analysis should be repeated. TODO: Cache serializer instances generated for a given set of generic parameters. Reuse it later instead of recomputing every time.- Specified by:
writein classSerializer<T>- Parameters:
object- May be null ifSerializer.getAcceptsNull()is true.
-
read
Description copied from class:SerializerReads bytes and returns a new object of the specified concrete type.Before Kryo can be used to read child objects,
Kryo.reference(Object)must be called with the parent object to ensure it can be referenced by the child objects. Any serializer that usesKryoto read a child object may need to be reentrant.This method should not be called directly, instead this serializer can be passed to
Kryoread methods that accept a serialier.- Specified by:
readin classSerializer<T>- Returns:
- May be null if
Serializer.getAcceptsNull()is true.
-
create
Used byread(Kryo, Input, Class)to create the new object. This can be overridden to customize object creation, eg to call a constructor with arguments. The default implementation usesKryo.newInstance(Class). -
getField
Allows specific fields to be optimized. -
getCachedFieldName
-
removeField
Removes a field so that it won't be serialized. -
removeField
Removes a field so that it won't be serialized. -
getFields
Get all fields controlled by this FieldSerializer- Returns:
- all fields controlled by this FieldSerializer
-
getTransientFields
Get all transient fields controlled by this FieldSerializer- Returns:
- all transient fields controlled by this FieldSerializer
-
getType
-
getKryo
-
getCopyTransient
public boolean getCopyTransient() -
getSerializeTransient
public boolean getSerializeTransient() -
createCopy
Used bycopy(Kryo, Object)to create the new object. This can be overridden to customize object creation, eg to call a constructor with arguments. The default implementation usesKryo.newInstance(Class). -
copy
Description copied from class:SerializerReturns a copy of the specified object. The default implementation returns the original ifSerializer.isImmutable()is true, else throwsKryoException. Subclasses should override this method if needed to supportKryo.copy(Object).Before Kryo can be used to copy child objects,
Kryo.reference(Object)must be called with the copy to ensure it can be referenced by the child objects. Any serializer that usesKryoto copy a child object may need to be reentrant.This method should not be called directly, instead this serializer can be passed to
Kryocopy methods that accept a serialier.- Overrides:
copyin classSerializer<T>
-
getGenericsScope
-
getConfig
-