Interface BeanConverterExtension
- All Known Implementing Classes:
CollectionExtension,JavaTimeExtension,MapPrimitiveKeyExtension,OptionalExtension,StandardBeanConverterExtension
public interface BeanConverterExtension
Interface for extensions of the PersistentBeanConverter.
The PersistentBeanFactory is responsible for creating a generic data-structure for
java beans. Also it will convert back this data-structures to the original objects.
The reason for this is to omit the type information when serializing objects.
As this is a highly generic and complicated process DeepSampler offers extensions
for this BeanFactory the BeanConverterExtension. To add an extension you have
to specify it when recording/loading samples with PersistentSampler
(PersistentSampleManager.addBeanExtension(BeanConverterExtension).
The extension will be used for all processed objects (including embedded objects).
When you write an extension you firstly have to define for which type you
want to extend the behavior of the original BeanFactory. For this purpose you
can use isProcessable(Class, ParameterizedType).
After this, every bean with the specified type will be processed within the extension. Now
you can implement custom conversion logic yourBean -> generic data-structure (
convert(Object, ParameterizedType, PersistentBeanConverter)
and generic data-structure -> yourBean (revert(Object, Class, ParameterizedType, PersistentBeanConverter)).
It is also possible to skip the processing of all types for which
your implementation of isProcessable(Class, ParameterizedType) will return true. This is done by implementing
skip(Class, ParameterizedType) So you can exclude some types from being processed by the PersistentBeanConverter.
StandardBeanConverterExtension which is a simplified implementation of this interface.-
Method Summary
Modifier and Type Method Description java.lang.Objectconvert(java.lang.Object originalBean, java.lang.reflect.ParameterizedType beanType, PersistentBeanConverter persistentBeanConverter)Conversion logic for the type you defined to process and not to skip.booleanisProcessable(java.lang.Class<?> beanClass, java.lang.reflect.ParameterizedType beanType)Checks it this extension is responsible for objects of the type beanType.<T> Trevert(java.lang.Object persistentBean, java.lang.Class<T> targetClass, java.lang.reflect.ParameterizedType targetType, PersistentBeanConverter persistentBeanConverter)Conversion logic for the generic data-structure to the processed bean type.booleanskip(java.lang.Class<?> beanClass, java.lang.reflect.ParameterizedType beanType)Skip the conversion of all beans of the given type.
-
Method Details
-
isProcessable
boolean isProcessable(java.lang.Class<?> beanClass, java.lang.reflect.ParameterizedType beanType)Checks it this extension is responsible for objects of the type beanType.- Parameters:
beanClass- theClassof the type that will be processed by this extension.beanType- theParameterizedTypeof the type that will be processed by this extension. This parameter can only be supplied if the type is actually a generic type. If this is not the case, beanType is null.- Returns:
- true if the
Typeshould be processed within this extension, false otherwise
-
skip
boolean skip(java.lang.Class<?> beanClass, java.lang.reflect.ParameterizedType beanType)Skip the conversion of all beans of the given type. Skipped objects will be sent directly to the underlying persistence api. Therefore the persistence api must be able to handle the serialization / deserialization.- Parameters:
beanClass- theClassof the type that will be skipped by this extension.beanType- theParameterizedTypethat will be skipped by this extension. This parameter can only be supplied if the type is actually a generic type. If this is not the case, beanType is null.- Returns:
- true if the Type should be skipped
-
convert
java.lang.Object convert(java.lang.Object originalBean, java.lang.reflect.ParameterizedType beanType, PersistentBeanConverter persistentBeanConverter)Conversion logic for the type you defined to process and not to skip. Converts an original bean to thePersistentBeanwhich will be sent to the persistence api.It is also possible to convert bean to any other data structure if the underlying persistence api is fully capable of handling the data structure on its own.
- Parameters:
originalBean- the original bean that is supposed to be converted to a serializable data structure, most likely aPersistentBean.beanType- theParameterizedTypethat will be used for the conversion. This parameter can only be supplied if the type is actually a generic type. If this is not the case, beanType is null.persistentBeanConverter- the currentPersistentBeanConverterthat may be used to convert sub objects of bean.- Returns:
- the generic data-structure for the bean
-
revert
<T> T revert(java.lang.Object persistentBean, java.lang.Class<T> targetClass, java.lang.reflect.ParameterizedType targetType, PersistentBeanConverter persistentBeanConverter)Conversion logic for the generic data-structure to the processed bean type. Reverts to the original Bean by converting thePersistentBean, which was deserialized by the underlying persistence api, to the original object.It is also possible to deserialize other types then
PersistentBeanif the underlying persistence api is fully capable of deserializing this type.revert() is not called, if the persisted value is null. In that case, null will be returned as the deserialized value without calling an extension. If null needs to be converted, the value must be encapsulated in a
DefaultPersistentBean.- Type Parameters:
T- type of the original bean- Parameters:
persistentBean- the generic beantargetClass- theClassof the type that will be created from the persistentBean.targetType- theParameterizedTypefor the type that will be created from persistentBean, This parameter can only be supplied if the type is actually a generic type. If this is not the case, targetType is null.persistentBeanConverter- the currentPersistentBeanConverterthat may be used to revert sub objects of persistentBean.- Returns:
- original bean
-