Top Description Fields Constructors Methods
javax.ws.rs.core

public Class GenericEntity<T>

extends Object
Class Inheritance
Type Parameters
<T>
response entity instance type
Imports
java.lang.reflect.GenericArrayType, .ParameterizedType, .Type

Represents a message entity of a generic type T.

Normally type erasure removes generic type information such that a Response instance that contains, e.g., an entity of type List<String> appears to contain a raw List<?> at runtime. When the generic type is required to select a suitable javax.ws.rs.ext.MessageBodyWriter, this class may be used to wrap the entity and capture its generic type.

There are two ways to create an instance:

  1. Create a (typically anonymous) subclass of this class which enables retrieval of the type information at runtime despite type erasure. For example, the following code shows how to create a Response containing an entity of type List<String> whose generic type will be available at runtime for selection of a suitable javax.ws.rs.ext.MessageBodyWriter:
    List<String> list = new ArrayList<String>();
    GenericEntity<List<String>> entity = new GenericEntity<List<String>>(list) {};
    Response response = Response.ok(entity).build();

    where list is the instance of List<String> that will form the response body and entity is an instance of an anonymous subclass of GenericEntity.

  2. Create an instance directly by supplying the generic type information with the entity. For example the following code shows how to create a response containing the result of a method invoked via reflection:
    Method method = ...;
    GenericEntity<Object> entity = new GenericEntity<Object>(
       method.invoke(...), method.getGenericReturnType());
    Response response = Response.ok(entity).build();
  3. The above obtains the generic type from the return type of the method, the raw type is the class of entity.

Authors
Paul Sandoz, Marc Hadley
Since
1.0
See Also
GenericType

Field Summary

Modifier and TypeField and Description
private final T
private final Class<?>
private final Type

Constructor Summary

AccessConstructor and Description
protected
GenericEntity(final T
the entity instance, must not be null.
entity
)

Constructs a new generic entity.

public
GenericEntity(final T
the entity instance, must not be null.
entity
,
final Type
the generic type, must not be null.
genericType
)

Create a new instance of GenericEntity, supplying the generic type information.

Method Summary

Modifier and TypeMethod and Description
private void
checkTypeCompatibility(final Class<?> c, final Type t)

public boolean
equals(Object
the reference object with which to compare.
obj
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.
public final T

Returns:

the enclosed entity.
getEntity
()

Get the enclosed entity.

public final Class<?>

Returns:

the raw type.
getRawType
()

Gets the raw type of the enclosed entity.

public final Type

Returns:

the type
getType
()

Gets underlying Type instance.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for the object.
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

entityback to summary
private final T entity
rawTypeback to summary
private final Class<?> rawType
typeback to summary
private final Type type

Constructor Detail

GenericEntityback to summary
protected GenericEntity(final T entity)

Constructs a new generic entity. Derives represented class from type parameter. Note that this constructor is protected, users should create a (usually anonymous) subclass as shown above.

Parameters
entity:T

the entity instance, must not be null.

Exceptions
IllegalArgumentException:
if entity is null.
GenericEntityback to summary
public GenericEntity(final T entity, final Type genericType)

Create a new instance of GenericEntity, supplying the generic type information. The entity must be assignable to a variable of the supplied generic type, e.g. if entity is an instance of ArrayList<String> then genericType could be the same or a superclass of ArrayList with the same generic type like List<String>.

Parameters
entity:T

the entity instance, must not be null.

genericType:Type

the generic type, must not be null.

Exceptions
IllegalArgumentException:
if the entity is not assignable to a variable of the supplied generic type or if entity or genericType is null.

Method Detail

checkTypeCompatibilityback to summary
private void checkTypeCompatibility(final Class<?> c, final Type t)
equalsback to summary
public boolean equals(Object obj)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Parameters
obj:Object

the reference object with which to compare.

Returns:boolean

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
getEntityback to summary
public final T getEntity()

Get the enclosed entity.

Returns:T

the enclosed entity.

getRawTypeback to summary
public final Class<?> getRawType()

Gets the raw type of the enclosed entity. Note that this is the raw type of the instance, not the raw type of the type parameter. I.e. in the example in the introduction, the raw type is ArrayList not List.

Returns:Class<?>

the raw type.

getTypeback to summary
public final Type getType()

Gets underlying Type instance. Note that this is derived from the type parameter, not the enclosed instance. I.e. in the example in the introduction, the type is List<String> not ArrayList<String>.

Returns:Type

the type

hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object.

Annotations
@Override
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.

Annotations
@Override