Package javax.ws.rs.core
Class GenericType<T>
- java.lang.Object
-
- javax.ws.rs.core.GenericType<T>
-
- Type Parameters:
T- the generic type parameter.
public class GenericType<T> extends java.lang.ObjectRepresents a generic message entity typeT. 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-classingGenericType. Alternatively, an object representing a concrete parameterized type can be created using aGenericType(java.lang.reflect.Type)and manually specifying theactual (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
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedGenericType()Constructs a new generic type, deriving the generic type and class from type parameter.GenericType(java.lang.reflect.Type genericType)Constructs a new generic type, supplying the generic type information and deriving the class.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanequals(java.lang.Object obj)static GenericTypeforInstance(java.lang.Object instance)Create ageneric typefrom a Javainstance.private static java.lang.ClassgetArrayClass(java.lang.Class c)Get Array class of component class.private static java.lang.ClassgetClass(java.lang.reflect.Type type)Returns the object representing the class or interface that declared the suppliedtype.java.lang.Class<?>getRawType()Returns the object representing the class or interface that declared the type represented by this generic type instance.java.lang.reflect.TypegetType()Retrieve the type represented by the generic type instance.(package private) static java.lang.reflect.TypegetTypeArgument(java.lang.Class<?> clazz, java.lang.Class<?> baseClass)Return the value of the type parameter ofGenericType<T>.inthashCode()java.lang.StringtoString()
-
-
-
Constructor Detail
-
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:
java.lang.IllegalArgumentException- in case the generic type parameter value is not provided by any of the subclasses.
-
GenericType
public GenericType(java.lang.reflect.Type genericType)
Constructs a new generic type, supplying the generic type information and deriving the class.- Parameters:
genericType- the generic type.- Throws:
java.lang.IllegalArgumentException- if genericType isnullor not an instance ofClassorParameterizedTypewhose raw type is an instance ofClass.
-
-
Method Detail
-
forInstance
public static GenericType forInstance(java.lang.Object instance)
Create ageneric typefrom a Javainstance.If the supplied instance is a
GenericEntity, the generic type will be computed using theGenericEntity.getType(). Otherwiseinstance.getClass()will be used.- Parameters:
instance- Java instance for which theGenericTypedescription should be created.- Returns:
GenericTypedescribing the Javainstance.- Since:
- 2.1
-
getType
public final java.lang.reflect.Type getType()
Retrieve the type represented by the generic type instance.- Returns:
- the actual type represented by this generic type instance.
-
getRawType
public final java.lang.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 java.lang.Class getClass(java.lang.reflect.Type type)
Returns the object representing the class or interface that declared the suppliedtype.- Parameters:
type-Typeto inspect.- Returns:
- the class or interface that declared the supplied
type.
-
getArrayClass
private static java.lang.Class getArrayClass(java.lang.Class c)
Get Array class of component class.- Parameters:
c- the component class of the array- Returns:
- the array class.
-
getTypeArgument
static java.lang.reflect.Type getTypeArgument(java.lang.Class<?> clazz, java.lang.Class<?> baseClass)Return the value of the type parameter ofGenericType<T>.- Parameters:
clazz- subClass ofbaseClassto 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(java.lang.Object obj)
- Overrides:
equalsin classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-