I am trying to bind a bunch of types from a properties Map where the type of Key is String and the type of value in the map is String, Boolean, Int, Long, etc or even some type of object.
This is in scala but the same applies to java
class GuiceFlagsModule(
flags: Map[String, Any])
extends AbstractModule
with Logging {
def configure() {
for ((flagName, flag) <- flags) {
debug("Binding flag: " + flagName + " = " + flag)
val key2 = Key.get(value.getClass, new FlagImpl(flagName))
binder.bind(key2).toInstance(value)
}
}
}
The last line here does not compile
binder.bind(key2).toInstance(value)
How can I get this to compile so it binds the generic type correctly?
I just tried this out in java and it seems to work in intellij but then fails on javac compiler :(.
public void tryMe(Object someObj) {
Binder b = null;
Key k = Key.get(someObj.getClass(), new FlagImpl("name"));
b.bind(k).toInstance(someObj);
}
so how to do this in java or scala?
I managed to get it working in java like so
public static void bind(Binder binder, String flagName, Object value) {
Key<Object> key = Key.get((Class) value.getClass(), new FlagImpl(flagName));
binder.bind(key).toInstance(value);
}
I still do not have it working in scala
thanks, Dean