Top Description Fields Constructors Methods
com.univocity.parsers.common.processor.core

public Class BeanConversionProcessor<T>

extends DefaultConversionProcessor
Class Inheritance
Known Direct Subclasses
com.univocity.parsers.common.processor.BeanWriterProcessor, com.univocity.parsers.common.processor.core.AbstractBeanProcessor
Type Parameters
<T>
the annotated class type.
Imports
com.univocity.parsers.annotations.*, com.univocity.parsers.annotations.helpers.*, com.univocity.parsers.common.*, com.univocity.parsers.common.beans.*, com.univocity.parsers.common.fields.*, com.univocity.parsers.common.processor.*, com.univocity.parsers.conversions.*, java.lang.annotation.*, java.lang.reflect.*, java.util.*

The base class for Processor and RowWriterProcessor implementations that support java beans annotated with the annotations provided in com.univocity.parsers.annotations.
Author
Univocity Software Pty Ltd - [email protected]
See Also
Processor, RowWriterProcessor

Field Summary

Modifier and TypeField and Description
pack-priv final Class<T>
private ColumnMapping
pack-priv final Constructor<T>
protected boolean
private int
private boolean
protected final MethodFilter
private FieldMapping[]
private Map<FieldMapping, BeanConversionProcessor<?>>
protected final Set<FieldMapping>
private FieldMapping[]
private Object[]
pack-priv boolean
private NormalizedString[]
protected final HeaderTransformer
private Object[]
Inherited from com.univocity.parsers.common.DefaultConversionProcessor:
conversions

Constructor Summary

AccessConstructor and Description
public
BeanConversionProcessor(Class<T>
the class annotated with one or more of the annotations provided in com.univocity.parsers.annotations.
beanType
)

Deprecated Use the BeanConversionProcessor(Class, MethodFilter) constructor instead.
Initializes the BeanConversionProcessor with the annotated bean class.
public
BeanConversionProcessor(Class<T>
the class annotated with one or more of the annotations provided in com.univocity.parsers.annotations.
beanType
,
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 with Parsed, when both methods target the same field.
methodFilter
)

Initializes the BeanConversionProcessor with the annotated bean class

pack-priv
BeanConversionProcessor(Class<T> beanType, HeaderTransformer transformer, MethodFilter methodFilter)

Method Summary

Modifier and TypeMethod and Description
protected void
addConversion(Conversion<I, O>
The conversion object that must be executed against the given field
conversion
,
FieldMapping
the helper object that contains information about how a field is mapped.
mapping
)

Associates a conversion to a field of the java bean class.

private boolean
applyDefaultConversion(Conversion<I, O> lastConversionApplied, Conversion<I, O> defaultConversion)

protected FieldConversionMapping

Returns:

a copy of the currently defined conversions
cloneConversions
()

Creates a copy of the manually defined conversions to be applied over any columns.

public T

Returns:

an instance of the java bean type defined in this class constructor.
createBean
(String[]
The values extracted from the parser
row
,
Context
The current state of the parsing process
context
)

Converts a record with values extracted from the parser into a java bean instance.

pack-priv BeanConversionProcessor<?>
createNestedProcessor(Annotation annotation, Class<T> nestedType, FieldMapping fieldMapping, HeaderTransformer transformer)

pack-priv static String
public Class<T>

Returns:

the class of the annotated java bean instances that will be manipulated by this processor.
getBeanClass
()

Returns the class of the annotated java bean instances that will be manipulated by this processor.

public final ColumnMapper

Returns:

the column mapper
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 Method
getConversionMethod(Conversion<I, O> conversion, String methodName)

pack-priv Map<FieldMapping, BeanConversionProcessor<?>>
private void
public final void
initialize()

Identifies and extracts fields annotated with the Parsed annotation

protected final void
initialize(String[]
headers parsed from the input.
headers
)

Identifies and extracts fields annotated with the Parsed annotation

protected final void
initialize(NormalizedString[]
headers parsed from the input.
headers
)

Identifies and extracts fields annotated with the Parsed annotation

