I create a Preferences class and for the Getters I wan’t to use Runtime-Type Token.
So here is my getter method:
public <T> T get(String key, Class<T> clazz) {
// do some crazy stuff (e.g. Double <-> Float)
}
Up to that, everything works fine. But I would like that the class parameter will be optional.
boolean b = preferences.get(key);
So I add an additional method:
public <T> T get(String key) {
// return get(key, Class<T>);
}
Now the Question: Is there a way to do that? Is there a way to get an/the instance of Class<T>.
It’s possible with a small workaround:
public <T> T get(String key, T... args) {
return get(key, (Class<T>) args.getClass().getComponentType());
}
public <T> T get(String key, Class<T> clazz) {
System.out.println("key : " + key);
System.out.println("clazz: " + clazz);
}
// using
Boolean b = get("mykey");
It’s possible with a small workaround.
Jep I don’t like the varargs too, but it works so far.