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 for Processor and RowWriterProcessor implementations that support java beans annotated with the annotations provided in com.univocity.parsers.annotations.
Author:
Univocity Software Pty Ltd - parsers@univocity.com
See Also:
  • Field Details

    • parsedFields

      protected final Set<FieldMapping> parsedFields
    • initialized

      protected boolean initialized
    • transformer

      protected final HeaderTransformer transformer
    • methodFilter

      protected final MethodFilter methodFilter
  • Constructor Details

    • BeanConversionProcessor

      @Deprecated public BeanConversionProcessor(Class<T> beanType)
      Deprecated.
      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 in com.univocity.parsers.annotations.
    • BeanConversionProcessor

      public BeanConversionProcessor(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 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.
  • Method Details

    • 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 the Parsed 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 with Parsed nor Nested. Any mappings defined with the column mapper will take precedence over these annotations.
      Returns:
      the column mapper
    • initialize

      protected final void initialize(String[] headers)
      Identifies and extracts fields annotated with the Parsed annotation
      Parameters:
      headers - headers parsed from the input.
    • initialize

      protected final void initialize(NormalizedString[] headers)
      Identifies and extracts fields annotated with the Parsed annotation
      Parameters:
      headers - headers parsed from the input.
    • 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.
    • 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
    • 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, otherwise false.
    • 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 field
      mapping - the helper object that contains information about how a field is mapped.
    • createBean

      public T createBean(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 parser
      context - The current state of the parsing process
      Returns:
      an instance of the java bean type defined in this class constructor.
    • reverseConversions

      public final 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 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...)
      Returns:
      a row of objects containing the values extracted from the java bean
    • getBeanClass

      public 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