private void
public boolean

Returns:

flag indicating whether strict validation of headers is enabled.
isStrictHeaderValidationEnabled
()

Returns a flag indicating whether all headers declared in the annotated class must be present in the input.

private void
mapFieldIndexes(Context context, Object[]
A row with values for the given java bean.
row
,
NormalizedString[]
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 in CommonSettings#getHeaders()
headers
,
int[]
The indexes of the headers or row that are actually being used. May be null if no fields have been selected using CommonSettings#selectFields(String...) or CommonSettings#selectIndexes(Integer...)
indexes
,
boolean
Indicates the indexes provided were reordered and do not match the original sequence of headers.
columnsReordered
)

Identifies which fields are associated with which columns in a row.

private void
mapFieldsToValues(T
the java bean instance to be read
instance
,
Object[]
object array that will receive the values extracted from java bean
row
,
NormalizedString[]
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 in CommonSettings#getHeaders()
headers
,
int[]
The indexes of the headers or row that are actually being used. May be null if no fields have been selected using CommonSettings#selectFields(String...) or CommonSettings#selectIndexes(Integer...)
indexes
,
boolean
Indicates the indexes provided were reordered and do not match the original sequence of headers.
columnsReordered
)

Iterates over all fields in the java bean instance and extracts its values.

pack-priv void
mapValuesToFields(T
the java bean instance that is going to have its properties set
instance
,
Object[]
the values to associate with each field of the javabean.
row
,
Context
information about the current parsing process.
context
)

Goes through a list of objects and associates each value to a particular field of a java bean instance

private int
private void
populateTargetMaps(Map<Object, Integer> targetCounts, Map<Object, String> targetSources)

pack-priv void
processField(AnnotatedElement element, String targetName, PropertyWrapper propertyDescriptor, NormalizedString[] headers)

protected boolean

Returns:

true if the given field should be processed, otherwise false.
processField
(FieldMapping
the field to be processed
field
)

Determines whether or not an annotated field should be processed.

pack-priv void
processNestedAttributes(String[] row, Object instance, Context context)

private void
processNestedField(Class<T> nestedType, AnnotatedElement element, String targetName, PropertyWrapper propertyDescriptor, NormalizedString[] headers, Nested nested)

public final Object[]

Returns:

a row of objects containing the values extracted from the java bean
reverseConversions
(T
an instance of the type defined in this class constructor.
bean
,
NormalizedString[]
All field names used to produce records in a given destination. May be null if no headers have been defined in CommonSettings#getHeaders()
headers
,
int[]
The indexes of the headers that are actually being written. May be null if no fields have been selected using CommonSettings#selectFields(String...) or CommonSettings#selectIndexes(Integer...)
indexesToWrite
)

Converts a java bean instance into a sequence of values for writing.

public void
setColumnMapper(ColumnMapper
the column mappings to use
columnMapper
)

Copies the given column mappings over to this processor.

public void
setStrictHeaderValidationEnabled(boolean
flag indicating whether strict validation of headers is enabled.
strictHeaderValidationEnabled
)

Defines whether all headers declared in the annotated class must be present in the input.

private void
setupConversions(AnnotatedElement
the field and method annotated with Parsed that must be associated with one or more Conversion objects
target
,
FieldMapping
a helper class to store information how the field or method is mapped to a parsed record.
mapping
)

Goes through each field and method annotated with Parsed and extracts the sequence of Conversion elements associated with each one.

pack-priv void
private void
Inherited from com.univocity.parsers.common.DefaultConversionProcessor:
applyConversionsconvertAllconvertFieldsconvertIndexesconvertTypehandleConversionErrorinitializeConversionsreverseConversionstoDataProcessingException

Field Detail

