Class Util
-
Method Summary
Modifier and TypeMethodDescriptionstatic TypeToken<?>
findErasureDifference
(TypeToken<?> genType, Collection<? extends TypeToken<?>> candidates) Check whether a generic type and its erased equivalent match the same set of candidate types.static PermazenField
findField
(PermazenClass<?> pclass, String fieldName) Identify the named field in the givenPermazenClass
.static PermazenField
findField
(PermazenClass<?> pclass, String fieldName, Boolean expectSubField) Identify the named field in the givenPermazenClass
.static PermazenSimpleField
findSimpleField
(PermazenClass<?> pclass, String fieldName) Identify the named simple field in the givenPermazenClass
.static <A extends Annotation>
AgetAnnotation
(AnnotatedElement element, Class<A> annotationType) Find the annotation on the given element.static Type
getTypeParameter
(Type type, int index) Get the n'th generic type parameter.static boolean
Determine whether the given object has any JSR 303 annotation(s) defining validation constraints in the default group.static AnnotatedElement
hasValidation
(Class<?> type) Determine if any JSR 303 validation annotations are present on the given type itself or any of its methods (public methods only).static boolean
Determine whether the given object has any JSR 303 annotation(s).static Object
Invoke method via reflection and re-throw any checked exception wrapped in anPermazenException
.static boolean
isAnyGroupBeingValidated
(Class<?>[] constraintGroups, Class<?>[] validationGroups) Determine if a constraint whosegroups()
contain the given constraint groups should be applied when validating with the given validation groups.static boolean
isGroupBeingValidated
(Class<?> constraintGroup, Class<?>[] validationGroups) Determine if a constraint whosegroups()
contain the given constraint group should be applied when validating with the given validation groups.static boolean
requiresDefaultValidation
(Class<?> type) Determine if instances of the given type require any validation under the default validation group.static boolean
requiresDefaultValidation
(Method method) Determine if the given getter method, or any method it overrides, has a JSR 303 validation constraint applicable under the default validation group.static <A,
B> Converter<B, A> Return the reverse of the givenConverter
, treating a null converter as if it were the identity.static <T> Stream<T>
streamOf
(T obj) Substitute forStream.of(Object)
.
-
Method Details
-
getAnnotation
public static <A extends Annotation> A getAnnotation(AnnotatedElement element, Class<A> annotationType) Find the annotation on the given element.If
spring-core
is available on the classpath, the implementation inAnnotationScanner
utilizes Spring'sAnnotatedElementUtils.getMergedAnnotation(AnnotatedElement, Class)
method to find annotations that are either present or meta-present on the element, and includes support for annotation attribute overrides; otherwise, it just invokesAnnotatedElement.getAnnotation(Class)
.- Type Parameters:
A
- annotation type- Parameters:
element
- element with annotation or meta-present annotationannotationType
- type of the annotation to find- Returns:
- the annotation found, or null if not found
-
hasValidation
Determine if any JSR 303 validation annotations are present on the given type itself or any of its methods (public methods only).- Parameters:
type
- object type- Returns:
- a non-null object with JSR 303 validation requirements, or null if none found
- Throws:
IllegalArgumentException
- iftype
is null
-
requiresDefaultValidation
Determine if instances of the given type require any validation under the default validation group.This will be true if
type
or any of its declared methods has a JSR 303 (public methods only) or@OnValidate
annotation, or if any of its super-types requires validation.- Parameters:
type
- object type- Returns:
- true if
type
has any validation requirements - Throws:
IllegalArgumentException
- iftype
is null- See Also:
-
requiresDefaultValidation
Determine if the given getter method, or any method it overrides, has a JSR 303 validation constraint applicable under the default validation group.- Parameters:
method
- annotated method- Returns:
- true if
obj
has one or more JSR 303 annotations - Throws:
IllegalArgumentException
- ifmethod
is null
-
hasDefaultValidationAnnotation
Determine whether the given object has any JSR 303 annotation(s) defining validation constraints in the default group.- Parameters:
obj
- annotated element- Returns:
- true if
obj
has one or more JSR 303 default validation constraint annotations - Throws:
IllegalArgumentException
- ifobj
is null
-
hasValidationAnnotation
Determine whether the given object has any JSR 303 annotation(s).- Parameters:
obj
- annotated element- Returns:
- true if
obj
has one or more JSR 303 validation constraint annotations - Throws:
IllegalArgumentException
- ifobj
is null
-
isGroupBeingValidated
Determine if a constraint whosegroups()
contain the given constraint group should be applied when validating with the given validation groups.- Parameters:
constraintGroup
- validation group associated with a validation constraintvalidationGroups
- groups for which validation is being performed- Returns:
- whether to apply the validation constraint
- Throws:
IllegalArgumentException
- if any null values are encountered
-
isAnyGroupBeingValidated
public static boolean isAnyGroupBeingValidated(Class<?>[] constraintGroups, Class<?>[] validationGroups) Determine if a constraint whosegroups()
contain the given constraint groups should be applied when validating with the given validation groups.- Parameters:
constraintGroups
- validation groups associated with a validation constraintvalidationGroups
- groups for which validation is being performed- Returns:
- whether to apply the validation constraint
- Throws:
IllegalArgumentException
- if any null values are encountered
-
findField
Identify the named field in the givenPermazenClass
.The field may be specified by name like
"myfield"
or by name and storage ID like"myfield#1234"
.To specify a sub-field of a complex field, qualify it with the parent field like
"mymap.key"
.This method is equivalent to
findField(pclass, fieldName, null)
.- Parameters:
pclass
- containing object typefieldName
- field name- Returns:
- resulting
PermazenField
, or null if no such field is found inpclass
- Throws:
IllegalArgumentException
- iffieldName
is ambiguous or invalidIllegalArgumentException
- ifpclass
orfieldName
is null
-
findSimpleField
Identify the named simple field in the givenPermazenClass
.The field may be specified by name like
"myfield"
or by name and storage ID like"myfield#1234"
.To specify a sub-field of a complex field, qualify it with the parent field like
"mymap.key"
.This method is equivalent to
findField(pclass, fieldName, true)
plus filtering out non-PermazenSimpleField
s.- Parameters:
pclass
- containing object typefieldName
- field name- Returns:
- resulting
PermazenField
, or null if no such field is found inpclass
- Throws:
IllegalArgumentException
- iffieldName
is ambiguous or invalidIllegalArgumentException
- ifpclass
orfieldName
is null
-
findField
public static PermazenField findField(PermazenClass<?> pclass, String fieldName, Boolean expectSubField) Identify the named field in the givenPermazenClass
.The field may be specified by name like
"myfield"
or by name and storage ID like"myfield#1234"
.To specify a sub-field of a complex field, qualify it with the parent field like
"mymap.key"
.The
expectSubField
parameter controls what happens when a complex field is matched. If true, then either a sub-field must be specified, or else the complex field must have only one sub-field and then that sub-field is assumed. If false, it is an error to specify a sub-field of a complex field. If null, either is OK.- Parameters:
pclass
- containing object typefieldName
- field nameexpectSubField
- true if the field should be a complex sub-field instead of a complex field, false if field should not be complex field instead of a complex sub-field, or null for don't care- Returns:
- resulting
PermazenField
, or null if no such field is found inpclass
- Throws:
IllegalArgumentException
- iffieldName
is ambiguous or invalidIllegalArgumentException
- ifpclass
orfieldName
is null
-
findErasureDifference
public static TypeToken<?> findErasureDifference(TypeToken<?> genType, Collection<? extends TypeToken<?>> candidates) Check whether a generic type and its erased equivalent match the same set of candidate types.- Parameters:
genType
- parameter generic typecandidates
- possible candidates for parameter- Returns:
- mismatching candidate, or null if generic and erased match the same candidates
-
streamOf
Substitute forStream.of(Object)
.Permazen needs this method because the v1.6 class files we generate don't support invoking static methods on interfaces.
-
getTypeParameter
Get the n'th generic type parameter.- Parameters:
type
- parameterized generic typeindex
- type parameter index (zero based)- Returns:
- type parameter at
index
- Throws:
IllegalArgumentException
- iftype
is not a parameterized type with more thanindex
type variables
-
invoke
Invoke method via reflection and re-throw any checked exception wrapped in anPermazenException
.- Parameters:
method
- method to invoketarget
- instance, or null if method is staticparams
- method parameters- Returns:
- method return value
- Throws:
PermazenException
- if an error occurs
-
reverse
Return the reverse of the givenConverter
, treating a null converter as if it were the identity.- Parameters:
converter
- original converter, or null for the identity conversion- Returns:
- reversed converter
-