Class BeanConversionProcessor<T>
- java.lang.Object
-
- com.univocity.parsers.common.DefaultConversionProcessor
-
- com.univocity.parsers.common.processor.core.BeanConversionProcessor<T>
-
- Type Parameters:
T
- the annotated class type.
- All Implemented Interfaces:
ConversionProcessor
- Direct Known Subclasses:
AbstractBeanProcessor
,BeanWriterProcessor
public class BeanConversionProcessor<T> extends DefaultConversionProcessor
The base class forProcessor
andRowWriterProcessor
implementations that support java beans annotated with the annotations provided incom.univocity.parsers.annotations
.- See Also:
Processor
,RowWriterProcessor
-
-
Field Summary
Fields Modifier and Type Field Description (package private) java.lang.Class<T>
beanClass
private ColumnMapping
columnMapper
(package private) java.lang.reflect.Constructor<T>
constructor
protected boolean
initialized
private int
lastFieldIndexMapped
private boolean
mappingsForWritingValidated
protected MethodFilter
methodFilter
private FieldMapping[]
missing
private java.util.Map<FieldMapping,BeanConversionProcessor<?>>
nestedAttributes
protected java.util.Set<FieldMapping>
parsedFields
private FieldMapping[]
readOrder
private java.lang.Object[]
row
(package private) boolean
strictHeaderValidationEnabled
private NormalizedString[]
syntheticHeaders
protected HeaderTransformer
transformer
private java.lang.Object[]
valuesForMissing
-
Fields inherited from class com.univocity.parsers.common.DefaultConversionProcessor
conversions
-
-
Constructor Summary
Constructors Constructor Description BeanConversionProcessor(java.lang.Class<T> beanType)
Deprecated.Use theBeanConversionProcessor(Class, MethodFilter)
constructor instead.BeanConversionProcessor(java.lang.Class<T> beanType, HeaderTransformer transformer, MethodFilter methodFilter)
BeanConversionProcessor(java.lang.Class<T> beanType, MethodFilter methodFilter)
Initializes the BeanConversionProcessor with the annotated bean class
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
addConversion(Conversion conversion, FieldMapping mapping)
Associates a conversion to a field of the java bean class.private boolean
applyDefaultConversion(Conversion lastConversionApplied, Conversion defaultConversion)
protected FieldConversionMapping
cloneConversions()
Creates a copy of the manually defined conversions to be applied over any columns.T
createBean(java.lang.String[] row, Context context)
Converts a record with values extracted from the parser into a java bean instance.(package private) BeanConversionProcessor<?>
createNestedProcessor(java.lang.annotation.Annotation annotation, java.lang.Class nestedType, FieldMapping fieldMapping, HeaderTransformer transformer)
(package private) static java.lang.String
describeField(java.lang.reflect.AnnotatedElement target)
java.lang.Class<T>
getBeanClass()
Returns the class of the annotated java bean instances that will be manipulated by this processor.ColumnMapper
getColumnMapper()
Returns a mapper that allows users to manually define mappings from attributes/methods of a given class to columns to be parsed or written.private java.lang.reflect.Method
getConversionMethod(Conversion conversion, java.lang.String methodName)
(package private) java.util.Map<FieldMapping,BeanConversionProcessor<?>>
getNestedAttributes()
private void
identifyLiterals()
void
initialize()
Identifies and extracts fields annotated with theParsed
annotationprotected void
initialize(NormalizedString[] headers)
Identifies and extracts fields annotated with theParsed
annotationprotected void
initialize(java.lang.String[] headers)
Identifies and extracts fields annotated with theParsed
annotationprivate void
initializeValuesForMissing()
boolean
isStrictHeaderValidationEnabled()
Returns a flag indicating whether all headers declared in the annotated class must be present in the input.private void
mapFieldIndexes(Context context, java.lang.Object[] row, NormalizedString[] headers, int[] indexes, boolean columnsReordered)
Identifies which fields are associated with which columns in a row.private void
mapFieldsToValues(T instance, java.lang.Object[] row, NormalizedString[] headers, int[] indexes, boolean columnsReordered)
Iterates over all fields in the java bean instance and extracts its values.(package private) void
mapValuesToFields(T instance, java.lang.Object[] row, Context context)
Goes through a list of objects and associates each value to a particular field of a java bean instanceprivate int
nonNullReadOrderLength()
private void
populateTargetMaps(java.util.Map<java.lang.Object,java.lang.Integer> targetCounts, java.util.Map<java.lang.Object,java.lang.String> targetSources)
protected boolean
processField(FieldMapping field)
Determines whether or not an annotated field should be processed.(package private) void
processField(java.lang.reflect.AnnotatedElement element, java.lang.String targetName, PropertyWrapper propertyDescriptor, NormalizedString[] headers)
(package private) void
processNestedAttributes(java.lang.String[] row, java.lang.Object instance, Context context)
private void
processNestedField(java.lang.Class nestedType, java.lang.reflect.AnnotatedElement element, java.lang.String targetName, PropertyWrapper propertyDescriptor, NormalizedString[] headers, Nested nested)
java.lang.Object[]
reverseConversions(T bean, NormalizedString[] headers, int[] indexesToWrite)
Converts a java bean instance into a sequence of values for writing.void
setColumnMapper(ColumnMapper columnMapper)
Copies the given column mappings over to this processor.void
setStrictHeaderValidationEnabled(boolean strictHeaderValidationEnabled)
Defines whether all headers declared in the annotated class must be present in the input.private void
setupConversions(java.lang.reflect.AnnotatedElement target, FieldMapping mapping)
Goes through each field and method annotated withParsed
and extracts the sequence ofConversion
elements associated with each one.(package private) void
validateMappings()
private void
validateMappingsForWriting()
-
Methods inherited from class com.univocity.parsers.common.DefaultConversionProcessor
applyConversions, convertAll, convertFields, convertIndexes, convertType, handleConversionError, initializeConversions, reverseConversions, toDataProcessingException
-
-
-
-
Field Detail
-
beanClass
final java.lang.Class<T> beanClass
-
constructor
final java.lang.reflect.Constructor<T> constructor
-
parsedFields
protected final java.util.Set<FieldMapping> parsedFields
-
lastFieldIndexMapped
private int lastFieldIndexMapped
-
readOrder
private FieldMapping[] readOrder
-
missing
private FieldMapping[] missing
-
valuesForMissing
private java.lang.Object[] valuesForMissing
-
initialized
protected boolean initialized
-
strictHeaderValidationEnabled
boolean strictHeaderValidationEnabled
-
syntheticHeaders
private NormalizedString[] syntheticHeaders
-
row
private java.lang.Object[] row
-
nestedAttributes
private java.util.Map<FieldMapping,BeanConversionProcessor<?>> nestedAttributes
-
transformer
protected final HeaderTransformer transformer
-
methodFilter
protected final MethodFilter methodFilter
-
columnMapper
private ColumnMapping columnMapper
-
mappingsForWritingValidated
private boolean mappingsForWritingValidated
-
-
Constructor Detail
-
BeanConversionProcessor
@Deprecated public BeanConversionProcessor(java.lang.Class<T> beanType)
Deprecated.Use theBeanConversionProcessor(Class, MethodFilter)
constructor instead.Initializes the BeanConversionProcessor with the annotated bean class. If any method of the given class has annotations, only the setter methods will be used (getters will be ignored), making this processor useful mostly for parsing into instances of the given class.- Parameters:
beanType
- the class annotated with one or more of the annotations provided incom.univocity.parsers.annotations
.
-
BeanConversionProcessor
public BeanConversionProcessor(java.lang.Class<T> beanType, MethodFilter methodFilter)
Initializes the BeanConversionProcessor with the annotated bean class- Parameters:
beanType
- the class annotated with one or more of the annotations provided incom.univocity.parsers.annotations
.methodFilter
- filter to apply over annotated methods when the processor is reading data from beans (to write values to an output) or writing values into beans (when parsing). It is used to choose either a "get" or a "set" method annotated withParsed
, when both methods target the same field.
-
BeanConversionProcessor
BeanConversionProcessor(java.lang.Class<T> beanType, HeaderTransformer transformer, MethodFilter methodFilter)
-
-
Method Detail
-
isStrictHeaderValidationEnabled
public boolean isStrictHeaderValidationEnabled()
Returns a flag indicating whether all headers declared in the annotated class must be present in the input. If enabled, an exception will be thrown in case the input data does not contain all headers required.- Returns:
- flag indicating whether strict validation of headers is enabled.
-
initialize
public final void initialize()
Identifies and extracts fields annotated with theParsed
annotation
-
getColumnMapper
public final ColumnMapper getColumnMapper()
Returns a mapper that allows users to manually define mappings from attributes/methods of a given class to columns to be parsed or written. This allows users to use instances of classes that are not annotated withParsed
norNested
. Any mappings defined with the column mapper will take precedence over these annotations.- Returns:
- the column mapper
-
initialize
protected final void initialize(java.lang.String[] headers)
Identifies and extracts fields annotated with theParsed
annotation- Parameters:
headers
- headers parsed from the input.
-
initialize
protected final void initialize(NormalizedString[] headers)
Identifies and extracts fields annotated with theParsed
annotation- Parameters:
headers
- headers parsed from the input.
-
identifyLiterals
private void identifyLiterals()
-
setStrictHeaderValidationEnabled
public void setStrictHeaderValidationEnabled(boolean strictHeaderValidationEnabled)
Defines whether all headers declared in the annotated class must be present in the input. If enabled, an exception will be thrown in case the input data does not contain all headers required.- Parameters:
strictHeaderValidationEnabled
- flag indicating whether strict validation of headers is enabled.
-
processField
void processField(java.lang.reflect.AnnotatedElement element, java.lang.String targetName, PropertyWrapper propertyDescriptor, NormalizedString[] headers)
-
processNestedField
private void processNestedField(java.lang.Class nestedType, java.lang.reflect.AnnotatedElement element, java.lang.String targetName, PropertyWrapper propertyDescriptor, NormalizedString[] headers, Nested nested)
-
cloneConversions
protected FieldConversionMapping cloneConversions()
Creates a copy of the manually defined conversions to be applied over any columns.- Returns:
- a copy of the currently defined conversions
-
getNestedAttributes
java.util.Map<FieldMapping,BeanConversionProcessor<?>> getNestedAttributes()
-
createNestedProcessor
BeanConversionProcessor<?> createNestedProcessor(java.lang.annotation.Annotation annotation, java.lang.Class nestedType, FieldMapping fieldMapping, HeaderTransformer transformer)
-
processField
protected boolean processField(FieldMapping field)
Determines whether or not an annotated field should be processed. Can be overridden by subclasses for fine grained control.- Parameters:
field
- the field to be processed- Returns:
true
if the given field should be processed, otherwisefalse
.
-
validateMappings
void validateMappings()
-
describeField
static java.lang.String describeField(java.lang.reflect.AnnotatedElement target)
-
setupConversions
private void setupConversions(java.lang.reflect.AnnotatedElement target, FieldMapping mapping)
Goes through each field and method annotated withParsed
and extracts the sequence ofConversion
elements associated with each one.- Parameters:
target
- the field and method annotated withParsed
that must be associated with one or moreConversion
objectsmapping
- a helper class to store information how the field or method is mapped to a parsed record.
-
applyDefaultConversion
private boolean applyDefaultConversion(Conversion lastConversionApplied, Conversion defaultConversion)
-
getConversionMethod
private java.lang.reflect.Method getConversionMethod(Conversion conversion, java.lang.String methodName)
-
addConversion
protected void addConversion(Conversion conversion, FieldMapping mapping)
Associates a conversion to a field of the java bean class.- Parameters:
conversion
- The conversion object that must be executed against the given fieldmapping
- the helper object that contains information about how a field is mapped.
-
mapValuesToFields
void mapValuesToFields(T instance, java.lang.Object[] row, Context context)
Goes through a list of objects and associates each value to a particular field of a java bean instance- Parameters:
instance
- the java bean instance that is going to have its properties setrow
- the values to associate with each field of the javabean.context
- information about the current parsing process.
-
mapFieldIndexes
private void mapFieldIndexes(Context context, java.lang.Object[] row, NormalizedString[] headers, int[] indexes, boolean columnsReordered)
Identifies which fields are associated with which columns in a row.- Parameters:
row
- A row with values for the given java bean.headers
- The names of all fields of the record (including any header that is not mapped to the java bean). May be null if no headers have been defined inCommonSettings.getHeaders()
indexes
- The indexes of the headers or row that are actually being used. May be null if no fields have been selected usingCommonSettings.selectFields(String...)
orCommonSettings.selectIndexes(Integer...)
columnsReordered
- Indicates the indexes provided were reordered and do not match the original sequence of headers.
-
nonNullReadOrderLength
private int nonNullReadOrderLength()
-
initializeValuesForMissing
private void initializeValuesForMissing()
-
createBean
public T createBean(java.lang.String[] row, Context context)
Converts a record with values extracted from the parser into a java bean instance.- Parameters:
row
- The values extracted from the parsercontext
- The current state of the parsing process- Returns:
- an instance of the java bean type defined in this class constructor.
-
processNestedAttributes
void processNestedAttributes(java.lang.String[] row, java.lang.Object instance, Context context)
-
mapFieldsToValues
private void mapFieldsToValues(T instance, java.lang.Object[] row, NormalizedString[] headers, int[] indexes, boolean columnsReordered)
Iterates over all fields in the java bean instance and extracts its values.- Parameters:
instance
- the java bean instance to be readrow
- object array that will receive the values extracted from java beanheaders
- The names of all fields of the record (including any header that is not mapped to the java bean). May be null if no headers have been defined inCommonSettings.getHeaders()
indexes
- The indexes of the headers or row that are actually being used. May be null if no fields have been selected usingCommonSettings.selectFields(String...)
orCommonSettings.selectIndexes(Integer...)
columnsReordered
- Indicates the indexes provided were reordered and do not match the original sequence of headers.
-
reverseConversions
public final java.lang.Object[] reverseConversions(T bean, NormalizedString[] headers, int[] indexesToWrite)
Converts a java bean instance into a sequence of values for writing.- Parameters:
bean
- an instance of the type defined in this class constructor.headers
- All field names used to produce records in a given destination. May be null if no headers have been defined inCommonSettings.getHeaders()
indexesToWrite
- The indexes of the headers that are actually being written. May be null if no fields have been selected usingCommonSettings.selectFields(String...)
orCommonSettings.selectIndexes(Integer...)
- Returns:
- a row of objects containing the values extracted from the java bean
-
getBeanClass
public java.lang.Class<T> getBeanClass()
Returns the class of the annotated java bean instances that will be manipulated by this processor.- Returns:
- the class of the annotated java bean instances that will be manipulated by this processor.
-
setColumnMapper
public void setColumnMapper(ColumnMapper columnMapper)
Copies the given column mappings over to this processor. Further changes to the given object won't be reflected on the copy stored internally.- Parameters:
columnMapper
- the column mappings to use
-
validateMappingsForWriting
private void validateMappingsForWriting()
-
populateTargetMaps
private void populateTargetMaps(java.util.Map<java.lang.Object,java.lang.Integer> targetCounts, java.util.Map<java.lang.Object,java.lang.String> targetSources)
-
-