beanClassback to summary
pack-priv final Class<T> beanClass
columnMapperback to summary
private ColumnMapping columnMapper
constructorback to summary
pack-priv final Constructor<T> constructor
initializedback to summary
protected boolean initialized
lastFieldIndexMappedback to summary
private int lastFieldIndexMapped
mappingsForWritingValidatedback to summary
private boolean mappingsForWritingValidated
methodFilterback to summary
protected final MethodFilter methodFilter
missingback to summary
private FieldMapping[] missing
nestedAttributesback to summary
private Map<FieldMapping, BeanConversionProcessor<?>> nestedAttributes
parsedFieldsback to summary
protected final Set<FieldMapping> parsedFields
readOrderback to summary
private FieldMapping[] readOrder
rowback to summary
private Object[] row
strictHeaderValidationEnabledback to summary
pack-priv boolean strictHeaderValidationEnabled
syntheticHeadersback to summary
private NormalizedString[] syntheticHeaders
transformerback to summary
protected final HeaderTransformer transformer
valuesForMissingback to summary
private Object[] valuesForMissing

Constructor Detail

BeanConversionProcessorback to summary
public BeanConversionProcessor(Class<T> beanType)

Deprecated

Use the BeanConversionProcessor(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:Class<T>

the class annotated with one or more of the annotations provided in com.univocity.parsers.annotations.

Annotations
@Deprecated
BeanConversionProcessorback to summary
public BeanConversionProcessor(Class<T> beanType, MethodFilter methodFilter)

Initializes the BeanConversionProcessor with the annotated bean class

Parameters
beanType:Class<T>

the class annotated with one or more of the annotations provided in com.univocity.parsers.annotations.

methodFilter: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 with Parsed, when both methods target the same field.

BeanConversionProcessorback to summary
pack-priv BeanConversionProcessor(Class<T> beanType, HeaderTransformer transformer, MethodFilter methodFilter)

Method Detail

addConversionback to summary
protected void addConversion(Conversion<I, O> conversion, FieldMapping mapping)

Associates a conversion to a field of the java bean class.

Parameters
conversion:Conversion<I, O>

The conversion object that must be executed against the given field

mapping:FieldMapping

the helper object that contains information about how a field is mapped.

Annotations
@SuppressWarnings:rawtypes
applyDefaultConversionback to summary
private boolean applyDefaultConversion(Conversion<I, O> lastConversionApplied, Conversion<I, O> defaultConversion)
Annotations
@SuppressWarnings:rawtypes
cloneConversionsback to summary
protected FieldConversionMapping cloneConversions()

Creates a copy of the manually defined conversions to be applied over any columns.

Returns:FieldConversionMapping

a copy of the currently defined conversions

createBeanback to summary
public T createBean(String[] row, Context context)

Converts a record with values extracted from the parser into a java bean instance.

Parameters
row:String[]

The values extracted from the parser

context:Context

The current state of the parsing process

Returns:T

an instance of the java bean type defined in this class constructor.

createNestedProcessorback to summary
pack-priv BeanConversionProcessor<?> createNestedProcessor(Annotation annotation, Class<T> nestedType, FieldMapping fieldMapping, HeaderTransformer transformer)
describeFieldback to summary
pack-priv static String describeField(AnnotatedElement target)
getBeanClassback to summary
public Class<T> getBeanClass()

Returns the class of the annotated java bean instances that will be manipulated by this processor.

Returns:Class<T>

the class of the annotated java bean instances that will be manipulated by this processor.

getColumnMapperback to summary
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 with Parsed nor Nested. Any mappings defined with the column mapper will take precedence over these annotations.

Returns:ColumnMapper

the column mapper

getConversionMethodback to summary
private Method getConversionMethod(Conversion<I, O> conversion, String methodName)
Annotations
@SuppressWarnings:rawtypes
getNestedAttributesback to summary
pack-priv Map<FieldMapping, BeanConversionProcessor<?>> getNestedAttributes()
identifyLiteralsback to summary
private void identifyLiterals()
initializeback to summary
public final void initialize()

Identifies and extracts fields annotated with the Parsed annotation

initializeback to summary
protected final void initialize(String[] headers)

Identifies and extracts fields annotated with the Parsed annotation

Parameters
headers:String[]

headers parsed from the input.

initializeback to summary
protected final void initialize(NormalizedString[] headers)

Identifies and extracts fields annotated with the Parsed annotation

Parameters
headers:NormalizedString[]

headers parsed from the input.

initializeValuesForMissingback to summary
private void initializeValuesForMissing()
isStrictHeaderValidationEnabledback to summary
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:boolean

flag indicating whether strict validation of headers is enabled.

mapFieldIndexesback to summary
private void mapFieldIndexes(Context context, Object[] row, NormalizedString[] headers, int[] indexes, boolean columnsReordered)

Identifies which fields are associated with which columns in a row.

Parameters
row:Object[]

A row with values for the given java bean.

headers:NormalizedString[]

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 in CommonSettings#getHeaders()

indexes:int[]

The indexes of the headers or row that are actually being used. May be null if no fields have been selected using CommonSettings#selectFields(String...) or CommonSettings#selectIndexes(Integer...)

columnsReordered:boolean

Indicates the indexes provided were reordered and do not match the original sequence of headers.

mapFieldsToValuesback to summary
private void mapFieldsToValues(T instance, Object[] row, NormalizedString[] headers, int[] indexes, boolean columnsReordered)

Iterates over all fields in the java bean instance and extracts its values.

Parameters
instance:T

the java bean instance to be read

row:Object[]

object array that will receive the values extracted from java bean

headers:NormalizedString[]

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 in CommonSettings#getHeaders()

indexes:int[]

The indexes of the headers or row that are actually being used. May be null if no fields have been selected using CommonSettings#selectFields(String...) or CommonSettings#selectIndexes(Integer...)

columnsReordered:boolean

Indicates the indexes provided were reordered and do not match the original sequence of headers.

mapValuesToFieldsback to summary
pack-priv void mapValuesToFields(T instance, 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:T

the java bean instance that is going to have its properties set

row:Object[]

the values to associate with each field of the javabean.

context:Context

information about the current parsing process.

nonNullReadOrderLengthback to summary
private int nonNullReadOrderLength()
populateTargetMapsback to summary
private void populateTargetMaps(Map<Object, Integer> targetCounts, Map<Object, String> targetSources)
processFieldback to summary
pack-priv void processField(AnnotatedElement element, String targetName, PropertyWrapper propertyDescriptor, NormalizedString[] headers)
processFieldback to summary
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:FieldMapping

the field to be processed

Returns:boolean

true if the given field should be processed, otherwise false.

processNestedAttributesback to summary
pack-priv void processNestedAttributes(String[] row, Object instance, Context context)
processNestedFieldback to summary
private void processNestedField(Class<T> nestedType, AnnotatedElement element, String targetName, PropertyWrapper propertyDescriptor, NormalizedString[] headers, Nested nested)
reverseConversionsback to summary
public final Object[] reverseConversions(T bean, NormalizedString[] headers, int[] indexesToWrite)

Converts a java bean instance into a sequence of values for writing.

Parameters
bean:T

an instance of the type defined in this class constructor.

headers:NormalizedString[]

All field names used to produce records in a given destination. May be null if no headers have been defined in CommonSettings#getHeaders()

indexesToWrite:int[]

The indexes of the headers that are actually being written. May be null if no fields have been selected using CommonSettings#selectFields(String...) or CommonSettings#selectIndexes(Integer...)

Returns:Object[]

a row of objects containing the values extracted from the java bean

setColumnMapperback to summary
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:ColumnMapper

the column mappings to use

setStrictHeaderValidationEnabledback to summary
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:boolean

flag indicating whether strict validation of headers is enabled.

setupConversionsback to summary
private void setupConversions(AnnotatedElement target, FieldMapping mapping)

Goes through each field and method annotated with Parsed and extracts the sequence of Conversion elements associated with each one.

Parameters
target:AnnotatedElement

the field and method annotated with Parsed that must be associated with one or more Conversion objects

mapping:FieldMapping

a helper class to store information how the field or method is mapped to a parsed record.

Annotations
@SuppressWarnings:rawtypes
validateMappingsback to summary
pack-priv void validateMappings()
validateMappingsForWritingback to summary
private void validateMappingsForWriting()