If you know which genericType's you want to register, then you may be able
to use the static method static TypeLiteral<?> TypeLiteral.get(Type type);
You can construct your Type manually. I've done this before to turn Scala's
Manifest objects into Type for jackson to use. It looked like this:


  private[this] def typeReference[T: Manifest] = new TypeReference[T] {
    override def getType = typeFromManifest(manifest[T])
  }

  private[this] def typeFromManifest(m: Manifest[_]): Type = {
    if (m.typeArguments.isEmpty) {m.erasure}
    else {
      new ParameterizedType {
        def getRawType = m.erasure
        def getActualTypeArguments =
m.typeArguments.map(typeFromManifest).toArray
        def getOwnerType = null
      }
    }
  }

So, your raw Type would be the interface or the implementation class, the
actual type arguments is an array of Type (in case your generic class is
further parameterized).

It might help to better understand your real use case -- like how the user
indicates how he wants to use which generic types.


On Sun, Feb 16, 2014 at 1:35 AM, david <[email protected]> wrote:

> Hi there,
>
> I'm new to Guice and I'm looking for a way to bind or inject Generic types
> which, extend a known type, but are themselves unknown at compile time.
>
> For example, given the a base generic type like:
>
>            class BaseGenericType{}
>
> and the following derived types:
>
>     class GenericTypeA extends BaseGenericType{}
>     class GenericTypeB extends BaseGenericType{}
>     class GenericTypeC extends GenericTypeB{}
>
>
> when someone registers a binding which has a generic parameter of a
> derived type
>     bind(new TypeLiteral<someInterface<GenericTypeC>>() {
>         }).to(new TypeLiteral<someImplementation<GenericTypeC>>() {
>         });
>
> (in this case GenericTypeC extends GenericTypeB which extends
> BaseGenericType)
>
>
> I would like to automatically create a binding:
>     bind(new TypeLiteral<myInterface<GenericTypeC>>() {
>         }).to(new TypeLiteral<myImplementation<GenericTypeC>>() {
>         });
>
>
>
> here is the sudo code for what i'm looking to accomplish.
>
> if(type.getType() instanceof ParameterizedType)
> {
> Type[] genericTypes = ((ParameterizedType)
> type.getType()).getActualTypeArguments();
> for(Type genericType: genericTypes)
> {
>     if (BaseGenericType.class.isAssignableFrom((Class<?>) genericType)) {
>
>
>         TypeLiteral<myInterface<genericType>> typeLiteral = new
> TypeLiteral<myInterface<genericType>>();
>         TypeLiteral<myImplementation<genericType>> typeLiteral2 =
> (TypeLiteral<myImplementation<genericType>>();
>
>         bind(typeLiteral).to(typeLiteral2);
>
>     }
> }
>
>
> Thanks for your help,
> David
>
>  --
> You received this message because you are subscribed to the Google Groups
> "google-guice" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/google-guice.
> For more options, visit https://groups.google.com/groups/opt_out.
>

-- 
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-guice.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to