xiaodududu / google-guice

Automatically exported from code.google.com/p/google-guice
Apache License 2.0
0 stars 0 forks source link

Type-safe means to build parameterized TypeLiterals from existing TypeLiterals/class tokens #657

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Various framework code based on Guice needs to bind a parameterized type where 
some of the type arguments contain in-scope type variables, which obviously 
must be reified by existing type tokens of some kind.  A contrived example:

abstract class FancyModule extends AbstractModule {
  <T> AnnotatedBindingBuilder<List<T>> bindListOf(Class<T> classT) {
    TypeLiteral<List<T>> tl_LT = /* ??? */;
    return bind(tl_LT);
  }
}

One would like to just do:

    TypeLiteral<List<T>> tl_LT = new TypeLiteral<List<T>>() {};

but there is no way to pass in classT, so Guice will inevitably raise the error 
"List<T> cannot be used as a key; It is not fully specified" when it discovers 
the information is missing.  The following works, but is clunky and incurs an 
unchecked warning:

    TypeLiteral<List<T>> tl_LT = (TypeLiteral<List<T>>) \
      TypeLiteral.get(Types.newParameterizedType(List.class, classT));

I propose to introduce new variants of the TypeLiteral class that take 
additional type parameters and corresponding TypeLiteral tokens, like this:

abstract class TypeLiteralV1<T, A> extends TypeLiteral<T> {
    protected TypeLiteralV1(TypeLiteral<A> tokA) { ... }
}

    TypeLiteral<T> tl_T = TypeLiteral.get(classT);
    TypeLiteral<List<T>> tl_LT = new TypeLiteralV1<List<T>, T>(tlT) {};

The implementation then reads the anonymous subclass's extends declaration 
"TypeLiteralV1<List<T>, T>" to learn that T is reified by tl_T, and thus uses 
tl_T to construct a reification of List of whatever T actually is.

I have sample code for this construction from a Google internal project, and 
the code has just been approved for open-source release.  I will be submitting 
a patch based on this code to Guice.

Original issue reported on code.google.com by mattmccu...@google.com on 30 Sep 2011 at 10:37