public final class

TypeResolver

extends Object
java.lang.Object
   ↳ com.sun.beans.TypeResolver

Class Overview

This is utility class to resolve types.

Summary

Public Methods
static Class[] erase(Type[] types)
Converts all types in the given array to the corresponding classes.
static Class<?> erase(Type type)
Converts the given type to the corresponding class.
static Type resolve(Type actual, Type formal)
Replaces type variables of the given formal type with the types they stand for in the given actual type.
static Type[] resolve(Type actual, Type[] formals)
Replaces type variables of all formal types in the given array with the types they stand for in the given actual type.
static Type resolveInClass(Class<?> inClass, Type type)
Replaces the given type in an inherited method with the actual type it has in the given inClass.
static Type[] resolveInClass(Class<?> inClass, Type[] types)
Replaces all types in the given array with the actual types they have in the given inClass.
[Expand]
Inherited Methods
From class java.lang.Object

Public Methods

public static Class[] erase (Type[] types)

Converts all types in the given array to the corresponding classes.

Parameters
types the array of types to convert
Returns
  • an array of corresponding classes
See Also

public static Class<?> erase (Type type)

Converts the given type to the corresponding class. This method implements the concept of type erasure, that is described in section 4.6 of Java Language Specification.

Parameters
type the array of types to convert
Returns
  • a corresponding class

public static Type resolve (Type actual, Type formal)

Replaces type variables of the given formal type with the types they stand for in the given actual type.

A ParameterizedType is a class with type parameters, and the values of those parameters. For example, Map<K,V> is a generic class, and a corresponding ParameterizedType might look like Map<K=String,V=Integer>. Given such a ParameterizedType, this method will replace K with String, or List<K> with List<String;, or List<? super K> with List<? super String>.

The actual argument to this method can also be a Class. In this case, either it is equivalent to a ParameterizedType with no parameters (for example, Integer.class), or it is equivalent to a "raw" ParameterizedType (for example, Map.class). In the latter case, every type parameter declared or inherited by the class is replaced by its "erasure". For a type parameter declared as <T>, the erasure is Object. For a type parameter declared as <T extends Number>, the erasure is Number.

Although type parameters are not inherited by subclasses in the Java language, they are effectively inherited when using reflection. For example, if you declare an interface like this...

 public interface StringToIntMap extends Map<String,Integer> {}
 

...then StringToIntMap.class.getMethods() will show that it has methods like put(K,V) even though StringToIntMap has no type parameters. The K and V variables are the ones declared by Map, so getGenericDeclaration() will return Map.class.

For this reason, this method replaces inherited type parameters too. Therefore if this method is called with actual being StringToIntMap.class and formal being the K from Map, it will return String.class.

In the case where actual is a "raw" ParameterizedType, the inherited type parameters will also be replaced by their erasures. The erasure of a Class is the Class itself, so a "raw" subinterface of StringToIntMap will still show the K from Map as String.class. But in a case like this...

 public interface StringToIntListMap extends Map<String,List<Integer>> {}
 public interface RawStringToIntListMap extends StringToIntListMap {}
 

...the V inherited from Map will show up as List<Integer> in StringToIntListMap, but as plain List in RawStringToIntListMap.

Parameters
actual the type that supplies bindings for type variables
formal the type where occurrences of the variables in actual will be replaced by the corresponding bound values
Returns
  • a resolved type
See Also
  • #TypeResolver(Type)
  • #resolve(Type)

public static Type[] resolve (Type actual, Type[] formals)

Replaces type variables of all formal types in the given array with the types they stand for in the given actual type.

Parameters
actual the type that supplies bindings for type variables
formals the array of types to resolve
Returns
  • an array of resolved types
See Also
  • #TypeResolver(Type)
  • #resolve(Type[])

public static Type resolveInClass (Class<?> inClass, Type type)

Replaces the given type in an inherited method with the actual type it has in the given inClass.

Although type parameters are not inherited by subclasses in the Java language, they are effectively inherited when using reflection. For example, if you declare an interface like this...

 public interface StringToIntMap extends Map<String,Integer> {}
 

...then StringToIntMap.class.getMethods() will show that it has methods like put(K,V) even though StringToIntMap has no type parameters. The K and V variables are the ones declared by Map, so getGenericDeclaration() will return Map.class.

The purpose of this method is to take a Type from a possibly-inherited method and replace it with the correct Type for the inheriting class. So given parameters of K and StringToIntMap.class in the above example, this method will return String.

Parameters
inClass the base class used to resolve
type the type to resolve
Returns
  • a resolved type
See Also

public static Type[] resolveInClass (Class<?> inClass, Type[] types)

Replaces all types in the given array with the actual types they have in the given inClass.

Parameters
inClass the base class used to resolve
types the array of types to resolve
Returns
  • an array of resolved types
See Also