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);
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!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 likeKey<Set<TypeLiteral<T>>>
--
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 google...@googlegroups.com.
To unsubscribe from this group, send email to google-guice...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.
Awesome Stuart, thanks so much! I was able to get it to execute correctly, albeit with an unchecked cast and warning as below. Do you (or does anyone else) know of any way around this?
final Key<Set<T>> multibinderKey = (Key<Set<T>>) Key.get(Types.setOf( superClass ), annotation);
--
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/-/qYnPiVt-cqoJ.
FWIW, that's the only way I've been able to get it to work (class cast and warning).