Class AnnotationApiHelper

java.lang.Object
org.hibernate.validator.ap.internal.util.AnnotationApiHelper

public class AnnotationApiHelper extends Object
A helper class providing some useful methods to work with types from the JSR-269-API.
Author:
Gunnar Morling
  • Constructor Details

    • AnnotationApiHelper

      public AnnotationApiHelper(Elements elementUtils, Types typeUtils)
  • Method Details

    • filterByType

      public List<AnnotationMirror> filterByType(List<? extends AnnotationMirror> annotationMirrors, TypeMirror annotationType)
      Returns a list containing those annotation mirrors from the input list, which are of type annotationType. The input collection remains untouched.
      Parameters:
      annotationMirrors - A list of annotation mirrors.
      annotationType - The type to be compared against.
      Returns:
      A list with those annotation mirrors from the input list, which are of type annotationType. May be empty but never null.
    • getMirror

      public AnnotationMirror getMirror(List<? extends AnnotationMirror> annotationMirrors, Class<? extends Annotation> annotationClazz)
      Returns that mirror from the given list of annotation mirrors that represents the annotation type specified by the given class.
      Parameters:
      annotationMirrors - A list of annotation mirrors.
      annotationClazz - The class of the annotation of interest.
      Returns:
      The mirror from the given list that represents the specified annotation or null, if the given list doesn't contain such a mirror.
    • getMirror

      public AnnotationMirror getMirror(List<? extends AnnotationMirror> annotationMirrors, String annotationTypeName)
      Returns that mirror from the given list of annotation mirrors that represents the annotation type specified by the given class.
      Parameters:
      annotationMirrors - A list of annotation mirrors.
      annotationTypeName - The FQN of the annotation of interest.
      Returns:
      The mirror from the given list that represents the specified annotation or null, if the given list doesn't contain such a mirror.
    • getMirrorForType

      public TypeMirror getMirrorForType(Class<?> clazz)
      Returns a TypeMirror for the given class.
      Parameters:
      clazz - The class of interest.
      Returns:
      A TypeMirror for the given class.
    • getDeclaredTypeByName

      public DeclaredType getDeclaredTypeByName(String className)
      Returns the DeclaredType for the given class name.
      Parameters:
      className - A fully qualified class name, e.g. "java.lang.String".
      Returns:
      A DeclaredType representing the type with the given name, or null, if no such type exists.
    • getAnnotationValue

      public AnnotationValue getAnnotationValue(AnnotationMirror annotationMirror, String name)
      Returns the annotation value of the given annotation mirror with the given name.
      Parameters:
      annotationMirror - An annotation mirror.
      name - The name of the annotation value of interest.
      Returns:
      The annotation value with the given name or null, if one of the input values is null or if no value with the given name exists within the given annotation mirror.
    • getAnnotationValueOrDefault

      public AnnotationValue getAnnotationValueOrDefault(AnnotationMirror annotationMirror, String name)
      Returns the annotation value of the given annotation mirror with the given name or its default value if it was not specified.
      Parameters:
      annotationMirror - An annotation mirror.
      name - The name of the annotation value of interest.
      Returns:
      The annotation value with the given name or it's default value or null, if one of the input values is null or if no value with the given name exists within the given annotation mirror.
    • getAnnotationArrayValue

      public List<? extends AnnotationValue> getAnnotationArrayValue(AnnotationMirror annotationMirror, String name)
      Returns the given annotation mirror's array-typed annotation value with the given name.
      Parameters:
      annotationMirror - An annotation mirror.
      name - The name of the annotation value of interest.
      Returns:
      The annotation value with the given name or an empty list, if no such value exists within the given annotation mirror or such a value exists but is not an array-typed one.
    • keepLowestTypePerHierarchy

      public Set<TypeMirror> keepLowestTypePerHierarchy(Set<TypeMirror> types)

      Returns a set containing the "lowest" type per hierarchy contained in the input set. The following examples shall demonstrate the behavior.

      • Input: String; Output: String
      • Input: Object, String; Output: String
      • Input: Object, Collection, List; Output: List
      • Input: Collection, Set, List; Output: List, Set
      Parameters:
      types - A set of type mirrors.
      Returns:
      A set with the lowest types per hierarchy or null, if the input set was null.
    • isInterface

      public boolean isInterface(TypeMirror typeMirror)
      Test if the given TypeMirror represents an interface or not.
    • isClass

      public boolean isClass(TypeMirror typeMirror)
      Test if the given TypeMirror represents a class or not.
    • determineUnwrapMode

      public AnnotationApiHelper.UnwrapMode determineUnwrapMode(AnnotationMirror annotationMirror)
      Checks the annotation's payload for unwrapping option (javax.validation.valueextraction.Unwrapping.Unwrap, javax.validation.valueextraction.Unwrapping.Skip) of constraint.
      Parameters:
      annotationMirror - constraint annotation mirror under check
      Returns:
      unwrapping option, if one is present in the annotation payload, AnnotationApiHelper.UnwrapMode.NONE otherwise