I'm attempting to create a reusable class that can be used to update many
different types. The updater looks like this:
public class Updater<T extends Updatable> implements Runnable {
private final Updatable target;
@Inject
public Updater(T target) {
this.target = target;
}
public void run() {
target.update();
}
}
These Updaters are called via a scheduler. The scheduler takes Runnables but
knows nothing of Guice. Therefore, I create a class called InjectableRunnable
that has the Injector and a Key. It looks like this:
public class InjectableRunnable<T extends Runnable> implements Runnable {
private final Injector injector;
private final Key<T> key;
public InjectableRunnable(Injector injector, Key<T> key) {
this.injector = injector;
this.key = key;
}
public void run() {
Runnable instance = injector.getInstance(key);
instance.run();
}
}
Then I add this to the scheduler like so:
scheduler. addRecurringTask(
new InjectableRunnable<Updater<SomeUpdatable>>(injector, new
Key<Updater<SomeUpdatable>>() {})
);
However, the runtime types aren't available and I'm getting the usual expcetion:
Caused by: java.lang.AssertionError: Unexpected type. Expected:
java.lang.reflect.ParameterizedType, got:
sun.reflect.generics.reflectiveObjects.TypeVariableImpl, for type literal: T.
I can't seem to figure out how to get the generics to take. There are a couple
ways I can refactor to remove the generics and get it working, but I really
wanted to see if I could get the generics going. Anyone have the fix?
Thanks,
-bp
--
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.