Class Types


  • public class Types
    extends Object
    Type arithmetic functions.
    Author:
    Kohsuke Kawaguchi
    • Constructor Detail

      • Types

        public Types()
    • Method Detail

      • getBaseClass

        public static Type getBaseClass​(Type type,
                                        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 String getTypeName​(Type type)
        Gets the display name of the type object
        Returns:
        a human-readable name that the type represents.
      • isSubClassOf

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

        public static <T> Class<T> erasure​(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 ParameterizedType createParameterizedType​(Class rawType,
                                                                Type... arguments)
        Returns the Type object that represents clazz&lt;T1,T2,T3>.
      • isArray

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

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

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

        public static Type getTypeArgument​(Type type,
                                           int i,
                                           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:
        IndexOutOfBoundsException - If i is out of range.
      • isPrimitive

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

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