Class TypeUtils

java.lang.Object
org.apache.commons.lang3.reflect.TypeUtils

public class TypeUtils extends Object
Utility methods focusing on type inspection, particularly with regard to generics.
Since:
3.0
  • Field Details

  • Constructor Details

    • TypeUtils

      @Deprecated public TypeUtils()
      Deprecated.
      TODO Make private in 4.0.
      TypeUtils instances should NOT be constructed in standard programming. Instead, the class should be used as TypeUtils.isAssignable(cls, toClass).

      This constructor is public to permit tools that require a JavaBean instance to operate.

  • Method Details

    • anyToString

      private static <T> String anyToString(T object)
    • appendRecursiveTypes

      private static void appendRecursiveTypes(StringBuilder builder, int[] recursiveTypeIndexes, Type[] argumentTypes)
    • classToString

      private static <T> String classToString(Class<T> cls)
      Formats a Class as a String.
      Parameters:
      cls - Class to format.
      Returns:
      String.
    • containsTypeVariables

      public static boolean containsTypeVariables(Type type)
      Tests, recursively, whether any of the type parameters associated with type are bound to variables.
      Parameters:
      type - the type to check for type variables.
      Returns:
      boolean.
      Since:
      3.2
    • containsVariableTypeSameParametrizedTypeBound

      private static boolean containsVariableTypeSameParametrizedTypeBound(TypeVariable<?> typeVariable, ParameterizedType parameterizedType)
    • determineTypeArguments

      public static Map<TypeVariable<?>,Type> determineTypeArguments(Class<?> cls, ParameterizedType superParameterizedType)
      Tries to determine the type arguments of a class/interface based on a super parameterized type's type arguments. This method is the inverse of getTypeArguments(Type, Class) which gets a class/interface's type arguments based on a subtype. It is far more limited in determining the type arguments for the subject class's type variables in that it can only determine those parameters that map from the subject Class object to the supertype.

      Example: TreeSet sets its parameter as the parameter for NavigableSet, which in turn sets the parameter of SortedSet, which in turn sets the parameter of Set, which in turn sets the parameter of Collection, which in turn sets the parameter of Iterable. Since TreeSet's parameter maps (indirectly) to Iterable's parameter, it will be able to determine that based on the super type Iterable<? extends Map<Integer, ? extends Collection<?>>>, the parameter of TreeSet is ? extends Map<Integer, ? extends Collection<?>>.

      Parameters:
      cls - the class whose type parameters are to be determined, not null.
      superParameterizedType - the super type from which cls's type arguments are to be determined, not null.
      Returns:
      a Map of the type assignments that could be determined for the type variables in each type in the inheritance hierarchy from type to toClass inclusive.
      Throws:
      NullPointerException - if either cls or superParameterizedType is null.
    • equals

      private static boolean equals(GenericArrayType genericArrayType, Type type)
      Tests whether t equals a.
      Parameters:
      genericArrayType - LHS.
      type - RHS.
      Returns:
      boolean.
    • equals

      private static boolean equals(ParameterizedType parameterizedType, Type type)
      Tests whether t equals p.
      Parameters:
      parameterizedType - LHS.
      type - RHS.
      Returns:
      boolean.
    • equals

      public static boolean equals(Type type1, Type type2)
      Tests equality of types.
      Parameters:
      type1 - the first type.
      type2 - the second type.
      Returns:
      boolean.
      Since:
      3.2
    • equals

      private static boolean equals(Type[] type1, Type[] type2)
      Tests whether t1 equals t2.
      Parameters:
      type1 - LHS.
      type2 - RHS.
      Returns:
      boolean.
    • equals

      private static boolean equals(WildcardType wildcardType, Type type)
      Tests whether t equals w.
      Parameters:
      wildcardType - LHS.
      type - RHS.
      Returns:
      boolean.
    • extractTypeArgumentsFrom

      private static Type[] extractTypeArgumentsFrom(Map<TypeVariable<?>,Type> mappings, TypeVariable<?>[] variables)
      Helper method to establish the formal parameters for a parameterized type.
      Parameters:
      mappings - map containing the assignments.
      variables - expected map keys.
      Returns:
      array of map values corresponding to specified keys.
    • findRecursiveTypes

      private static int[] findRecursiveTypes(ParameterizedType parameterizedType)
    • genericArrayType

      public static GenericArrayType genericArrayType(Type componentType)
      Creates a generic array type instance.
      Parameters:
      componentType - the type of the elements of the array. For example the component type of boolean[] is boolean.
      Returns:
      GenericArrayType.
      Since:
      3.2
    • genericArrayTypeToString

      private static String genericArrayTypeToString(GenericArrayType genericArrayType)
      Formats a GenericArrayType as a String.
      Parameters:
      genericArrayType - GenericArrayType to format.
      Returns:
      String.
    • getArrayComponentType

      public static Type getArrayComponentType(Type type)
      Gets the array component type of type.
      Parameters:
      type - the type to be checked.
      Returns:
      component type or null if type is not an array type.
    • getClosestParentType

      private static Type getClosestParentType(Class<?> cls, Class<?> superClass)
      Gets the closest parent type to the super class specified by superClass.
      Parameters:
      cls - the class in question.
      superClass - the super class.
      Returns:
      the closes parent type.
    • getImplicitBounds

      public static Type[] getImplicitBounds(TypeVariable<?> typeVariable)
      Gets an array containing the sole type of Object if TypeVariable.getBounds() returns an empty array. Otherwise, it returns the result of TypeVariable.getBounds() passed into normalizeUpperBounds(java.lang.reflect.Type[]).
      Parameters:
      typeVariable - the subject type variable, not null.
      Returns:
      a non-empty array containing the bounds of the type variable, which could be Object.
      Throws:
      NullPointerException - if typeVariable is null.
    • getImplicitLowerBounds

      public static Type[] getImplicitLowerBounds(WildcardType wildcardType)
      Gets an array containing a single value of null if WildcardType.getLowerBounds() returns an empty array. Otherwise, it returns the result of WildcardType.getLowerBounds().
      Parameters:
      wildcardType - the subject wildcard type, not null.
      Returns:
      a non-empty array containing the lower bounds of the wildcard type, which could be null.
      Throws:
      NullPointerException - if wildcardType is null.
    • getImplicitUpperBounds

      public static Type[] getImplicitUpperBounds(WildcardType wildcardType)
      Gets an array containing the sole value of Object if WildcardType.getUpperBounds() returns an empty array. Otherwise, it returns the result of WildcardType.getUpperBounds() passed into normalizeUpperBounds(java.lang.reflect.Type[]).
      Parameters:
      wildcardType - the subject wildcard type, not null.
      Returns:
      a non-empty array containing the upper bounds of the wildcard type.
      Throws:
      NullPointerException - if wildcardType is null.
    • getRawType

      private static Class<?> getRawType(ParameterizedType parameterizedType)
      Transforms the passed in type to a Class object. Type-checking method of convenience.
      Parameters:
      parameterizedType - the type to be converted.
      Returns:
      the corresponding Class object.
      Throws:
      IllegalStateException - if the conversion fails.
    • getRawType

      public static Class<?> getRawType(Type type, Type assigningType)
      Gets the raw type of a Java type, given its context. Primarily for use with TypeVariables and GenericArrayTypes, or when you do not know the runtime type of type: if you know you have a Class instance, it is already raw; if you know you have a ParameterizedType, its raw type is only a method call away.
      Parameters:
      type - to resolve.
      assigningType - type to be resolved against.
      Returns:
      the resolved Class object or null if the type could not be resolved.
    • getTypeArguments

      private static Map<TypeVariable<?>,Type> getTypeArguments(Class<?> cls, Class<?> toClass, Map<TypeVariable<?>,Type> subtypeVarAssigns)
      Gets a map of the type arguments of a class in the context of toClass.
      Parameters:
      cls - the class in question.
      toClass - the context class.
      subtypeVarAssigns - a map with type variables.
      Returns:
      the Map with type arguments.
    • getTypeArguments

      public static Map<TypeVariable<?>,Type> getTypeArguments(ParameterizedType type)
      Gets all the type arguments for this parameterized type including owner hierarchy arguments such as Outer<K, V>.Inner<T>.DeepInner<E> . The arguments are returned in a Map specifying the argument type for each TypeVariable.
      Parameters:
      type - specifies the subject parameterized type from which to harvest the parameters.
      Returns:
      a Map of the type arguments to their respective type variables.
    • getTypeArguments

      private static Map<TypeVariable<?>,Type> getTypeArguments(ParameterizedType parameterizedType, Class<?> toClass, Map<TypeVariable<?>,Type> subtypeVarAssigns)
      Gets a map of the type arguments of a parameterized type in the context of toClass.
      Parameters:
      parameterizedType - the parameterized type.
      toClass - the class.
      subtypeVarAssigns - a map with type variables.
      Returns:
      the Map with type arguments.
    • getTypeArguments

      public static Map<TypeVariable<?>,Type> getTypeArguments(Type type, Class<?> toClass)
      Gets the type arguments of a class/interface based on a subtype. For instance, this method will determine that both of the parameters for the interface Map are Object for the subtype Properties even though the subtype does not directly implement the Map interface.

      This method returns null if type is not assignable to toClass. It returns an empty map if none of the classes or interfaces in its inheritance hierarchy specify any type arguments.

      A side effect of this method is that it also retrieves the type arguments for the classes and interfaces that are part of the hierarchy between type and toClass. So with the above example, this method will also determine that the type arguments for Hashtable are also both Object. In cases where the interface specified by toClass is (indirectly) implemented more than once (e.g. where toClass specifies the interface Iterable and type specifies a parameterized type that implements both Set and Collection), this method will look at the inheritance hierarchy of only one of the implementations/subclasses; the first interface encountered that isn't a subinterface to one of the others in the type to toClass hierarchy.

      Parameters:
      type - the type from which to determine the type parameters of toClass
      toClass - the class whose type parameters are to be determined based on the subtype type
      Returns:
      a Map of the type assignments for the type variables in each type in the inheritance hierarchy from type to toClass inclusive.
    • getTypeArguments

      private static Map<TypeVariable<?>,Type> getTypeArguments(Type type, Class<?> toClass, Map<TypeVariable<?>,Type> subtypeVarAssigns)
      Gets a map of the type arguments of type in the context of toClass.
      Parameters:
      type - the type in question.
      toClass - the class.
      subtypeVarAssigns - a map with type variables.
      Returns:
      the Map with type arguments.
    • isArrayType

      public static boolean isArrayType(Type type)
      Tests whether the specified type denotes an array type.
      Parameters:
      type - the type to be checked.
      Returns:
      true if type is an array class or a GenericArrayType.
    • isAssignable

      private static boolean isAssignable(Type type, Class<?> toClass)
      Tests if the subject type may be implicitly cast to the target class following the Java generics rules.
      Parameters:
      type - the subject type to be assigned to the target type.
      toClass - the target class.
      Returns:
      true if type is assignable to toClass.
    • isAssignable

      private static boolean isAssignable(Type type, GenericArrayType toGenericArrayType, Map<TypeVariable<?>,Type> typeVarAssigns)
      Tests if the subject type may be implicitly cast to the target generic array type following the Java generics rules.
      Parameters:
      type - the subject type to be assigned to the target type.
      toGenericArrayType - the target generic array type.
      typeVarAssigns - a map with type variables.
      Returns:
      true if type is assignable to toGenericArrayType.
    • isAssignable

      private static boolean isAssignable(Type type, ParameterizedType toParameterizedType, Map<TypeVariable<?>,Type> typeVarAssigns)
      Tests if the subject type may be implicitly cast to the target parameterized type following the Java generics rules.
      Parameters:
      type - the subject type to be assigned to the target type.
      toParameterizedType - the target parameterized type.
      typeVarAssigns - a map with type variables.
      Returns:
      true if type is assignable to toType.
    • isAssignable

      public static boolean isAssignable(Type type, Type toType)
      Tests if the subject type may be implicitly cast to the target type following the Java generics rules. If both types are Class objects, the method returns the result of ClassUtils.isAssignable(Class, Class).
      Parameters:
      type - the subject type to be assigned to the target type.
      toType - the target type.
      Returns:
      true if type is assignable to toType.
    • isAssignable

      private static boolean isAssignable(Type type, Type toType, Map<TypeVariable<?>,Type> typeVarAssigns)
      Tests if the subject type may be implicitly cast to the target type following the Java generics rules.
      Parameters:
      type - the subject type to be assigned to the target type.
      toType - the target type.
      typeVarAssigns - optional map of type variable assignments.
      Returns:
      true if type is assignable to toType.
    • isAssignable

      private static boolean isAssignable(Type type, TypeVariable<?> toTypeVariable, Map<TypeVariable<?>,Type> typeVarAssigns)
      Tests if the subject type may be implicitly cast to the target type variable following the Java generics rules.
      Parameters:
      type - the subject type to be assigned to the target type.
      toTypeVariable - the target type variable.
      typeVarAssigns - a map with type variables.
      Returns:
      true if type is assignable to toTypeVariable.
    • isAssignable

      private static boolean isAssignable(Type type, WildcardType toWildcardType, Map<TypeVariable<?>,Type> typeVarAssigns)
      Tests if the subject type may be implicitly cast to the target wildcard type following the Java generics rules.
      Parameters:
      type - the subject type to be assigned to the target type.
      toWildcardType - the target wildcard type.
      typeVarAssigns - a map with type variables.
      Returns:
      true if type is assignable to toWildcardType.
    • isCyclical

      private static boolean isCyclical(Class<?> cls)
      Tests whether the class contains a cyclical reference in the qualified name of a class. If any of the type parameters of A class is extending X class which is in scope of A class, then it forms cycle.
      Parameters:
      cls - the class to test.
      Returns:
      whether the class contains a cyclical reference.
    • isInstance

      public static boolean isInstance(Object value, Type type)
      Tests if the given value can be assigned to the target type following the Java generics rules.
      Parameters:
      value - the value to be checked.
      type - the target type.
      Returns:
      true if value is an instance of type.
    • mapTypeVariablesToArguments

      private static <T> void mapTypeVariablesToArguments(Class<T> cls, ParameterizedType parameterizedType, Map<TypeVariable<?>,Type> typeVarAssigns)
      Maps type variables.
      Type Parameters:
      T - the generic type of the class in question.
      Parameters:
      cls - the class in question.
      parameterizedType - the parameterized type.
      typeVarAssigns - the map to be filled.
    • normalizeUpperBounds

      public static Type[] normalizeUpperBounds(Type[] bounds)
      Strips out the redundant upper bound types in type variable types and wildcard types (or it would with wildcard types if multiple upper bounds were allowed).

      Example, with the variable type declaration:

      
       <K extends java.util.Collection<String> & java.util.List<String>>
       

      since List is a subinterface of Collection, this method will return the bounds as if the declaration had been:

      
       <K extends java.util.List<String>>
       
      Parameters:
      bounds - an array of types representing the upper bounds of either WildcardType or TypeVariable, not null.
      Returns:
      an array containing the values from bounds minus the redundant types.
      Throws:
      NullPointerException - if bounds is null.
    • normalizeUpperToObject

      private static Type[] normalizeUpperToObject(Type[] bounds)
      Delegates to normalizeUpperBounds(Type[]) unless bounds is empty in which case return an array with the element Object.class.
      Parameters:
      bounds - bounds an array of types representing the upper bounds of either WildcardType or TypeVariable, not null.
      Returns:
      result from normalizeUpperBounds(Type[]) unless bounds is empty in which case return an array with the element Object.class.
    • parameterize

      public static final ParameterizedType parameterize(Class<?> rawClass, Map<TypeVariable<?>,Type> typeVariableMap)
      Creates a parameterized type instance.
      Parameters:
      rawClass - the raw class to create a parameterized type instance for.
      typeVariableMap - the map used for parameterization.
      Returns:
      ParameterizedType.
      Throws:
      NullPointerException - if either rawClass or typeVariableMap is null.
      Since:
      3.2
    • parameterize

      public static final ParameterizedType parameterize(Class<?> rawClass, Type... typeArguments)
      Creates a parameterized type instance.
      Parameters:
      rawClass - the raw class to create a parameterized type instance for.
      typeArguments - the types used for parameterization.
      Returns:
      ParameterizedType.
      Throws:
      NullPointerException - if rawClass is null.
      Since:
      3.2
    • parameterizedTypeToString

      private static String parameterizedTypeToString(ParameterizedType parameterizedType)
      Formats a ParameterizedType as a String.
      Parameters:
      parameterizedType - ParameterizedType to format.
      Returns:
      String.
    • parameterizeWithOwner

      public static final ParameterizedType parameterizeWithOwner(Type owner, Class<?> rawClass, Map<TypeVariable<?>,Type> typeVariableMap)
      Creates a parameterized type instance.
      Parameters:
      owner - the owning type.
      rawClass - the raw class to create a parameterized type instance for.
      typeVariableMap - the map used for parameterization.
      Returns:
      ParameterizedType.
      Throws:
      NullPointerException - if either rawClass or typeVariableMap is null.
      Since:
      3.2
    • parameterizeWithOwner

      public static final ParameterizedType parameterizeWithOwner(Type owner, Class<?> rawClass, Type... typeArguments)
      Creates a parameterized type instance.
      Parameters:
      owner - the owning type.
      rawClass - the raw class to create a parameterized type instance for.
      typeArguments - the types used for parameterization.
      Returns:
      ParameterizedType.
      Throws:
      NullPointerException - if rawClass is null.
      Since:
      3.2
    • substituteTypeVariables

      private static Type substituteTypeVariables(Type type, Map<TypeVariable<?>,Type> typeVarAssigns)
      Finds the mapping for type in typeVarAssigns.
      Parameters:
      type - the type to be replaced.
      typeVarAssigns - the map with type variables.
      Returns:
      the replaced type.
      Throws:
      IllegalArgumentException - if the type cannot be substituted.
    • toLongString

      public static String toLongString(TypeVariable<?> typeVariable)
      Formats a TypeVariable including its GenericDeclaration.
      Parameters:
      typeVariable - the type variable to create a String representation for, not null.
      Returns:
      String.
      Throws:
      NullPointerException - if typeVariable is null.
      Since:
      3.2
    • toString

      public static String toString(Type type)
      Formats a given type as a Java-esque String.
      Parameters:
      type - the type to create a String representation for, not null.
      Returns:
      String.
      Throws:
      NullPointerException - if type is null.
      Since:
      3.2
    • typesSatisfyVariables

      public static boolean typesSatisfyVariables(Map<TypeVariable<?>,Type> typeVariableMap)
      Determines whether or not specified types satisfy the bounds of their mapped type variables. When a type parameter extends another (such as <T, S extends T>), uses another as a type parameter (such as <T, S extends Comparable>>), or otherwise depends on another type variable to be specified, the dependencies must be included in typeVarAssigns.
      Parameters:
      typeVariableMap - specifies the potential types to be assigned to the type variables, not null.
      Returns:
      whether or not the types can be assigned to their respective type variables.
      Throws:
      NullPointerException - if typeVariableMap is null.
    • typeVariableToString

      private static String typeVariableToString(TypeVariable<?> typeVariable)
      Formats a TypeVariable as a String.
      Parameters:
      typeVariable - TypeVariable to format.
      Returns:
      String.
    • unrollBounds

      private static Type[] unrollBounds(Map<TypeVariable<?>,Type> typeArguments, Type[] bounds)
      Unrolls variables in a type bounds array.
      Parameters:
      typeArguments - assignments Map.
      bounds - in which to expand variables.
      Returns:
      bounds with any variables reassigned.
    • unrollVariableAssignments

      private static Type unrollVariableAssignments(TypeVariable<?> typeVariable, Map<TypeVariable<?>,Type> typeVarAssigns)
      Looks up typeVariable in typeVarAssigns transitively, i.e. keep looking until the value found is not a type variable.
      Parameters:
      typeVariable - the type variable to look up.
      typeVarAssigns - the map used for the look-up.
      Returns:
      Type or null if some variable was not in the map.
    • unrollVariables

      public static Type unrollVariables(Map<TypeVariable<?>,Type> typeArguments, Type type)
      Gets a type representing type with variable assignments "unrolled."
      Parameters:
      typeArguments - as from getTypeArguments(Type, Class).
      type - the type to unroll variable assignments for.
      Returns:
      Type.
      Since:
      3.2
    • wildcardType

      public static TypeUtils.WildcardTypeBuilder wildcardType()
      Returns:
      a new TypeUtils.WildcardTypeBuilder.
      Since:
      3.2
    • wildcardTypeToString

      private static String wildcardTypeToString(WildcardType wildcardType)
      Formats a WildcardType as a String.
      Parameters:
      wildcardType - WildcardType to format.
      Returns:
      String.
    • wrap

      public static <T> Typed<T> wrap(Class<T> type)
      Wraps the specified Class in a Typed wrapper.
      Type Parameters:
      T - generic type.
      Parameters:
      type - to wrap.
      Returns:
      Typed<T>.
      Since:
      3.2
    • wrap

      public static <T> Typed<T> wrap(Type type)
      Wraps the specified Type in a Typed wrapper.
      Type Parameters:
      T - inferred generic type.
      Parameters:
      type - to wrap.
      Returns:
      Typed<T>.
      Since:
      3.2