Class Util
-
Method Summary
Modifier and TypeMethodDescriptionstatic 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 TypeToken<?>
findLowestCommonAncestor
(Stream<TypeToken<?>> types) Find the narrowest type that is a supertype of all of the given types.static TypeToken<?>
findLowestCommonAncestorOfClasses
(Stream<Class<?>> types) Find the narrowest type that is a supertype of all of the given types.findLowestCommonAncestors
(Stream<TypeToken<?>> types) Find the narrowest type(s) each of which is a supertype of all of the given types.findLowestCommonAncestorsOfClasses
(Stream<Class<?>> types) Find the narrowest type(s) each of which is a supertype of all of the given types.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 <T> TypeToken<? extends T>
getWildcardedType
(Class<T> type) Parameterize the raw type with wildcards.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 <T> TypeToken<? extends T>
newParameterizedType
(Class<T> target, Type[] params) Convert a raw class back into its generic type using caller-supplied type parameters.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
-
findLowestCommonAncestorOfClasses
Find the narrowest type that is a supertype of all of the given types.This method delegates to
findLowestCommonAncestor()
after converting theClass
instances toTypeToken
s.- Parameters:
types
- sub-types- Returns:
- narrowest common super-type
- Throws:
IllegalArgumentException
- iftypes
or any type intypes
is null- See Also:
-
findLowestCommonAncestorsOfClasses
Find the narrowest type(s) each of which is a supertype of all of the given types.This method delegates to
findLowestCommonAncestors()
after converting theClass
instances toTypeToken
s.- Parameters:
types
- sub-types- Returns:
- maximally narrow common supertype(s)
- Throws:
IllegalArgumentException
- if any type intypes
is null- See Also:
-
findLowestCommonAncestor
Find the narrowest type that is a supertype of all of the given types.Note that there may be more than one such type. The returned type will always be as narrow as possible, but it's possible there for there to be multiple such types for which none is a sub-type of any other.
When there is more than one choice, heuristics are used. For example, we prefer non-interface types, and
PermazenObject
over other interface types.- Parameters:
types
- sub-types- Returns:
- narrowest common super-type
-
findLowestCommonAncestors
Find the narrowest type(s) each of which is a supertype of all of the given types.- Parameters:
types
- sub-types- Returns:
- maximally narrow common supertype(s)
-
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.
-
getWildcardedType
Parameterize the raw type with wildcards.- Type Parameters:
T
- raw type- Parameters:
type
- raw type- Returns:
type
genericized with wildcards
-
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
-
newParameterizedType
Convert a raw class back into its generic type using caller-supplied type parameters.- Type Parameters:
T
- raw class type- Parameters:
target
- raw classparams
- type parameters- Returns:
- generic
TypeToken
fortarget
- See Also:
-
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
-