On 29 Dec 2011, at 20:22, glenviewjeff wrote:
> Also posted on Stack Overflow.
>
> This code functions as expected:
>
> ArrayList<Class<? extends Option>> options =
> new ArrayList<Class<? extends Option>>();
> bindMultibinder(annotation, options);
>
> public Key<Set<Option>> bindMultibinder(
> Named annotation, ArrayList<Class<? extends Option>> contents) {
> Multibinder<Option> options =
> Multibinder.newSetBinder(binder(), Option.class, annotation);
> for (Class<? extends Option> option : contents) {
> options.addBinding().to(option);
> }
> final Key<Set<Option>> multibinderKey =
> Key.get(new TypeLiteral<Set<Option>>(){}, annotation);
> return multibinderKey;
> }
>
> But its generic equivalent (below) does not function. Attempting to use it
> results in the following error:
>
> com.google.inject.CreationException: Guice creation errors: 1)
> java.util.Set<T> cannot be used as a key; It is not fully specified.
>
> Generic equivalent:
>
> bindMultibinder(annotation, Option.class, options);
>
> public <T> Key<Set<T>> bindMultibinder(
> Named annotation, Class<T> superClass, ArrayList<Class<? extends T>>
> contents) {
> Multibinder<T> options =
> Multibinder.newSetBinder(binder(), superClass, annotation);
> for (Class<? extends T> t : contents) {
> options.addBinding().to(t);
> }
> final Key<Set<T>> multibinderKey =
> Key.get(new TypeLiteral<Set<T>>(){}, annotation);
^ the new TypeLiteral<...>(){} anonymous class trick only works when the type
parameter is known at compile time.
If you need to build generic types at runtime you can use the
com.google.inject.util.Types utility class, for example:
final Key<Set<T>> multibinderKey =
Key.get( Types.setOf( superClass ), annotation );
HTH
> return multibinderKey;
> }
>
>
> Guice's Key documentation states only that:
>
> Key supports generic types via subclassing just like TypeLiteral.
>
> I need more help than that to figure out how to replace the Key<Set<T>> with
> something like Key<Set<TypeLiteral<T>>>
>
> Thanks for any help!
>
> --
> You received this message because you are subscribed to the Google Groups
> "google-guice" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/google-guice/-/N7baUuZY8MsJ.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/google-guice?hl=en.
--
You received this message because you are subscribed to the Google Groups
"google-guice" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-guice?hl=en.