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 jakarta.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:
Response containing an entity of
type List<String> whose generic type will be available at runtime for selection of a suitable
jakarta.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.
Method method = ...; GenericEntity<Object> entity = new GenericEntity<Object>( method.invoke(...), method.getGenericReturnType()); Response response = Response.ok(entity).build();
The above obtains the generic type from the return type of the method, the raw type is the class of entity.
GenericType
| Modifier and Type | Field and Description |
|---|---|
| private final T | |
| private final Class | |
| private final Type |
| Access | Constructor and Description |
|---|---|
| protected | |
| public | GenericEntity(final T
the entity instance, must not be entity, final Type null.the generic type, must not be genericType)null.Create a new instance of GenericEntity, supplying the generic type information. |
| Modifier and Type | Method and Description |
|---|---|
| private void | |
| public boolean | equals(final Object
the reference object with which to compare. obj)Overrides java. |
| public final T | |
| public final Class | |
| public final Type | |
| public int | |
| public String |
| entity | back to summary |
|---|---|
| private final T entity | |
| rawType | back to summary |
|---|---|
| private final Class<?> rawType | |
| type | back to summary |
|---|---|
| private final Type type | |
| GenericEntity | back 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.
| |
| GenericEntity | back 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
| |
| checkTypeCompatibility | back to summary |
|---|---|
| private void checkTypeCompatibility(final Class<?> c, final Type t) | |
| equals | back to summary |
|---|---|
| public boolean equals(final Object obj) Overrides java. Doc from java. Indicates whether some other object is "equal to" this one.
The
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. | |
| getEntity | back to summary |
|---|---|
| public final T getEntity() Get the enclosed entity.
| |
| getRawType | back to summary |
|---|---|
| public final Class 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
| |
| getType | back to summary |
|---|---|
| public final Type getType() Gets underlying
| |
| hashCode | back to summary |
|---|---|
| public int hashCode() Overrides java. Doc from java. Returns a hash code value for the object. This method is
supported for the benefit of hash tables such as those provided by
The general contract of
| |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Doc from java. Returns a string representation of the object. | |