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
    • 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 type
      candidates - possible candidates for parameter
      Returns:
      mismatching candidate, or null if generic and erased match the same candidates
    • 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.

    • 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
    • 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