Reputation: 165
I need to configure bindings for classes which are not known at compile time. Additionally, these classes have to be loaded by different class loaders. The following snippet roughly describes my intentions:
Classloader cl = findClassloader(...);
Class<?> key = cl.loadClass(keyClassName);
Class<?> impl = cl.loadClass(implClassName);
now, I'd like to simply bind the key class to the impl class like in
bind(key).to(impl);
However, as by the wildcarded Class types, this does not work. Simply telling Guice the full full qualified class names (I guess there is a mechanism for this to load bindings from property files) would not work either, as guice does not know which class loader to use.
Upvotes: 1
Views: 512
Reputation: 95614
Generics are a great tool to ensure that you're doing the right thing, but they're not set up for complicated cases like this. Luckily, the generics aren't remotely necessary here, and they'll be erased upon compilation anyway.
Try this:
bind(key).to((Class) impl);
Or wrap it in a warning-suppressed wrapper:
@SuppressWarnings({"rawtypes"})
void bindUnsafely(Class<?> key, Class<?> impl) {
bind(key).to((Class) impl);
}
Upvotes: 2