Class ReflectionBasedAutoSerializer
- All Implemented Interfaces:
Declarable,PdxSerializer
PdxSerialzer to perform automatic serialization of domain objects. The implication is that the
domain classes do not need to implement the PdxSerializable interface.
This implementation will serialize all relevant fields
For example:
Cache c = new CacheFactory().set("cache-xml-file", cacheXmlFileName)
.setPdxSerializer(new ReflectionBasedAutoSerializer("com.foo.DomainObject")).create();
In this example DomainObject would not need to implement
PdxSerializable to be serialized.
The equivalent cache.xml entries might be as follows:
<pdx>
<pdx-serializer>
<class-name>
org.apache.geode.pdx.ReflectionBasedAutoSerializer
</class-name>
<parameter name="classes">
<string> com.company.domain.DomainObject </string>
</parameter>
</pdx-serializer>
</pdx>
See reconfigure for additional
details on the format of the parameter string.- Since:
- GemFire 6.6
-
Constructor Summary
ConstructorsConstructorDescriptionDefault constructor primarily used during declarative configuration via the cache.xml file.ReflectionBasedAutoSerializer(boolean checkPortability, String... patterns) Constructor which takes a list of class name patterns which are to be auto-serialized.ReflectionBasedAutoSerializer(String... patterns) Constructor which takes a list of class name patterns which are to be auto-serialized.ReflectionBasedAutoSerializer(List<String> classes) Deprecated.as of 6.6.2 use ReflectionBasedAutoSerializer(String...) instead. -
Method Summary
Modifier and TypeMethodDescriptionMethod implemented fromPdxSerializerwhich performs object de-serialization.Return aPropertiesobject with a representation of the current config.getFieldName(Field f, Class<?> clazz) Controls the field name that will be used in pdx for a field being auto serialized.getFieldType(Field f, Class<?> clazz) Controls what pdx field type will be used when auto serializing.For internal use only.voidinit(Properties props) Deprecated.as of Geode 1.5 use initialize insteadvoidinitialize(Cache cache, Properties props) Used for declarative class initialization from cache.xml.booleanisClassAutoSerialized(Class<?> clazz) Controls what classes will be auto serialized by this serializer.booleanisFieldIncluded(Field f, Class<?> clazz) Controls what fields of a class will be auto serialized by this serializer.booleanisIdentityField(Field f, Class<?> clazz) Controls what fields of a class that is auto serialized will be marked as pdx identity fields.readTransform(Field f, Class<?> clazz, Object serializedValue) Controls what field value is read during auto deserialization.voidreconfigure(boolean checkPortability, String... patterns) Method to reconfigure this serializer.voidreconfigure(String... patterns) Method to reconfigure this serializer.voidsetSerializableClasses(List<String> patterns) Deprecated.as of 6.6.2 usereconfigure(String...)instead.booleanMethod implemented fromPdxSerializerwhich performs object serialization.booleantransformFieldValue(Field f, Class<?> clazz) Controls if a pdx field's value can be transformed during serialization.writeTransform(Field f, Class<?> clazz, Object originalValue) Controls what field value is written during auto serialization.
-
Constructor Details
-
ReflectionBasedAutoSerializer
public ReflectionBasedAutoSerializer()Default constructor primarily used during declarative configuration via the cache.xml file. Instances created with this constructor will not match any classes so useReflectionBasedAutoSerializer(String...)instead. -
ReflectionBasedAutoSerializer
Deprecated.as of 6.6.2 use ReflectionBasedAutoSerializer(String...) instead.Constructor which takes a list of class name patterns which are to be auto-serialized. Portability of serialization will not be checked.Each string in the list represents a definition in the following form:
<class pattern>#identity=<identity field pattern>#exclude=<exclude field pattern>
The hash (#) characters are separators and are not part of the parameter name. An example would be:com.company.DomainObject.*#identity=id.*#exclude=creationDate
This would select all classes with a class name beginning withcom.company.DomainObjectand would select as PDX identity fields any fields beginning withidand would not serialize the field calledcreationDate.There is no association between the the identity and exclude options, so the above example could also be expressed as:
com.company.DomainObject.*#identity=id.* com.company.DomainObject.*#exclude=creationDate
Note that all defined patterns are used when determining whether a field should be considered as an identity field or should be excluded. Thus the order of the patterns is not relevant.- Parameters:
classes- the patterns which are matched against domain class names to determine whether they should be serialized
-
ReflectionBasedAutoSerializer
Constructor which takes a list of class name patterns which are to be auto-serialized. Portability of serialization will not be checked.Each string in the list represents a definition in the following form:
<class pattern>#identity=<identity field pattern>#exclude=<exclude field pattern>
The hash (#) characters are separators and are not part of the parameter name. An example would be:com.company.DomainObject.*#identity=id.*#exclude=creationDate
This would select all classes with a class name beginning withcom.company.DomainObjectand would select as PDX identity fields any fields beginning withidand would not serialize the field calledcreationDate.There is no association between the the identity and exclude options, so the above example could also be expressed as:
com.company.DomainObject.*#identity=id.* com.company.DomainObject.*#exclude=creationDate
Note that all defined patterns are used when determining whether a field should be considered as an identity field or should be excluded. Thus the order of the patterns is not relevant.- Parameters:
patterns- the patterns which are matched against domain class names to determine whether they should be serialized- Since:
- GemFire 6.6.2
-
ReflectionBasedAutoSerializer
Constructor which takes a list of class name patterns which are to be auto-serialized.Each string in the list represents a definition in the following form:
<class pattern>#identity=<identity field pattern>#exclude=<exclude field pattern>
The hash (#) characters are separators and are not part of the parameter name. An example would be:com.company.DomainObject.*#identity=id.*#exclude=creationDate
This would select all classes with a class name beginning withcom.company.DomainObjectand would select as PDX identity fields any fields beginning withidand would not serialize the field calledcreationDate.There is no association between the the identity and exclude options, so the above example could also be expressed as:
com.company.DomainObject.*#identity=id.* com.company.DomainObject.*#exclude=creationDate
Note that all defined patterns are used when determining whether a field should be considered as an identity field or should be excluded. Thus the order of the patterns is not relevant.- Parameters:
checkPortability- iftruethen an serialization done by this serializer will throw an exception if the object it not portable to non-java languages.patterns- the patterns which are matched against domain class names to determine whether they should be serialized- Since:
- GemFire 6.6.2
-
-
Method Details
-
setSerializableClasses
Deprecated.as of 6.6.2 usereconfigure(String...)instead.Method to configure classes to consider for serialization, to set any identity fields and to define any fields to exclude from serialization.Each string in the list represents a definition in the following form:
<class pattern>#identity=<identity field pattern>#exclude=<exclude field pattern>
The hash (#) characters are separators and are not part of the parameter name. An example would be:com.company.DomainObject.*#identity=id.*#exclude=creationDate
This would select all classes with a class name beginning withcom.company.DomainObjectand would select as PDX identity fields any fields beginning withidand would not serialize the field calledcreationDate.There is no association between the the identity and exclude options, so the above example could also be expressed as:
com.company.DomainObject.*#identity=id.* com.company.DomainObject.*#exclude=creationDate
Note that all defined patterns are used when determining whether a field should be considered as an identity field or should be excluded. Thus the order of the patterns is not relevant.- Parameters:
patterns- the list of definitions to apply
-
reconfigure
Method to reconfigure this serializer. Any previous configuration is cleared. The serializer will not check for portable serialization.Each string in the list represents a definition in the following form:
<class pattern>#identity=<identity field pattern>#exclude=<exclude field pattern>
The hash (#) characters are separators and are not part of the parameter name. An example would be:com.company.DomainObject.*#identity=id.*#exclude=creationDate
This would select all classes with a class name beginning withcom.company.DomainObjectand would select as PDX identity fields any fields beginning withidand would not serialize the field calledcreationDate.There is no association between the the identity and exclude options, so the above example could also be expressed as:
com.company.DomainObject.*#identity=id.* com.company.DomainObject.*#exclude=creationDate
Note that all defined patterns are used when determining whether a field should be considered as an identity field or should be excluded. Thus the order of the patterns is not relevant.- Parameters:
patterns- the definitions to apply- Since:
- GemFire 6.6.2
-
reconfigure
Method to reconfigure this serializer. Any previous configuration is cleared.Each string in the list represents a definition in the following form:
<class pattern>#identity=<identity field pattern>#exclude=<exclude field pattern>
The hash (#) characters are separators and are not part of the parameter name. An example would be:com.company.DomainObject.*#identity=id.*#exclude=creationDate
This would select all classes with a class name beginning withcom.company.DomainObjectand would select as PDX identity fields any fields beginning withidand would not serialize the field calledcreationDate.There is no association between the the identity and exclude options, so the above example could also be expressed as:
com.company.DomainObject.*#identity=id.* com.company.DomainObject.*#exclude=creationDate
Note that all defined patterns are used when determining whether a field should be considered as an identity field or should be excluded. Thus the order of the patterns is not relevant.- Parameters:
patterns- the definitions to applycheckPortability- iftruethen an serialization done by this serializer will throw an exception if the object it not portable to non-java languages.- Since:
- GemFire 6.6.2
-
toData
Method implemented fromPdxSerializerwhich performs object serialization.- Specified by:
toDatain interfacePdxSerializer- Parameters:
obj- the object to serializewriter- thePdxWriterto use when serializing this object- Returns:
trueif the object was serialized,falseotherwise
-
fromData
Method implemented fromPdxSerializerwhich performs object de-serialization.- Specified by:
fromDatain interfacePdxSerializer- Parameters:
clazz- the class of the object to re-createreader- thePdxReaderto use when creating this object- Returns:
- the deserialized object if this serializer handles the given class, null otherwise.
-
init
Deprecated.as of Geode 1.5 use initialize insteadUsed for declarative class initialization from cache.xml. The following property may be specified:- classes - a comma-delimited list of strings which represent the patterns used to
select classes for serialization, patterns to select identity fields and patterns to exclude
fields. See
reconfigurefor specifics. - check-portability - if true then an exception will be thrown if an attempt to serialize data that is not portable to .NET is made.
- Specified by:
initin interfaceDeclarable- Parameters:
props- properties used to configure the auto serializer
- classes - a comma-delimited list of strings which represent the patterns used to
select classes for serialization, patterns to select identity fields and patterns to exclude
fields. See
-
initialize
Used for declarative class initialization from cache.xml. The following property may be specified:- classes - a comma-delimited list of strings which represent the patterns used to
select classes for serialization, patterns to select identity fields and patterns to exclude
fields. See
reconfigurefor specifics. - check-portability - if true then an exception will be thrown if an attempt to serialize data that is not portable to .NET is made.
- Specified by:
initializein interfaceDeclarable- Parameters:
cache- the cache that owns this serializerprops- properties used to configure the auto serializer- Since:
- Geode 1.5
- classes - a comma-delimited list of strings which represent the patterns used to
select classes for serialization, patterns to select identity fields and patterns to exclude
fields. See
-
getConfig
Return aPropertiesobject with a representation of the current config. Depending on how thisReflectionBasedAutoSerializerwas configured, the returned property value will have the correct semantics but may differ from the the original configuration string.- Returns:
- a
Propertiesobject
-
isClassAutoSerialized
Controls what classes will be auto serialized by this serializer. Override this method to customize what classes will be auto serialized.The default implementation:
- only serializes classes whose name matches one of the patterns
- excludes classes whose package begins with "org.apache.", "java.", or "javax." unless the system property "gemfire.auto.serialization.no.hardcoded.excludes" is set to "true".
- excludes classes that do not have a public no-arg constructor
- excludes enum classes
- excludes classes that require standard java serialization. A class requires standard java serialization if it extends Externalizable or if it extends Serializable and has either a private writeObject method or a writeReplace method as defined by the java serialization specification.
This method is only called the first time it sees a new class. The result will be remembered and used the next time the same class is seen.
- Parameters:
clazz- the class that is being considered for auto serialization.- Returns:
- true if instances of the class should be auto serialized; false if not.
- Since:
- GemFire 6.6.2
-
isFieldIncluded
Controls what fields of a class will be auto serialized by this serializer. Override this method to customize what fields of a class will be auto serialized.The default implementation:
- excludes transient fields
- excludes static fields
- excludes any fields that match an "#exclude=" pattern.
This method is only called the first time it sees a new class. The result will be remembered and used the next time the same class is seen.
- Parameters:
f- the field being considered for serializationclazz- the original class being serialized that owns this field. Note that this field may have been inherited from a super class by this class. If you want to find the class that declared this field useField.getDeclaringClass().- Returns:
- true if the field should be serialized as a pdx field; false if it should be ignored.
- Since:
- GemFire 6.6.2
-
getFieldName
Controls the field name that will be used in pdx for a field being auto serialized. Override this method to customize the field names that will be generated by auto serialization. It allows you to convert a local, language dependent name, to a more portable name. The returned name is the one that will show up in aPdxInstanceand that one that will need to be used to access the field when doing a query.The default implementation returns the name obtained from
f.This method is only called the first time it sees a new class. The result will be remembered and used the next time the same class is seen.
- Parameters:
f- the field whose name is returned.clazz- the original class being serialized that owns this field. Note that this field may have been inherited from a super class by this class. If you want to find the class that declared this field useField.getDeclaringClass().- Returns:
- the name of the field
- Since:
- GemFire 6.6.2
-
isIdentityField
Controls what fields of a class that is auto serialized will be marked as pdx identity fields. Override this method to customize what fields of an auto serialized class will be identity fields. Identity fields are used when aPdxInstancecomputes its hash code and checks to see if it is equal to another object.The default implementation only marks fields that match an "#identity=" pattern as identity fields.
This method is only called the first time it sees a new class. The result will be remembered and used the next time the same class is seen.
- Parameters:
f- the field to test to see if it is an identity field.clazz- the original class being serialized that owns this field. Note that this field may have been inherited from a super class by this class. If you want to find the class that declared this field useField.getDeclaringClass().- Returns:
- true if the field should be marked as an identity field; false if not.
- Since:
- GemFire 6.6.2
-
getFieldType
Controls what pdx field type will be used when auto serializing. Override this method to customize what pdx field type will be used for a given domain class field.The default implementation uses
FieldType.get(Class)by passing itField.getType().This method is only called the first time it sees a new class. The result will be remembered and used the next time the same class is seen.
- Parameters:
f- the field whose pdx field type needs to be determinedclazz- the original class being serialized that owns this field. Note that this field may have been inherited from a super class by this class. If you want to find the class that declared this field useField.getDeclaringClass().- Returns:
- the pdx field type of the given domain class field.
- Since:
- GemFire 6.6.2
-
transformFieldValue
Controls if a pdx field's value can be transformed during serialization. Override this method to customize what fields can have their values transformed. If you return true then you need to also overridewriteTransform(java.lang.reflect.Field, java.lang.Class<?>, java.lang.Object)andreadTransform(java.lang.reflect.Field, java.lang.Class<?>, java.lang.Object).The default implementation returns false.
This method is only called the first time it sees a new class. The result will be remembered and used the next time the same class is seen.
- Parameters:
f- the field in questionclazz- the original class being serialized that owns this field. Note that this field may have been inherited from a super class by this class. If you want to find the class that declared this field useField.getDeclaringClass().- Returns:
- true if the
writeTransform(java.lang.reflect.Field, java.lang.Class<?>, java.lang.Object)andreadTransform(java.lang.reflect.Field, java.lang.Class<?>, java.lang.Object)need to be called when serializing and deserializing this field's value. - Since:
- GemFire 6.6.2
-
writeTransform
Controls what field value is written during auto serialization. Override this method to customize the data that will be written during auto serialization. This method will only be called iftransformFieldValue(java.lang.reflect.Field, java.lang.Class<?>)returned true.- Parameters:
f- the field in questionclazz- the original class being serialized that owns this field. Note that this field may have been inherited from a super class by this class. If you want to find the class that declared this field useField.getDeclaringClass().originalValue- the value of the field that was read from the domain object.- Returns:
- the actual value to write for this field. Return
originalValueif you decide not to transform the value. - Since:
- GemFire 6.6.2
-
readTransform
Controls what field value is read during auto deserialization. Override this method to customize the data that will be read during auto deserialization. This method will only be called iftransformFieldValue(java.lang.reflect.Field, java.lang.Class<?>)returned true.- Parameters:
f- the field in questionclazz- the original class being serialized that owns this field. Note that this field may have been inherited from a super class by this class. If you want to find the class that declared this field useField.getDeclaringClass().serializedValue- the value of the field that was serialized for this field.- Returns:
- the actual value to write for this field. Return
serializedValueif you decide not to transform the value. - Since:
- GemFire 6.6.2
-
getRegionService
- Returns:
- the cache that this serializer is installed on. Returns null if it is not installed.
- Since:
- GemFire 6.6.2
-
getManager
For internal use only.- Returns:
- the manager associated with this serializer
- Since:
- GemFire 8.2
-