dev@jsr311.java.net

TypeLiteral

From: Marc Hadley <Marc.Hadley_at_Sun.COM>
Date: Tue, 03 Jun 2008 13:40:39 -0400

Type erasure creates an issue for resource method return values. E.g.
given:

@GET
List<Bean> getList() {
   List<Bean> results = ...;
   return results;
}

we can determine the generic return type and find a suitable message
body writer. However, if the method needs to add additional metadata
then it has to return an instance of Response like this:

@GET
Response getList() {
   List<Bean> results = ...;
   return Response.ok().entity(results).cacheControl(...).build();
}

The problem is that because of type erasure we lose the type of list
that forms the entity. To get round this we'd like to add new methods
to ResponseBuilder that include a parameter that provides the missing
information and add a corresponding method to Response to extract that
information.

Google Guice includes a TypeLiteral[1] class that seems to offer
everything we need and Bob Lee (Google) has kindly agreed to donate it
for inclusion in this JSR. Usage would look something like this:

@GET
Response getList() {
   List<Bean> results = ...;
   TypeLiteral<List<Bean>> listOfBean = new TypeLiteral<List<Bean>>()
{};
   return
Response
.ok().entity(results).genericType(listOfBean).cacheControl(...).build();
}
Thoughts, comments ?

Marc.

[1] http://google-guice.googlecode.com/svn/trunk/src/com/google/inject/TypeLiteral.java

---
Marc Hadley <marc.hadley at sun.com>
CTO Office, Sun Microsystems.