Class Types


  • public class Types
    extends java.lang.Object
    Type arithmetic functions.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      private static class  Types.BinderArg  
    • Constructor Summary

      Constructors 
      Constructor Description
      Types()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.reflect.Type bind​(java.lang.reflect.Type t, java.lang.reflect.GenericDeclaration decl, java.lang.reflect.ParameterizedType args)
      Replaces the type variables in t by its actual values.
      static java.lang.reflect.ParameterizedType createParameterizedType​(java.lang.Class rawType, java.lang.reflect.Type... arguments)
      Returns the Type object that represents clazz<T1,T2,T3>.
      static <T> java.lang.Class<T> erasure​(java.lang.reflect.Type t)
      Returns the Class representation of the given type.
      private static java.lang.reflect.Type fix​(java.lang.reflect.Type t)
      JDK 5.0 has a bug of createing GenericArrayType where it shouldn't.
      static java.lang.reflect.Type getBaseClass​(java.lang.reflect.Type type, java.lang.Class baseType)
      Gets the parameterization of the given base type.
      static java.lang.reflect.Type getComponentType​(java.lang.reflect.Type t)
      Gets the component type of the array.
      static java.lang.reflect.Type getTypeArgument​(java.lang.reflect.Type type, int i)
      Gets the i-th type argument from a parameterized type.
      static java.lang.reflect.Type getTypeArgument​(java.lang.reflect.Type type, int i, java.lang.reflect.Type defaultValue)
      Gets the i-th type argument from a parameterized type.
      static java.lang.String getTypeName​(java.lang.reflect.Type type)
      Gets the display name of the type object
      static boolean isArray​(java.lang.reflect.Type t)
      Checks if the type is an array type.
      static boolean isArrayButNotByteArray​(java.lang.reflect.Type t)
      Checks if the type is an array type but not byte[].
      static boolean isOverriding​(java.lang.reflect.Method method, java.lang.Class base)
      Tests if the given method overrides another method defined in 'base' (or its super types.)
      static boolean isPrimitive​(java.lang.reflect.Type type)
      Checks if the given type is a primitive type.
      static boolean isSubClassOf​(java.lang.reflect.Type sub, java.lang.reflect.Type sup)
      Checks if sub is a sub-type of sup.
      • Methods inherited from class java.lang.Object

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

      • baseClassFinder

        private static final TypeVisitor<java.lang.reflect.Type,​java.lang.Class> baseClassFinder
      • eraser

        private static final TypeVisitor<java.lang.Class,​java.lang.Void> eraser
        Implements the logic for erasure(Type).
    • Constructor Detail

      • Types

        public Types()
    • Method Detail

      • bind

        public static java.lang.reflect.Type bind​(java.lang.reflect.Type t,
                                                  java.lang.reflect.GenericDeclaration decl,
                                                  java.lang.reflect.ParameterizedType args)
        Replaces the type variables in t by its actual values.

        This is primarily used to resolve a method of a generic type to a concrete signature.

        For example, binding Collection<T> with T=List<String> results in Collection<List<String>>.

        Parameters:
        decl - provides a list of type variables. See GenericDeclaration.getTypeParameters()
        args - actual arguments. See ParameterizedType.getActualTypeArguments()
      • getBaseClass

        public static java.lang.reflect.Type getBaseClass​(java.lang.reflect.Type type,
                                                          java.lang.Class baseType)
        Gets the parameterization of the given base type.

        For example, given the following

        
         interface Foo<T> extends List<List<T>> {}
         interface Bar extends Foo<String> {}
         
        This method works like this:
        
         getBaseClass( Bar, List ) = List<List<String>
         getBaseClass( Bar, Foo  ) = Foo<String>
         getBaseClass( Foo<? extends Number>, Collection ) = Collection<List<? extends Number>>
         getBaseClass( ArrayList<? extends BigInteger>, List ) = List<? extends BigInteger>
         
        Parameters:
        type - The type that derives from baseType
        baseType - The class whose parameterization we are interested in.
        Returns:
        The use of baseType in type. or null if the type is not assignable to the base type.
      • getTypeName

        public static java.lang.String getTypeName​(java.lang.reflect.Type type)
        Gets the display name of the type object
        Returns:
        a human-readable name that the type represents.
      • isSubClassOf

        public static boolean isSubClassOf​(java.lang.reflect.Type sub,
                                           java.lang.reflect.Type sup)
        Checks if sub is a sub-type of sup.
      • erasure

        public static <T> java.lang.Class<T> erasure​(java.lang.reflect.Type t)
        Returns the Class representation of the given type. This corresponds to the notion of the erasure in JSR-14.

        It made me realize how difficult it is to define the common navigation layer for two different underlying reflection library. The other way is to throw away the entire parameterization and go to the wrapper approach.

      • createParameterizedType

        public static java.lang.reflect.ParameterizedType createParameterizedType​(java.lang.Class rawType,
                                                                                  java.lang.reflect.Type... arguments)
        Returns the Type object that represents clazz&lt;T1,T2,T3>.
      • isArray

        public static boolean isArray​(java.lang.reflect.Type t)
        Checks if the type is an array type.
      • isArrayButNotByteArray

        public static boolean isArrayButNotByteArray​(java.lang.reflect.Type t)
        Checks if the type is an array type but not byte[].
      • getComponentType

        public static java.lang.reflect.Type getComponentType​(java.lang.reflect.Type t)
        Gets the component type of the array.
        Parameters:
        t - must be an array.
      • getTypeArgument

        public static java.lang.reflect.Type getTypeArgument​(java.lang.reflect.Type type,
                                                             int i)
        Gets the i-th type argument from a parameterized type.

        Unlike getTypeArgument(Type, int, Type), this method throws IllegalArgumentException if the given type is not parameterized.

      • getTypeArgument

        public static java.lang.reflect.Type getTypeArgument​(java.lang.reflect.Type type,
                                                             int i,
                                                             java.lang.reflect.Type defaultValue)
        Gets the i-th type argument from a parameterized type.

        For example, getTypeArgument([Map<Integer,String>],0)=Integer If the given type is not a parameterized type, returns the specified default value.

        This is convenient for handling raw types and parameterized types uniformly.

        Throws:
        java.lang.IndexOutOfBoundsException - If i is out of range.
      • isPrimitive

        public static boolean isPrimitive​(java.lang.reflect.Type type)
        Checks if the given type is a primitive type.
      • isOverriding

        public static boolean isOverriding​(java.lang.reflect.Method method,
                                           java.lang.Class base)
        Tests if the given method overrides another method defined in 'base' (or its super types.)
      • fix

        private static java.lang.reflect.Type fix​(java.lang.reflect.Type t)
        JDK 5.0 has a bug of createing GenericArrayType where it shouldn't. fix that manually to work around the problem. See bug 6202725.