Class TypeUtil


  • public final class TypeUtil
    extends Object
    This class provides utilities to work with java Types. Its main goal is to provide tools for working with generic types.
    Author:
    Eugen Cepoi
    • Constructor Detail

      • TypeUtil

        public TypeUtil()
    • Method Detail

      • wrap

        public static final Class<?> wrap​(Class<?> clazz)
      • expandType

        public static final Type expandType​(Type type,
                                            Type rootType)
        Expands type in the type rootType to Class, ParameterizedType or GenericArrayType. Useful for generic types. rootType is used to get the specialization information for expansion.
      • lookupGenericType

        public static final Type lookupGenericType​(Class<?> ofClass,
                                                   Class<?> inClass)
        Searches for ofClass in the inherited classes and interfaces of inClass. If ofClass has been found in the super classes/interfaces of inClass, then the generic type corresponding to inClass and its TypeVariables is returned, otherwise null. For example :

         abstract class MyClass implements Serializer<Number> {
        
         }
        
         // type value will be the parameterized type Serializer<Number>
         Type type = lookupGenericType(Serializer.class, MyClass.class);
         
      • getRawClass

        public static final Class<?> getRawClass​(Type type)
      • getCollectionType

        public static final Type getCollectionType​(Type type)
        Returns the type of this Collection or Array.
        Throws:
        IllegalArgumentException - if type is not a Collection, not a generic array and not a primitive array.
      • resolveTypeVariable

        public static final Type resolveTypeVariable​(TypeVariable<? extends GenericDeclaration> type,
                                                     Class<?> inClass)
        Searches for the typevariable definition in the inClass hierarchy.
        Parameters:
        type -
        inClass -
        Returns:
        the resolved type or type if unable to resolve it.
      • match

        public static final boolean match​(Type type,
                                          Type oType,
                                          boolean strictMatch)
        Deep comparison between type and oType. If parameter strictMatch is true, then type and oType will be strictly compared otherwise this method checks whether oType is assignable from type.
      • typeOf

        public static final Type typeOf​(int parameterIdx,
                                        Type fromType)
        Convenient method that returns the type of the parameter at position parameterIdx in the type fromType.
        Throws:
        UnsupportedOperationException - thrown if fromType is not a Class nor a ParameterizedType.