Package io.permazen

Class Util

java.lang.Object
io.permazen.Util

public final class Util extends Object
Various utility routines.
  • 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 in AnnotationScanner utilizes Spring's AnnotatedElementUtils.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 invokes AnnotatedElement.getAnnotation(Class).

      Type Parameters:
      A - annotation type
      Parameters:
      element - element with annotation or meta-present annotation
      annotationType - type of the annotation to find
      Returns:
      the annotation found, or null if not found
    • hasValidation

      public 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).
      Parameters:
      type - object type
      Returns:
      a non-null object with JSR 303 validation requirements, or null if none found
      Throws:
      IllegalArgumentException - if type is null
    • requiresDefaultValidation

      public static boolean requiresDefaultValidation(Class<?> type)
      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 - if type is null
      See Also:
    • requiresDefaultValidation

      public 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.
      Parameters:
      method - annotated method
      Returns:
      true if obj has one or more JSR 303 annotations
      Throws:
      IllegalArgumentException - if method is null
    • hasDefaultValidationAnnotation

      public static boolean hasDefaultValidationAnnotation(AnnotatedElement obj)
      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 - if obj is null
    • hasValidationAnnotation

      public static boolean hasValidationAnnotation(AnnotatedElement obj)
      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 - if obj is null
    • isGroupBeingValidated

      public static boolean isGroupBeingValidated(Class<?> constraintGroup, Class<?>[] validationGroups)
      Determine if a constraint whose groups() contain the given constraint group should be applied when validating with the given validation groups.
      Parameters:
      constraintGroup - validation group associated with a validation constraint
      validationGroups - 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 whose groups() contain the given constraint groups should be applied when validating with the given validation groups.
      Parameters:
      constraintGroups - validation groups associated with a validation constraint
      validationGroups - groups for which validation is being performed
      Returns:
      whether to apply the validation constraint
      Throws:
      IllegalArgumentException - if any null values are encountered
    • findField

      public static PermazenField findField(PermazenClass<?> pclass, String fieldName)
      Identify the named field in the given PermazenClass.

      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 type
      fieldName - field name
      Returns:
      resulting PermazenField, or null if no such field is found in pclass
      Throws:
      IllegalArgumentException - if fieldName is ambiguous or invalid
      IllegalArgumentException - if pclass or fieldName is null
    • findSimpleField

      public static PermazenSimpleField findSimpleField(PermazenClass<?> pclass, String fieldName)
      Identify the named simple field in the given PermazenClass.

      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-PermazenSimpleFields.

      Parameters:
      pclass - containing object type
      fieldName - field name
      Returns:
      resulting PermazenField, or null if no such field is found in pclass
      Throws:
      IllegalArgumentException - if fieldName is ambiguous or invalid
      IllegalArgumentException - if pclass or fieldName is null
    • findField

      public static PermazenField findField(PermazenClass<?> pclass, String fieldName, Boolean expectSubField)
      Identify the named field in the given PermazenClass.

      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 type
      fieldName - field name
      expectSubField - 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 in pclass
      Throws:
      IllegalArgumentException - if fieldName is ambiguous or invalid
      IllegalArgumentException - if pclass or fieldName is null
    • findLowestCommonAncestorOfClasses

      public static TypeToken<?> findLowestCommonAncestorOfClasses(Stream<Class<?>> types)
      Find the narrowest type that is a supertype of all of the given types.

      This method delegates to findLowestCommonAncestor() after converting the Class instances to TypeTokens.

      Parameters:
      types - sub-types
      Returns:
      narrowest common super-type
      Throws:
      IllegalArgumentException - if types or any type in types is null
      See Also:
    • findLowestCommonAncestorsOfClasses

      public static Set<TypeToken<?>> findLowestCommonAncestorsOfClasses(Stream<Class<?>> types)
      Find the narrowest type(s) each of which is a supertype of all of the given types.

      This method delegates to findLowestCommonAncestors() after converting the Class instances to TypeTokens.

      Parameters:
      types - sub-types
      Returns:
      maximally narrow common supertype(s)
      Throws:
      IllegalArgumentException - if any type in types is null
      See Also:
    • findLowestCommonAncestor

      public static TypeToken<?> findLowestCommonAncestor(Stream<TypeToken<?>> types)
      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

      public static Set<TypeToken<?>> findLowestCommonAncestors(Stream<TypeToken<?>> types)
      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

      public static <T> Stream<T> streamOf(T obj)
      Substitute for Stream.of(Object).

      Permazen needs this method because the v1.6 class files we generate don't support invoking static methods on interfaces.

    • getWildcardedType

      public static <T> TypeToken<? extends T> getWildcardedType(Class<T> type)
      Parameterize the raw type with wildcards.
      Type Parameters:
      T - raw type
      Parameters:
      type - raw type
      Returns:
      type genericized with wildcards
    • getTypeParameter

      public static Type getTypeParameter(Type type, int index)
      Get the n'th generic type parameter.
      Parameters:
      type - parameterized generic type
      index - type parameter index (zero based)
      Returns:
      type parameter at index
      Throws:
      IllegalArgumentException - if type is not a parameterized type with more than index type variables
    • invoke

      public static Object invoke(Method method, Object target, Object... params)
      Invoke method via reflection and re-throw any checked exception wrapped in an PermazenException.
      Parameters:
      method - method to invoke
      target - instance, or null if method is static
      params - method parameters
      Returns:
      method return value
      Throws:
      PermazenException - if an error occurs
    • newParameterizedType

      public 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.
      Type Parameters:
      T - raw class type
      Parameters:
      target - raw class
      params - type parameters
      Returns:
      generic TypeToken for target
      See Also:
    • reverse

      public static <A, B> Converter<B,A> reverse(Converter<A,B> converter)
      Return the reverse of the given Converter, treating a null converter as if it were the identity.
      Parameters:
      converter - original converter, or null for the identity conversion
      Returns:
      reversed converter