Class TypeFactory


  • public class TypeFactory
    extends java.lang.Object
    Utility class for creating instances of Type. These types can be used with the GenericTypeReflector or anything else handling Java types.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.lang.reflect.WildcardType UNBOUND_WILDCARD  
    • Constructor Summary

      Constructors 
      Constructor Description
      TypeFactory()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.reflect.AnnotatedType annotatedClass​(java.lang.Class<?> clazz, java.lang.annotation.Annotation[] annotations)  
      static java.lang.reflect.AnnotatedType annotatedInnerClass​(java.lang.reflect.Type owner, java.lang.Class<?> clazz, java.lang.annotation.Annotation[] annotations)  
      static <A extends java.lang.annotation.Annotation>
      A
      annotation​(java.lang.Class<A> annotationType, java.util.Map<java.lang.String,​java.lang.Object> values)
      Creates an instance of an annotation.
      static java.lang.reflect.AnnotatedArrayType arrayOf​(java.lang.reflect.AnnotatedType componentType, java.lang.annotation.Annotation[] annotations)
      Creates an AnnotatedArrayType wrapped around an array types created by arrayOf(Type)
      static java.lang.reflect.Type arrayOf​(java.lang.reflect.Type componentType)
      Creates a array type.
      private static void checkParametersWithinBound​(java.lang.reflect.ParameterizedType type)
      Check if the type arguments of the given type are within the bounds declared on the type parameters.
      private static boolean couldHaveCommonSubtype​(java.lang.reflect.Type type1, java.lang.reflect.Type type2)
      Checks if the intersection of two types is not empty.
      static java.lang.reflect.Type innerClass​(java.lang.reflect.Type owner, java.lang.Class<?> clazz)
      Creates a type of clazz nested in owner.
      static java.lang.reflect.AnnotatedType parameterizedAnnotatedClass​(java.lang.Class<?> clazz, java.lang.annotation.Annotation[] annotations, java.lang.reflect.AnnotatedType... arguments)  
      static java.lang.reflect.AnnotatedType parameterizedAnnotatedInnerClass​(java.lang.reflect.AnnotatedType owner, java.lang.Class<?> clazz, java.lang.annotation.Annotation[] annotations, java.lang.reflect.AnnotatedType... arguments)  
      static java.lang.reflect.AnnotatedType parameterizedAnnotatedInnerClass​(java.lang.reflect.Type owner, java.lang.Class<?> clazz, java.lang.annotation.Annotation[] annotations, java.lang.reflect.AnnotatedType... arguments)  
      static java.lang.reflect.AnnotatedParameterizedType parameterizedAnnotatedType​(java.lang.reflect.ParameterizedType type, java.lang.annotation.Annotation[] typeAnnotations, java.lang.annotation.Annotation[]... argumentAnnotations)  
      static java.lang.reflect.Type parameterizedClass​(java.lang.Class<?> clazz, java.lang.reflect.Type... arguments)
      Creates a type of class clazz with arguments as type arguments.
      static java.lang.reflect.Type parameterizedInnerClass​(java.lang.reflect.Type owner, java.lang.Class<?> clazz, java.lang.reflect.Type... arguments)
      Creates a type of clazz with arguments as type arguments, nested in owner.
      private static java.lang.reflect.Type transformOwner​(java.lang.reflect.Type givenOwner, java.lang.Class<?> clazz)
      Transforms the given owner type into an appropriate one when constructing a parameterized type.
      static java.lang.reflect.WildcardType unboundWildcard()
      Returns the wildcard type without bounds.
      static java.lang.reflect.WildcardType wildcardExtends​(java.lang.reflect.Type upperBound)
      Creates a wildcard type with an upper bound.
      static java.lang.reflect.WildcardType wildcardSuper​(java.lang.reflect.Type lowerBound)
      Creates a wildcard type with a lower bound.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • UNBOUND_WILDCARD

        private static final java.lang.reflect.WildcardType UNBOUND_WILDCARD
    • Constructor Detail

      • TypeFactory

        public TypeFactory()
    • Method Detail

      • parameterizedClass

        public static java.lang.reflect.Type parameterizedClass​(java.lang.Class<?> clazz,
                                                                java.lang.reflect.Type... arguments)
        Creates a type of class clazz with arguments as type arguments.

        For example: parameterizedClass(Map.class, Integer.class, String.class) returns the type Map<Integer, String>.

        Parameters:
        clazz - Type class of the type to create
        arguments - Type arguments for the variables of clazz, or null if these are not known.
        Returns:
        A ParameterizedType, or simply clazz if arguments is null or empty.
      • annotatedClass

        public static java.lang.reflect.AnnotatedType annotatedClass​(java.lang.Class<?> clazz,
                                                                     java.lang.annotation.Annotation[] annotations)
      • parameterizedAnnotatedClass

        public static java.lang.reflect.AnnotatedType parameterizedAnnotatedClass​(java.lang.Class<?> clazz,
                                                                                  java.lang.annotation.Annotation[] annotations,
                                                                                  java.lang.reflect.AnnotatedType... arguments)
      • annotatedInnerClass

        public static java.lang.reflect.AnnotatedType annotatedInnerClass​(java.lang.reflect.Type owner,
                                                                          java.lang.Class<?> clazz,
                                                                          java.lang.annotation.Annotation[] annotations)
      • parameterizedAnnotatedInnerClass

        public static java.lang.reflect.AnnotatedType parameterizedAnnotatedInnerClass​(java.lang.reflect.Type owner,
                                                                                       java.lang.Class<?> clazz,
                                                                                       java.lang.annotation.Annotation[] annotations,
                                                                                       java.lang.reflect.AnnotatedType... arguments)
      • parameterizedAnnotatedInnerClass

        public static java.lang.reflect.AnnotatedType parameterizedAnnotatedInnerClass​(java.lang.reflect.AnnotatedType owner,
                                                                                       java.lang.Class<?> clazz,
                                                                                       java.lang.annotation.Annotation[] annotations,
                                                                                       java.lang.reflect.AnnotatedType... arguments)
      • parameterizedAnnotatedType

        public static java.lang.reflect.AnnotatedParameterizedType parameterizedAnnotatedType​(java.lang.reflect.ParameterizedType type,
                                                                                              java.lang.annotation.Annotation[] typeAnnotations,
                                                                                              java.lang.annotation.Annotation[]... argumentAnnotations)
      • innerClass

        public static java.lang.reflect.Type innerClass​(java.lang.reflect.Type owner,
                                                        java.lang.Class<?> clazz)
        Creates a type of clazz nested in owner.
        Parameters:
        owner - The owner type. This should be a subtype of clazz.getDeclaringClass(), or null if no owner is known.
        clazz - Type class of the type to create
        Returns:
        A ParameterizedType if the class declaring clazz is generic and its type parameters are known in owner and clazz itself has no type parameters. Otherwise, just returns clazz.
      • parameterizedInnerClass

        public static java.lang.reflect.Type parameterizedInnerClass​(java.lang.reflect.Type owner,
                                                                     java.lang.Class<?> clazz,
                                                                     java.lang.reflect.Type... arguments)
        Creates a type of clazz with arguments as type arguments, nested in owner.

        In the ideal case, this returns a ParameterizedType with all generic information in it. If some type arguments are missing or if the resulting type simply doesn't need any type parameters, it returns the raw clazz. Note that types with some parameters specified and others not, don't exist in Java.

        If the caller does not know the exact owner type or arguments, null should be given (or parameterizedClass(Class, Type...) or innerClass(Type, Class) could be used). If they are not needed (non-generic owner and/or clazz has no type parameters), they will be filled in automatically. If they are needed but are not given, the raw clazz is returned.

        The specified owner may be any subtype of clazz.getDeclaringClass(). It is automatically converted into the right parameterized version of the declaring class. If clazz is a static (nested) class, the owner is not used.

        Parameters:
        owner - The owner type. This should be a subtype of clazz.getDeclaringClass(), or null if no owner is known.
        clazz - Type class of the type to create
        arguments - Type arguments for the variables of clazz, or null if these are not known.
        Returns:
        A ParameterizedType if clazz or the class declaring clazz is generic, and all the needed type arguments are specified in owner and arguments. Otherwise, just returns clazz.
        Throws:
        java.lang.IllegalArgumentException - if arguments (is non-null and) has an incorrect length, or if one of the arguments is not within the bounds declared on the matching type variable, or if owner is non-null but clazz has no declaring class (e.g. is a top-level class), or if owner is not a a subtype of clazz.getDeclaringClass().
        java.lang.NullPointerException - if clazz or one of the elements in arguments is null.
      • checkParametersWithinBound

        private static void checkParametersWithinBound​(java.lang.reflect.ParameterizedType type)
        Check if the type arguments of the given type are within the bounds declared on the type parameters. Only the type arguments of the type itself are checked, the possible owner type is assumed to be valid.

        It does not follow the checks defined in the JLS because there are several problems with those (see http://stackoverflow.com/questions/7003009 for one). Instead, this applies some intuition and follows what Java compilers seem to do.

        Parameters:
        type - possibly inconsistent type to check.
        Throws:
        java.lang.IllegalArgumentException - if the type arguments are not within the bounds
      • couldHaveCommonSubtype

        private static boolean couldHaveCommonSubtype​(java.lang.reflect.Type type1,
                                                      java.lang.reflect.Type type2)
        Checks if the intersection of two types is not empty.
      • transformOwner

        private static java.lang.reflect.Type transformOwner​(java.lang.reflect.Type givenOwner,
                                                             java.lang.Class<?> clazz)
        Transforms the given owner type into an appropriate one when constructing a parameterized type.
      • unboundWildcard

        public static java.lang.reflect.WildcardType unboundWildcard()
        Returns the wildcard type without bounds. This is the '?' in for example List<?>.
        Returns:
        The unbound wildcard type
      • wildcardExtends

        public static java.lang.reflect.WildcardType wildcardExtends​(java.lang.reflect.Type upperBound)
        Creates a wildcard type with an upper bound.

        For example wildcardExtends(String.class) returns the type ? extends String.

        Parameters:
        upperBound - Upper bound of the wildcard
        Returns:
        A wildcard type
      • wildcardSuper

        public static java.lang.reflect.WildcardType wildcardSuper​(java.lang.reflect.Type lowerBound)
        Creates a wildcard type with a lower bound.

        For example wildcardSuper(String.class) returns the type ? super String.

        Parameters:
        lowerBound - Lower bound of the wildcard
        Returns:
        A wildcard type
      • arrayOf

        public static java.lang.reflect.Type arrayOf​(java.lang.reflect.Type componentType)
        Creates a array type.

        If componentType is not a generic type but a Class object, this returns the Class representing the non-generic array type. Otherwise, returns a GenericArrayType.

        For example:

        • arrayOf(String.class) returns String[].class
        • arrayOf(parameterizedClass(List.class, String.class)) returns the GenericArrayType for List<String>[]
        Parameters:
        componentType - The type of the components of the array.
        Returns:
        An array type.
      • arrayOf

        public static java.lang.reflect.AnnotatedArrayType arrayOf​(java.lang.reflect.AnnotatedType componentType,
                                                                   java.lang.annotation.Annotation[] annotations)
        Creates an AnnotatedArrayType wrapped around an array types created by arrayOf(Type)
        Parameters:
        componentType - The type of the components of the array.
        annotations - The annotations to be added to the array type itself.
        Returns:
        An array type.
      • annotation

        public static <A extends java.lang.annotation.Annotation> A annotation​(java.lang.Class<A> annotationType,
                                                                               java.util.Map<java.lang.String,​java.lang.Object> values)
                                                                        throws AnnotationFormatException
        Creates an instance of an annotation.
        Type Parameters:
        A - The type of the annotation.
        Parameters:
        annotationType - The Class representing the type of the annotation to be created.
        values - A map of values to be assigned to the annotation elements.
        Returns:
        An Annotation instanceof matching annotationType
        Throws:
        AnnotationFormatException - Thrown if incomplete or invalid values are provided