Class AnnotationHelper
java.lang.Object
com.univocity.parsers.annotations.helpers.AnnotationHelper
Helper class to process fields annotated with
Parsed-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static Annotationprivate static Class<? extends Annotation> private static AnnotatedElement -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanallFieldsIndexBasedForParsing(Class<?> beanClass) Runs through all annotations of a given class to identify whether all annotated fields and methods (with theParsedannotation) are mapped to a column by index.static booleanallFieldsIndexBasedForWriting(Class<?> beanClass) Runs through all annotations of a given class to identify whether all annotated fields and methods (with theParsedannotation) are mapped to a column by index.private static booleanallFieldsIndexOrNameBased(boolean searchName, Class<?> beanClass, MethodFilter filter) static booleanallFieldsNameBasedForParsing(Class<?> beanClass) Runs through all annotations of a given class to identify whether all annotated fields and methods (with theParsedannotation) are mapped to a column by name.static booleanallFieldsNameBasedForWriting(Class<?> beanClass) Runs through all annotations of a given class to identify whether all annotated fields and methods (with theParsedannotation) are mapped to a column by name.static voidapplyFormatSettings(Object formatter, String[] propertiesAndValues) Applied the configuration of a formatter object (SimpleDateFormat,NumberFormatand others).static EnumConversioncreateDefaultEnumConversion(Class fieldType, String nullRead, String nullWrite) static String[]deriveHeaderNamesFromFields(Class<?> beanClass, MethodFilter filter) Runs through allParsedannotations of a given class to identify all header names associated with its fields(package private) static StringdescribeElement(AnnotatedElement element) private static LocaleextractLocale(String[] options) private static StringextractOption(String[] options, String key) private static TimeZoneextractTimeZone(String[] options) static List<Annotation> findAllAnnotationsInPackage(AnnotatedElement annotatedElement, Package aPackage) Returns all annotations applied to an element, excluding the ones not in a given package.private static voidfindAllAnnotationsInPackage(AnnotatedElement annotatedElement, Package aPackage, ArrayList<? super Annotation> found, Set<Annotation> visited) static <A extends Annotation>
AfindAnnotation(AnnotatedElement annotatedElement, Class<A> annotationType) Searches for an annotation of a given type that's been applied to an element either directly (as a regular annotation) or indirectly (as a meta-annotations, i.e.private static <A> AfindAnnotation(AnnotatedElement annotatedElement, Class<A> annotationType, Set<Annotation> visited, Stack<Annotation> path) static <T extends Annotation>
TfindAnnotationInClass(Class<?> beanClass, Class<T> annotation) Searches for a given annotation in the hierarchy of a classprivate static Class<?> findAnnotationMethodType(Class<? extends Annotation> type, String methodName) static HeadersfindHeadersAnnotation(Class<?> beanClass) Searches for theHeadersannotation in the hierarchy of a classstatic Map<Field, PropertyWrapper> getAllFields(Class<?> beanClass) Returns all fields available from a given class.getAllMethods(Class<?> beanClass, MethodFilter filter) Returns all methods available from a given classgetAnnotatedFields(Class<?> beanClass) Returns all attributes available from a given class that have an annotation.static <A extends Annotation>
List<Field> getAnnotatedFields(Class<?> beanClass, Class<A> annotationType) Returns all attributes available from a given class that have an annotation.getAnnotatedMethods(Class<?> beanClass, MethodFilter filter) Returns all methods available from a given class that have an annotation.static <A extends Annotation>
List<Method> getAnnotatedMethods(Class<?> beanClass, MethodFilter filter, Class<A> annotationType) Returns all methods available from a given class that have a specific annotation.static ConversiongetConversion(Class classType, Annotation annotation) Identifies the proper conversion for a given type and an annotation from the packagecom.univocity.parsers.annotationsstatic ConversiongetConversion(Class fieldType, AnnotatedElement target, Annotation annotation, String nullRead, String nullWrite) static Class<?> getDeclaringClass(AnnotatedElement element) static ConversiongetDefaultConversion(Class fieldType, AnnotatedElement target, Parsed parsed) Identifies the proper conversion for a given typestatic ConversiongetDefaultConversion(AnnotatedElement target) Returns the defaultConversionthat should be applied to the field based on its type.static final ObjectReturns Java's default value for a given type, in a primitive type wrapper.static List<TransformedHeader> getFieldSequence(Class beanClass, boolean processNested, HeaderTransformer transformer, MethodFilter filter) Returns a list of fields withParsedannotations in the sequence they should be processed for parsing or writing.private static List<TransformedHeader> getFieldSequence(Class beanClass, boolean processNested, List<Integer> indexes, HeaderTransformer transformer, MethodFilter filter) static StringgetName(AnnotatedElement element) static StringgetNullReadValue(AnnotatedElement target, Parsed parsed) private static StringgetNullValue(String defaultValue) Converts the special "null" strings that might be provided byand Parsed#defaultNullWrite()static StringgetNullWriteValue(AnnotatedElement target, Parsed parsed) static Integer[]getSelectedIndexes(Class<?> beanClass, MethodFilter filter) Runs through allParsedannotations of a given class to identify all indexes associated with its fieldsstatic Class<?> getType(AnnotatedElement element) private static Objectinvoke(Annotation annotation, Method method) private static voidinvokeSetter(Object formatter, PropertyWrapper property, String value) private static booleanisCustomAnnotation(Annotation annotation) static <T> TnewInstance(Class parent, Class<T> type, String[] args) private static voidprocessAnnotations(AnnotatedElement element, boolean processNested, List<Integer> indexes, List<TransformedHeader> tmp, Map<AnnotatedElement, List<TransformedHeader>> nestedReplacements, HeaderTransformer transformer, MethodFilter filter)
-
Field Details
-
lastProcessedElement
-
lastProcessedAnnotationType
-
lastAnnotationFound
-
javaLangAnnotationTypes
-
customAnnotationTypes
-
-
Constructor Details
-
AnnotationHelper
private AnnotationHelper()
-
-
Method Details
-
getNullValue
Converts the special "null" strings that might be provided byand Parsed#defaultNullWrite()- Parameters:
defaultValue- The string returned byand Parsed#defaultNullWrite()- Returns:
- the default value if it is not the String literal "null" or "'null'".
If "null" was provided, then null will be returned.
If "'null'" was provided, then "null" will be returned.
-
getNullWriteValue
-
getNullReadValue
-
getConversion
Identifies the proper conversion for a given type and an annotation from the packagecom.univocity.parsers.annotations- Parameters:
classType- the type to have conversions applied toannotation- the annotation fromcom.univocity.parsers.annotationsthat identifies aConversioninstance.- Returns:
- The
Conversionthat should be applied to the type
-
createDefaultEnumConversion
public static EnumConversion createDefaultEnumConversion(Class fieldType, String nullRead, String nullWrite) -
getConversion
public static Conversion getConversion(Class fieldType, AnnotatedElement target, Annotation annotation, String nullRead, String nullWrite) -
extractOption
-
extractTimeZone
-
extractLocale
-
newInstance
-
getDefaultConversion
public static Conversion getDefaultConversion(Class fieldType, AnnotatedElement target, Parsed parsed) Identifies the proper conversion for a given type- Parameters:
fieldType- The type of field to have conversions applied to.target- the annotated attribute of method that has the annotationparsed- theParsedannotation fromcom.univocity.parsers.annotations.- Returns:
- The
Conversionthat should be applied to the field type
-
getDefaultConversion
Returns the defaultConversionthat should be applied to the field based on its type.- Parameters:
target- The field or method whose values must be converted from a given parsed String.- Returns:
- The default
Conversionapplied to the given field.
-
applyFormatSettings
Applied the configuration of a formatter object (SimpleDateFormat,NumberFormatand others).- Parameters:
formatter- the formatter instancepropertiesAndValues- a sequence of key-value pairs, where the key is a property of the formatter object to be set to the following value via reflection
-
invokeSetter
-
allFieldsIndexOrNameBased
private static boolean allFieldsIndexOrNameBased(boolean searchName, Class<?> beanClass, MethodFilter filter) -
allFieldsIndexBasedForParsing
Runs through all annotations of a given class to identify whether all annotated fields and methods (with theParsedannotation) are mapped to a column by index. -
allFieldsNameBasedForParsing
Runs through all annotations of a given class to identify whether all annotated fields and methods (with theParsedannotation) are mapped to a column by name. -
allFieldsIndexBasedForWriting
Runs through all annotations of a given class to identify whether all annotated fields and methods (with theParsedannotation) are mapped to a column by index. -
allFieldsNameBasedForWriting
Runs through all annotations of a given class to identify whether all annotated fields and methods (with theParsedannotation) are mapped to a column by name. -
getSelectedIndexes
Runs through allParsedannotations of a given class to identify all indexes associated with its fields- Parameters:
beanClass- a class whoseParsedannotations will be processed.filter- filter to apply over annotated methods when the class is being used for reading data from beans (to write values to an output) or when writing values into beans (while parsing). It is used to choose either a "get" or a "set" method annotated withParsed, when both methods target the same field.- Returns:
- an array of column indexes used by the given class
-
deriveHeaderNamesFromFields
Runs through allParsedannotations of a given class to identify all header names associated with its fields- Parameters:
beanClass- a class whoseParsedannotations will be processed.filter- a filter to exclude annotated methods that won't be used for parsing or writing- Returns:
- an array of column names used by the given class
-
findAnnotationInClass
public static <T extends Annotation> T findAnnotationInClass(Class<?> beanClass, Class<T> annotation) Searches for a given annotation in the hierarchy of a class- Type Parameters:
T- the type of the annotation to return- Parameters:
beanClass- the class whose hierarchy will be searchedannotation- the annotation to locate- Returns:
- the annotation of the given class or its most immediate parent, or
nullif not found.
-
findHeadersAnnotation
Searches for theHeadersannotation in the hierarchy of a class- Parameters:
beanClass- the class whose hierarchy will be searched- Returns:
- the
Headersannotation of the given class or its most immediate parent, ornullif not found.
-
getType
-
getDeclaringClass
-
getName
-
describeElement
-
processAnnotations
private static void processAnnotations(AnnotatedElement element, boolean processNested, List<Integer> indexes, List<TransformedHeader> tmp, Map<AnnotatedElement, List<TransformedHeader>> nestedReplacements, HeaderTransformer transformer, MethodFilter filter) -
getFieldSequence
public static List<TransformedHeader> getFieldSequence(Class beanClass, boolean processNested, HeaderTransformer transformer, MethodFilter filter) Returns a list of fields withParsedannotations in the sequence they should be processed for parsing or writing. The sequence is ordered taking into account their original order in the annotated class, unlessParsed.index()is set to a non-negative number.- Parameters:
beanClass- the class whose field sequence will be returned.processNested- flag indicating whetherNestedannotations should be processedtransformer- aHeaderTransformerinstance to be used for transforming headers of a givenNestedattribute.filter- filter to apply over annotated methods when the class is being used for reading data from beans (to write values to an output) or when writing values into beans (while parsing). It is used to choose either a "get" or a "set" method annotated withParsed, when both methods target the same field.- Returns:
- a list of fields ordered by their processing sequence
-
getFieldSequence
private static List<TransformedHeader> getFieldSequence(Class beanClass, boolean processNested, List<Integer> indexes, HeaderTransformer transformer, MethodFilter filter) -
getAllFields
Returns all fields available from a given class.- Parameters:
beanClass- a class whose fields will be returned.- Returns:
- a map of
Fieldand the correspondingPropertyWrapper
-
getAnnotatedMethods
public static <A extends Annotation> List<Method> getAnnotatedMethods(Class<?> beanClass, MethodFilter filter, Class<A> annotationType) Returns all methods available from a given class that have a specific annotation.- Type Parameters:
A- the expected annotation type- Parameters:
beanClass- a class whose methods will be returned.filter- filter to apply over annotated methods when the class is being used for reading data from beans (to write values to an output) or when writing values into beans (while parsing). It is used to choose either a "get" or a "set" method annotated withParsed, when both methods target the same field.annotationType- the annotation type to consider- Returns:
- a list of
Methodwith at least one annotation
-
getAllMethods
Returns all methods available from a given class- Parameters:
beanClass- a class whose methods will be returned.filter- filter to apply over annotated methods when the class is being used for reading data from beans (to write values to an output) or when writing values into beans (while parsing). It is used to choose either a "get" or a "set" method annotated withParsed, when both methods target the same field.- Returns:
- a list of
Methods that conform to the given filter.
-
getAnnotatedMethods
Returns all methods available from a given class that have an annotation.- Parameters:
beanClass- a class whose methods will be returned.filter- filter to apply over annotated methods when the class is being used for reading data from beans (to write values to an output) or when writing values into beans (while parsing). It is used to choose either a "get" or a "set" method annotated withParsed, when both methods target the same field.- Returns:
- a list of
Methodwith at least one annotation
-
getAnnotatedFields
Returns all attributes available from a given class that have an annotation.- Parameters:
beanClass- a class whose methods will be returned.- Returns:
- a list of
Fieldwith at least one annotation
-
getAnnotatedFields
public static <A extends Annotation> List<Field> getAnnotatedFields(Class<?> beanClass, Class<A> annotationType) Returns all attributes available from a given class that have an annotation.- Type Parameters:
A- the expected annotation type- Parameters:
beanClass- a class whose methods will be returned.annotationType- the type of annotation to consider- Returns:
- a list of
Fieldwith at least one annotation
-
findAnnotation
public static <A extends Annotation> A findAnnotation(AnnotatedElement annotatedElement, Class<A> annotationType) Searches for an annotation of a given type that's been applied to an element either directly (as a regular annotation) or indirectly (as a meta-annotations, i.e. an annotation that has annotations).- Type Parameters:
A- the type of the annotation being searched for- Parameters:
annotatedElement- the element whose annotations will be searchedannotationType- the type of annotation to search for- Returns:
- the annotation associated with the given element, or
nullif not found.
-
findAnnotationMethodType
private static Class<?> findAnnotationMethodType(Class<? extends Annotation> type, String methodName) -
invoke
-
findAnnotation
private static <A> A findAnnotation(AnnotatedElement annotatedElement, Class<A> annotationType, Set<Annotation> visited, Stack<Annotation> path) -
isCustomAnnotation
-
findAllAnnotationsInPackage
public static List<Annotation> findAllAnnotationsInPackage(AnnotatedElement annotatedElement, Package aPackage) Returns all annotations applied to an element, excluding the ones not in a given package.- Parameters:
annotatedElement- the element (method, field, etc) whose annotations will be extractedaPackage- the package of the annotations that should be returned- Returns:
- the list of annotation elements applied to the given element, that are also members of the given package.
-
findAllAnnotationsInPackage
private static void findAllAnnotationsInPackage(AnnotatedElement annotatedElement, Package aPackage, ArrayList<? super Annotation> found, Set<Annotation> visited) -
getDefaultPrimitiveValue
Returns Java's default value for a given type, in a primitive type wrapper.- Parameters:
type- the primitive type whose default value will be returned.- Returns:
- the default value for the given primitive type, or
nullif the type is not primitive.
-