Class GenericType<T>

java.lang.Object
javax.ws.rs.core.GenericType<T>
Type Parameters:
T - the generic type parameter.

public class GenericType<T> extends Object
Represents a generic message entity type T. Supports in-line instantiation of objects that represent generic types with actual type parameters. An object that represents any parameterized type may be obtained by sub-classing GenericType. Alternatively, an object representing a concrete parameterized type can be created using a GenericType(java.lang.reflect.Type) and manually specifying the actual (parameterized) type.

For example:

  GenericType<List<String>> stringListType = new GenericType<List<String>>() {};
 

Or:

  public class MyGenericType extends GenericType<List<String>> { ... }

  ...

  MyGenericType stringListType = new MyGenericType();
 

Note that due to the Java type erasure limitations the parameterized type information must be specified on a subclass, not just during the instance creation. For example, the following case would throw an IllegalArgumentException:

  public class MyGenericType<T> extends GenericType<T> { ... }

  ...

  // The type is only specified on instance, not in a sub-class
  MyGenericType<List<String>> stringListType =
          new MyGenericType<List<String>>();
 
Since:
2.0
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final Class<?>
    The actual raw parameter type.
    private final Type
    Type represented by the generic type instance.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    Constructs a new generic type, deriving the generic type and class from type parameter.
     
    GenericType(Type genericType)
    Constructs a new generic type, supplying the generic type information and deriving the class.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
     
    private static Class
    Get Array class of component class.
    private static Class
    getClass(Type type)
    Returns the object representing the class or interface that declared the supplied type.
    final Class<?>
    Returns the object representing the class or interface that declared the type represented by this generic type instance.
    final Type
    Retrieve the type represented by the generic type instance.
    (package private) static Type
    getTypeArgument(Class<?> clazz, Class<?> baseClass)
    Return the value of the type parameter of GenericType<T>.
    int
     
     

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • type

      private final Type type
      Type represented by the generic type instance.
    • rawType

      private final Class<?> rawType
      The actual raw parameter type.
  • Constructor Details

    • GenericType

      protected GenericType()
      Constructs a new generic type, deriving the generic type and class from type parameter. Note that this constructor is protected, users should create a (usually anonymous) subclass as shown above.
      Throws:
      IllegalArgumentException - in case the generic type parameter value is not provided by any of the subclasses.
    • GenericType

      public GenericType(Type genericType)
      Constructs a new generic type, supplying the generic type information and deriving the class.
      Parameters:
      genericType - the generic type.
      Throws:
      IllegalArgumentException - if genericType is null or not an instance of Class or ParameterizedType whose raw type is an instance of Class.
  • Method Details

    • getType

      public final Type getType()
      Retrieve the type represented by the generic type instance.
      Returns:
      the actual type represented by this generic type instance.
    • getRawType

      public final Class<?> getRawType()
      Returns the object representing the class or interface that declared the type represented by this generic type instance.
      Returns:
      the class or interface that declared the type represented by this generic type instance.
    • getClass

      private static Class getClass(Type type)
      Returns the object representing the class or interface that declared the supplied type.
      Parameters:
      type - Type to inspect.
      Returns:
      the class or interface that declared the supplied type.
    • getArrayClass

      private static Class getArrayClass(Class c)
      Get Array class of component class.
      Parameters:
      c - the component class of the array
      Returns:
      the array class.
    • getTypeArgument

      static Type getTypeArgument(Class<?> clazz, Class<?> baseClass)
      Return the value of the type parameter of GenericType<T>.
      Parameters:
      clazz - subClass of baseClass to analyze.
      baseClass - base class having the type parameter the value of which we need to retrieve
      Returns:
      the parameterized type of GenericType<T> (aka T)
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object