T
- the annotated class type.public class BeanConversionProcessor<T> extends DefaultConversionProcessor
Processor
and RowWriterProcessor
implementations that support java beans annotated with the annotations provided in com.univocity.parsers.annotations
.Processor
,
RowWriterProcessor
Modifier and Type | Field and Description |
---|---|
(package private) java.lang.Class<T> |
beanClass |
protected boolean |
initialized |
private int |
lastFieldIndexMapped |
private 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 java.lang.String[] |
syntheticHeaders |
private HeaderTransformer |
transformer |
private java.lang.Object[] |
valuesForMissing |
Constructor and Description |
---|
BeanConversionProcessor(java.lang.Class<T> beanType)
Deprecated.
Use the
BeanConversionProcessor(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
|
Modifier and Type | Method and 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) |
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.
|
private java.lang.reflect.Method |
getConversionMethod(Conversion conversion,
java.lang.String methodName) |
(package private) java.util.Map<FieldMapping,BeanConversionProcessor<?>> |
getNestedAttributes() |
void |
initialize()
Identifies and extracts fields annotated with the
Parsed annotation |
protected void |
initialize(java.lang.String[] headers)
Identifies and extracts fields annotated with the
Parsed annotation |
private 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,
java.lang.String[] 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,
java.lang.String[] 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 instance
|
(package private) void |
processField(java.lang.reflect.AnnotatedElement element,
PropertyWrapper propertyDescriptor,
java.lang.String[] headers) |
protected boolean |
processField(FieldMapping field)
Determines whether or not an annotated field should be processed.
|
(package private) void |
processNestedAttributes(java.lang.String[] row,
java.lang.Object instance,
Context context) |
java.lang.Object[] |
reverseConversions(T bean,
java.lang.String[] headers,
int[] indexesToWrite)
Converts a java bean instance into a sequence of values for writing.
|
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 with
Parsed and extracts the sequence of Conversion elements associated with each one. |
(package private) void |
validateMappings() |
applyConversions, convertAll, convertFields, convertIndexes, convertType, handleConversionError, initializeConversions, reverseConversions, toDataProcessingException
final java.lang.Class<T> beanClass
protected final java.util.Set<FieldMapping> parsedFields
private int lastFieldIndexMapped
private FieldMapping[] readOrder
private FieldMapping[] missing
private java.lang.Object[] valuesForMissing
protected boolean initialized
boolean strictHeaderValidationEnabled
private java.lang.String[] syntheticHeaders
private java.lang.Object[] row
private java.util.Map<FieldMapping,BeanConversionProcessor<?>> nestedAttributes
private final HeaderTransformer transformer
private final MethodFilter methodFilter
@Deprecated public BeanConversionProcessor(java.lang.Class<T> beanType)
BeanConversionProcessor(Class, MethodFilter)
constructor instead.beanType
- the class annotated with one or more of the annotations provided in com.univocity.parsers.annotations
.public BeanConversionProcessor(java.lang.Class<T> beanType, MethodFilter methodFilter)
beanType
- the class annotated with one or more of the annotations provided in com.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 with Parsed
, when both methods target the same field.BeanConversionProcessor(java.lang.Class<T> beanType, HeaderTransformer transformer, MethodFilter methodFilter)
public boolean isStrictHeaderValidationEnabled()
public final void initialize()
Parsed
annotationprotected final void initialize(java.lang.String[] headers)
Parsed
annotationheaders
- headers parsed from the input.public void setStrictHeaderValidationEnabled(boolean strictHeaderValidationEnabled)
strictHeaderValidationEnabled
- flag indicating whether strict validation of headers is enabled.void processField(java.lang.reflect.AnnotatedElement element, PropertyWrapper propertyDescriptor, java.lang.String[] headers)
java.util.Map<FieldMapping,BeanConversionProcessor<?>> getNestedAttributes()
BeanConversionProcessor<?> createNestedProcessor(java.lang.annotation.Annotation annotation, java.lang.Class nestedType, FieldMapping fieldMapping, HeaderTransformer transformer)
protected boolean processField(FieldMapping field)
field
- the field to be processedtrue
if the given field should be processed, otherwise false
.void validateMappings()
static java.lang.String describeField(java.lang.reflect.AnnotatedElement target)
private void setupConversions(java.lang.reflect.AnnotatedElement target, FieldMapping mapping)
Parsed
and extracts the sequence of Conversion
elements associated with each one.target
- the field and method annotated with Parsed
that must be associated with one or more Conversion
objectsmapping
- a helper class to store information how the field or method is mapped to a parsed record.private boolean applyDefaultConversion(Conversion lastConversionApplied, Conversion defaultConversion)
private java.lang.reflect.Method getConversionMethod(Conversion conversion, java.lang.String methodName)
protected void addConversion(Conversion conversion, FieldMapping mapping)
conversion
- The conversion object that must be executed against the given fieldmapping
- the helper object that contains information about how a field is mapped.void mapValuesToFields(T instance, java.lang.Object[] row, Context context)
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.private void mapFieldIndexes(Context context, java.lang.Object[] row, java.lang.String[] headers, int[] indexes, boolean columnsReordered)
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 in CommonSettings.getHeaders()
indexes
- 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
- Indicates the indexes provided were reordered and do not match the original sequence of headers.private void initializeValuesForMissing()
public T createBean(java.lang.String[] row, Context context)
row
- The values extracted from the parsercontext
- The current state of the parsing processvoid processNestedAttributes(java.lang.String[] row, java.lang.Object instance, Context context)
private void mapFieldsToValues(T instance, java.lang.Object[] row, java.lang.String[] headers, int[] indexes, boolean columnsReordered)
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 in CommonSettings.getHeaders()
indexes
- 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
- Indicates the indexes provided were reordered and do not match the original sequence of headers.public final java.lang.Object[] reverseConversions(T bean, java.lang.String[] headers, int[] indexesToWrite)
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 in CommonSettings.getHeaders()
indexesToWrite
- 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...)
public java.lang.Class<T> getBeanClass()