public class ReflectionBasedAutoSerializer extends Object implements PdxSerializer, Declarable
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.Constructor and Description |
---|
ReflectionBasedAutoSerializer()
Default 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(List<String> classes)
Deprecated.
as of 6.6.2 use ReflectionBasedAutoSerializer(String...) instead.
|
ReflectionBasedAutoSerializer(String... patterns)
Constructor which takes a list of class name patterns which are to be auto-serialized.
|
Modifier and Type | Method and Description |
---|---|
Object |
fromData(Class<?> clazz,
PdxReader reader)
Method implemented from
PdxSerializer which performs object de-serialization. |
Properties |
getConfig()
Return a
Properties object with a representation of the current config. |
String |
getFieldName(Field f,
Class<?> clazz)
Controls the field name that will be used in pdx for a field being auto serialized.
|
FieldType |
getFieldType(Field f,
Class<?> clazz)
Controls what pdx field type will be used when auto serializing.
|
Object |
getManager()
For internal use only.
|
RegionService |
getRegionService() |
void |
init(Properties props)
Deprecated.
as of Geode 1.5 use initialize instead
|
void |
initialize(Cache cache,
Properties props)
Used for declarative class initialization from cache.xml.
|
boolean |
isClassAutoSerialized(Class<?> clazz)
Controls what classes will be auto serialized by this serializer.
|
boolean |
isFieldIncluded(Field f,
Class<?> clazz)
Controls what fields of a class will be auto serialized by this serializer.
|
boolean |
isIdentityField(Field f,
Class<?> clazz)
Controls what fields of a class that is auto serialized will be marked as pdx identity fields.
|
Object |
readTransform(Field f,
Class<?> clazz,
Object serializedValue)
Controls what field value is read during auto deserialization.
|
void |
reconfigure(boolean checkPortability,
String... patterns)
Method to reconfigure this serializer.
|
void |
reconfigure(String... patterns)
Method to reconfigure this serializer.
|
void |
setSerializableClasses(List<String> patterns)
Deprecated.
as of 6.6.2 use
reconfigure(String...) instead. |
boolean |
toData(Object obj,
PdxWriter writer)
Method implemented from
PdxSerializer which performs object serialization. |
boolean |
transformFieldValue(Field f,
Class<?> clazz)
Controls if a pdx field's value can be transformed during serialization.
|
Object |
writeTransform(Field f,
Class<?> clazz,
Object originalValue)
Controls what field value is written during auto serialization.
|
public ReflectionBasedAutoSerializer()
ReflectionBasedAutoSerializer(String...)
instead.@Deprecated public ReflectionBasedAutoSerializer(List<String> classes)
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=creationDateThis would select all classes with a class name beginning with
com.company.DomainObject
and would select as PDX identity fields any fields
beginning with id
and would not serialize the field called
creationDate
.
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=creationDateNote 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.
classes
- the patterns which are matched against domain class names to determine whether
they should be serializedpublic ReflectionBasedAutoSerializer(String... patterns)
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=creationDateThis would select all classes with a class name beginning with
com.company.DomainObject
and would select as PDX identity fields any fields
beginning with id
and would not serialize the field called
creationDate
.
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=creationDateNote 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.
patterns
- the patterns which are matched against domain class names to determine whether
they should be serializedpublic ReflectionBasedAutoSerializer(boolean checkPortability, String... patterns)
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=creationDateThis would select all classes with a class name beginning with
com.company.DomainObject
and would select as PDX identity fields any fields
beginning with id
and would not serialize the field called
creationDate
.
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=creationDateNote 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.
checkPortability
- if true
then 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@Deprecated public void setSerializableClasses(List<String> patterns)
reconfigure(String...)
instead.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=creationDateThis would select all classes with a class name beginning with
com.company.DomainObject
and would select as PDX identity fields any fields
beginning with id
and would not serialize the field called
creationDate
.
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=creationDateNote 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.
patterns
- the list of definitions to applypublic void reconfigure(String... patterns)
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=creationDateThis would select all classes with a class name beginning with
com.company.DomainObject
and would select as PDX identity fields any fields
beginning with id
and would not serialize the field called
creationDate
.
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=creationDateNote 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.
patterns
- the definitions to applypublic void reconfigure(boolean checkPortability, String... patterns)
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=creationDateThis would select all classes with a class name beginning with
com.company.DomainObject
and would select as PDX identity fields any fields
beginning with id
and would not serialize the field called
creationDate
.
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=creationDateNote 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.
patterns
- the definitions to applycheckPortability
- if true
then an serialization done by this serializer will
throw an exception if the object it not portable to non-java languages.public boolean toData(Object obj, PdxWriter writer)
PdxSerializer
which performs object serialization.toData
in interface PdxSerializer
obj
- the object to serializewriter
- the PdxWriter
to use when serializing this objecttrue
if the object was serialized, false
otherwisepublic Object fromData(Class<?> clazz, PdxReader reader)
PdxSerializer
which performs object de-serialization.fromData
in interface PdxSerializer
clazz
- the class of the object to re-createreader
- the PdxReader
to use when creating this object@Deprecated public void init(Properties props)
reconfigure
for
specifics.init
in interface Declarable
props
- properties used to configure the auto serializerpublic void initialize(Cache cache, Properties props)
reconfigure
for
specifics.initialize
in interface Declarable
cache
- the cache that owns this serializerprops
- properties used to configure the auto serializerpublic Properties getConfig()
Properties
object with a representation of the current config. Depending
on how this ReflectionBasedAutoSerializer
was configured, the returned property
value will have the correct semantics but may differ from the the original configuration
string.Properties
objectpublic boolean isClassAutoSerialized(Class<?> clazz)
The default implementation:
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.
clazz
- the class that is being considered for auto serialization.public boolean isFieldIncluded(Field f, Class<?> clazz)
The default implementation:
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.
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 use Field.getDeclaringClass()
.public String getFieldName(Field f, Class<?> clazz)
PdxInstance
and 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.
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 use Field.getDeclaringClass()
.public boolean isIdentityField(Field f, Class<?> clazz)
PdxInstance
computes 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.
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 use Field.getDeclaringClass()
.public FieldType getFieldType(Field f, Class<?> clazz)
The default implementation uses FieldType.get(Class)
by passing it
Field.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.
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 use Field.getDeclaringClass()
.public boolean transformFieldValue(Field f, Class<?> clazz)
writeTransform(java.lang.reflect.Field, java.lang.Class<?>, java.lang.Object)
and readTransform(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.
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 use Field.getDeclaringClass()
.writeTransform(java.lang.reflect.Field, java.lang.Class<?>, java.lang.Object)
and readTransform(java.lang.reflect.Field, java.lang.Class<?>, java.lang.Object)
need to be called when
serializing and deserializing this field's value.public Object writeTransform(Field f, Class<?> clazz, Object originalValue)
transformFieldValue(java.lang.reflect.Field, java.lang.Class<?>)
returned true.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 use Field.getDeclaringClass()
.originalValue
- the value of the field that was read from the domain object.originalValue
if you
decide not to transform the value.public Object readTransform(Field f, Class<?> clazz, Object serializedValue)
transformFieldValue(java.lang.reflect.Field, java.lang.Class<?>)
returned true.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 use Field.getDeclaringClass()
.serializedValue
- the value of the field that was serialized for this field.serializedValue
if you
decide not to transform the value.public RegionService getRegionService()
public Object getManager()