Overview | Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## org.apache.commons.lang3.reflect
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
Version:
  $Id: TypeUtils.java 966171 2010-07-21 10:47:57Z sebb $
Author:
  David M. Sledge, Matt Benson, James Carman

Constructor Summary
`TypeUtils()` TypeUtils instances should NOT be constructed in standard programming.

Method Summary
`static Map<TypeVariable<?>,Type>` | `determineTypeArguments(Class<?> cls, ParameterizedType superType)` Tries to determine the type arguments of a class/interface based on a super parameterized type's type arguments.
`static Type[]` | `getImplicitBounds(TypeVariable<?> typeVariable)` Returns an array containing the sole type of `Object` if `TypeVariable.getBounds()` returns an empty array.
`static Type[]` | `getImplicitLowerBounds(WildcardType wildcardType)` Returns an array containing a single value of `null` if `WildcardType.getLowerBounds()` returns an empty array.
`static Type[]` | `getImplicitUpperBounds(WildcardType wildcardType)` Returns an array containing the sole value of `Object` if `WildcardType.getUpperBounds()` returns an empty array.
`static Class<?>` | `getRawType(Type type, Type assigningType)` Get the raw type of a Java type, given its context.
`static Map<TypeVariable<?>,Type>` | `getTypeArguments(ParameterizedType type)` Retrieves all the type arguments for this parameterized type including owner hierarchy arguments such as `Outer.Inner.DeepInner` .
`static Map<TypeVariable<?>,Type>` | `getTypeArguments(Type type, Class<?> toClass)` Gets the type arguments of a class/interface based on a subtype.
`static boolean` | `isAssignable(Type type, Type toType)` Checks if the subject type may be implicitly cast to the target type following the Java generics rules.
`static boolean` | `isInstance(Object value, Type type)` Checks if the given value can be assigned to the target type following the Java generics rules.
`static Type[]` | `normalizeUpperBounds(Type[] bounds)` This method 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).
`static boolean` | `typesSatisfyVariables(Map<TypeVariable<?>,Type> typeVarAssigns)` Determines whether or not specified types satisfy the bounds of their mapped type variables.

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Constructor Detail

### TypeUtils

    public TypeUtils()

  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 Detail

### isAssignable

    public static boolean isAssignable(Type type,
                                       Type toType)

  Checks 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`.

### getTypeArguments

    public static Map<TypeVariable<?>,Type> getTypeArguments(ParameterizedType type)

  Retrieves all the type arguments for this parameterized type including owner hierarchy arguments such as `Outer.Inner.DeepInner` . 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

    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.

### determineTypeArguments

    public static Map<TypeVariable<?>,Type> determineTypeArguments(Class<?> cls,
                                                                   ParameterizedType superType)

  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>>`, the parameter of `TreeSet` is `? extends Map>`.

  Parameters:
    `cls` - the class whose type parameters are to be determined
    `superType` - the super type from which `cls`'s type arguments are to be determined
  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.

### isInstance

    public static boolean isInstance(Object value,
                                     Type type)

  Checks if the given value can be assigned to the target type following the Java generics rules.

  Parameters:
    `value` -
    `type` -
  Returns:
    true of `value` is an instance of `type`.

### normalizeUpperBounds

    public static Type[] normalizeUpperBounds(Type[] bounds)

  This method 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`.
  Returns:
    an array containing the values from `bounds` minus the redundant types.

### getImplicitBounds

    public static Type[] getImplicitBounds(TypeVariable<?> typeVariable)

  Returns 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
  Returns:
    a non-empty array containing the bounds of the type variable.

### getImplicitUpperBounds

    public static Type[] getImplicitUpperBounds(WildcardType wildcardType)

  Returns 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
  Returns:
    a non-empty array containing the upper bounds of the wildcard type.

### getImplicitLowerBounds

    public static Type[] getImplicitLowerBounds(WildcardType wildcardType)

  Returns 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
  Returns:
    a non-empty array containing the lower bounds of the wildcard type.

### typesSatisfyVariables

    public static boolean typesSatisfyVariables(Map<TypeVariable<?>,Type> typeVarAssigns)

  Determines whether or not specified types satisfy the bounds of their mapped type variables. When a type parameter extends another (such as ``), uses another as a type parameter (such as ``), or otherwise depends on another type variable to be specified, the dependencies must be included in `typeVarAssigns`.

  Parameters:
    `typeVarAssigns` - specifies the potential types to be assigned to the type variables.
  Returns:
    whether or not the types can be assigned to their respective type variables.

### getRawType

    public static Class<?> getRawType(Type type,
                                      Type assigningType)

  Get the raw type of a Java type, given its context. Primarily for use with `TypeVariable`s and `GenericArrayType`s, 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

Overview | Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

Copyright © 2001-2010 The Apache Software Foundation. All Rights Reserved.