Class AnnotationHelper
- java.lang.Object
-
- com.univocity.parsers.annotations.helpers.AnnotationHelper
-
public class AnnotationHelper extends java.lang.ObjectHelper class to process fields annotated withParsed
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classAnnotationHelper.NO_ANNOTATIONS
-
Field Summary
Fields Modifier and Type Field Description private static java.util.Set<java.lang.Class>customAnnotationTypesprivate static java.util.Set<java.lang.Class>javaLangAnnotationTypesprivate static java.lang.annotation.AnnotationlastAnnotationFoundprivate static java.lang.Class<? extends java.lang.annotation.Annotation>lastProcessedAnnotationTypeprivate static java.lang.reflect.AnnotatedElementlastProcessedElement
-
Constructor Summary
Constructors Modifier Constructor Description privateAnnotationHelper()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static booleanallFieldsIndexBasedForParsing(java.lang.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(java.lang.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, java.lang.Class<?> beanClass, MethodFilter filter)static booleanallFieldsNameBasedForParsing(java.lang.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(java.lang.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(java.lang.Object formatter, java.lang.String[] propertiesAndValues)Applied the configuration of a formatter object (SimpleDateFormat,NumberFormatand others).static java.lang.String[]deriveHeaderNamesFromFields(java.lang.Class<?> beanClass, MethodFilter filter)Runs through allParsedannotations of a given class to identify all header names associated with its fields(package private) static java.lang.StringdescribeElement(java.lang.reflect.AnnotatedElement element)private static java.util.LocaleextractLocale(java.lang.String[] options)private static java.lang.StringextractOption(java.lang.String[] options, java.lang.String key)private static java.util.TimeZoneextractTimeZone(java.lang.String[] options)static java.util.List<java.lang.annotation.Annotation>findAllAnnotationsInPackage(java.lang.reflect.AnnotatedElement annotatedElement, java.lang.Package aPackage)Returns all annotations applied to an element, excluding the ones not in a given package.private static voidfindAllAnnotationsInPackage(java.lang.reflect.AnnotatedElement annotatedElement, java.lang.Package aPackage, java.util.ArrayList<? super java.lang.annotation.Annotation> found, java.util.Set<java.lang.annotation.Annotation> visited)static <A extends java.lang.annotation.Annotation>
AfindAnnotation(java.lang.reflect.AnnotatedElement annotatedElement, java.lang.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(java.lang.reflect.AnnotatedElement annotatedElement, java.lang.Class<A> annotationType, java.util.Set<java.lang.annotation.Annotation> visited, java.util.Stack<java.lang.annotation.Annotation> path)static <T extends java.lang.annotation.Annotation>
TfindAnnotationInClass(java.lang.Class<?> beanClass, java.lang.Class<T> annotation)Searches for a given annotation in the hierarchy of a classprivate static java.lang.Class<?>findAnnotationMethodType(java.lang.Class<? extends java.lang.annotation.Annotation> type, java.lang.String methodName)static HeadersfindHeadersAnnotation(java.lang.Class<?> beanClass)Searches for theHeadersannotation in the hierarchy of a classstatic java.util.Map<java.lang.reflect.Field,PropertyWrapper>getAllFields(java.lang.Class<?> beanClass)Returns all fields available from a given class.static java.util.List<java.lang.reflect.Method>getAllMethods(java.lang.Class<?> beanClass, MethodFilter filter)Returns all methods available from a given classstatic java.util.List<java.lang.reflect.Field>getAnnotatedFields(java.lang.Class<?> beanClass)Returns all attributes available from a given class that have an annotation.static <A extends java.lang.annotation.Annotation>
java.util.List<java.lang.reflect.Field>getAnnotatedFields(java.lang.Class<?> beanClass, java.lang.Class<A> annotationType)Returns all attributes available from a given class that have an annotation.static java.util.List<java.lang.reflect.Method>getAnnotatedMethods(java.lang.Class<?> beanClass, MethodFilter filter)Returns all methods available from a given class that have an annotation.static <A extends java.lang.annotation.Annotation>
java.util.List<java.lang.reflect.Method>getAnnotatedMethods(java.lang.Class<?> beanClass, MethodFilter filter, java.lang.Class<A> annotationType)Returns all methods available from a given class that have a specific annotation.static ConversiongetConversion(java.lang.Class classType, java.lang.annotation.Annotation annotation)Identifies the proper conversion for a given type and an annotation from the packagecom.univocity.parsers.annotationsprivate static ConversiongetConversion(java.lang.Class fieldType, java.lang.reflect.AnnotatedElement target, java.lang.annotation.Annotation annotation)static ConversiongetConversion(java.lang.reflect.AnnotatedElement target, java.lang.annotation.Annotation annotation)Identifies the proper conversion for a given Field and an annotation from the packagecom.univocity.parsers.annotationsstatic java.lang.Class<?>getDeclaringClass(java.lang.reflect.AnnotatedElement element)static ConversiongetDefaultConversion(java.lang.Class fieldType, java.lang.reflect.AnnotatedElement target, Parsed parsed)Identifies the proper conversion for a given typestatic ConversiongetDefaultConversion(java.lang.reflect.AnnotatedElement target)Returns the defaultConversionthat should be applied to the field based on its type.static java.lang.ObjectgetDefaultPrimitiveValue(java.lang.Class type)Returns Java's default value for a given type, in a primitive type wrapper.static java.util.List<TransformedHeader>getFieldSequence(java.lang.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 java.util.List<TransformedHeader>getFieldSequence(java.lang.Class beanClass, boolean processNested, java.util.List<java.lang.Integer> indexes, HeaderTransformer transformer, MethodFilter filter)static java.lang.StringgetName(java.lang.reflect.AnnotatedElement element)private static java.lang.StringgetNullReadValue(java.lang.reflect.AnnotatedElement target, Parsed parsed)private static java.lang.StringgetNullValue(java.lang.String defaultValue)Converts the special "null" strings that might be provided byand Parsed#defaultNullWrite()private static java.lang.StringgetNullWriteValue(java.lang.reflect.AnnotatedElement target, Parsed parsed)static java.lang.Integer[]getSelectedIndexes(java.lang.Class<?> beanClass, MethodFilter filter)Runs through allParsedannotations of a given class to identify all indexes associated with its fieldsstatic java.lang.Class<?>getType(java.lang.reflect.AnnotatedElement element)private static java.lang.Objectinvoke(java.lang.annotation.Annotation annotation, java.lang.reflect.Method method)private static voidinvokeSetter(java.lang.Object formatter, PropertyWrapper property, java.lang.String value)private static booleanisCustomAnnotation(java.lang.annotation.Annotation annotation)static <T> TnewInstance(java.lang.Class parent, java.lang.Class<T> type, java.lang.String[] args)private static voidprocessAnnotations(java.lang.reflect.AnnotatedElement element, boolean processNested, java.util.List<java.lang.Integer> indexes, java.util.List<TransformedHeader> tmp, java.util.Map<java.lang.reflect.AnnotatedElement,java.util.List<TransformedHeader>> nestedReplacements, HeaderTransformer transformer, MethodFilter filter)
-
-
-
Field Detail
-
lastProcessedElement
private static java.lang.reflect.AnnotatedElement lastProcessedElement
-
lastProcessedAnnotationType
private static java.lang.Class<? extends java.lang.annotation.Annotation> lastProcessedAnnotationType
-
lastAnnotationFound
private static java.lang.annotation.Annotation lastAnnotationFound
-
javaLangAnnotationTypes
private static final java.util.Set<java.lang.Class> javaLangAnnotationTypes
-
customAnnotationTypes
private static final java.util.Set<java.lang.Class> customAnnotationTypes
-
-
Method Detail
-
getNullValue
private static java.lang.String getNullValue(java.lang.String defaultValue)
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
private static java.lang.String getNullWriteValue(java.lang.reflect.AnnotatedElement target, Parsed parsed)
-
getNullReadValue
private static java.lang.String getNullReadValue(java.lang.reflect.AnnotatedElement target, Parsed parsed)
-
getConversion
public static Conversion getConversion(java.lang.reflect.AnnotatedElement target, java.lang.annotation.Annotation annotation)
Identifies the proper conversion for a given Field and an annotation from the packagecom.univocity.parsers.annotations- Parameters:
target- The field or method to have conversions applied toannotation- the annotation fromcom.univocity.parsers.annotationsthat identifies aConversioninstance.- Returns:
- The
Conversionthat should be applied to the field
-
getConversion
public static Conversion getConversion(java.lang.Class classType, java.lang.annotation.Annotation annotation)
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
-
getConversion
private static Conversion getConversion(java.lang.Class fieldType, java.lang.reflect.AnnotatedElement target, java.lang.annotation.Annotation annotation)
-
extractOption
private static java.lang.String extractOption(java.lang.String[] options, java.lang.String key)
-
extractTimeZone
private static java.util.TimeZone extractTimeZone(java.lang.String[] options)
-
extractLocale
private static java.util.Locale extractLocale(java.lang.String[] options)
-
newInstance
public static <T> T newInstance(java.lang.Class parent, java.lang.Class<T> type, java.lang.String[] args)
-
getDefaultConversion
public static Conversion getDefaultConversion(java.lang.Class fieldType, java.lang.reflect.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
public static Conversion getDefaultConversion(java.lang.reflect.AnnotatedElement target)
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
public static void applyFormatSettings(java.lang.Object formatter, java.lang.String[] propertiesAndValues)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
private static void invokeSetter(java.lang.Object formatter, PropertyWrapper property, java.lang.String value)
-
allFieldsIndexOrNameBased
private static boolean allFieldsIndexOrNameBased(boolean searchName, java.lang.Class<?> beanClass, MethodFilter filter)
-
allFieldsIndexBasedForParsing
public static boolean allFieldsIndexBasedForParsing(java.lang.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.
-
allFieldsNameBasedForParsing
public static boolean allFieldsNameBasedForParsing(java.lang.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.
-
allFieldsIndexBasedForWriting
public static boolean allFieldsIndexBasedForWriting(java.lang.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.
-
allFieldsNameBasedForWriting
public static boolean allFieldsNameBasedForWriting(java.lang.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.
-
getSelectedIndexes
public static java.lang.Integer[] getSelectedIndexes(java.lang.Class<?> beanClass, MethodFilter filter)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
public static java.lang.String[] deriveHeaderNamesFromFields(java.lang.Class<?> beanClass, MethodFilter filter)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 java.lang.annotation.Annotation> T findAnnotationInClass(java.lang.Class<?> beanClass, java.lang.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
public static Headers findHeadersAnnotation(java.lang.Class<?> beanClass)
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
public static java.lang.Class<?> getType(java.lang.reflect.AnnotatedElement element)
-
getDeclaringClass
public static java.lang.Class<?> getDeclaringClass(java.lang.reflect.AnnotatedElement element)
-
getName
public static java.lang.String getName(java.lang.reflect.AnnotatedElement element)
-
describeElement
static java.lang.String describeElement(java.lang.reflect.AnnotatedElement element)
-
processAnnotations
private static void processAnnotations(java.lang.reflect.AnnotatedElement element, boolean processNested, java.util.List<java.lang.Integer> indexes, java.util.List<TransformedHeader> tmp, java.util.Map<java.lang.reflect.AnnotatedElement,java.util.List<TransformedHeader>> nestedReplacements, HeaderTransformer transformer, MethodFilter filter)
-
getFieldSequence
public static java.util.List<TransformedHeader> getFieldSequence(java.lang.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 java.util.List<TransformedHeader> getFieldSequence(java.lang.Class beanClass, boolean processNested, java.util.List<java.lang.Integer> indexes, HeaderTransformer transformer, MethodFilter filter)
-
getAllFields
public static java.util.Map<java.lang.reflect.Field,PropertyWrapper> getAllFields(java.lang.Class<?> beanClass)
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 java.lang.annotation.Annotation> java.util.List<java.lang.reflect.Method> getAnnotatedMethods(java.lang.Class<?> beanClass, MethodFilter filter, java.lang.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
public static java.util.List<java.lang.reflect.Method> getAllMethods(java.lang.Class<?> beanClass, MethodFilter filter)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
public static java.util.List<java.lang.reflect.Method> getAnnotatedMethods(java.lang.Class<?> beanClass, MethodFilter filter)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
public static java.util.List<java.lang.reflect.Field> getAnnotatedFields(java.lang.Class<?> beanClass)
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 java.lang.annotation.Annotation> java.util.List<java.lang.reflect.Field> getAnnotatedFields(java.lang.Class<?> beanClass, java.lang.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 java.lang.annotation.Annotation> A findAnnotation(java.lang.reflect.AnnotatedElement annotatedElement, java.lang.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 java.lang.Class<?> findAnnotationMethodType(java.lang.Class<? extends java.lang.annotation.Annotation> type, java.lang.String methodName)
-
invoke
private static java.lang.Object invoke(java.lang.annotation.Annotation annotation, java.lang.reflect.Method method)
-
findAnnotation
private static <A> A findAnnotation(java.lang.reflect.AnnotatedElement annotatedElement, java.lang.Class<A> annotationType, java.util.Set<java.lang.annotation.Annotation> visited, java.util.Stack<java.lang.annotation.Annotation> path)
-
isCustomAnnotation
private static boolean isCustomAnnotation(java.lang.annotation.Annotation annotation)
-
findAllAnnotationsInPackage
public static java.util.List<java.lang.annotation.Annotation> findAllAnnotationsInPackage(java.lang.reflect.AnnotatedElement annotatedElement, java.lang.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(java.lang.reflect.AnnotatedElement annotatedElement, java.lang.Package aPackage, java.util.ArrayList<? super java.lang.annotation.Annotation> found, java.util.Set<java.lang.annotation.Annotation> visited)
-
getDefaultPrimitiveValue
public static final java.lang.Object getDefaultPrimitiveValue(java.lang.Class type)
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.
-
-