I have this method that I need to get V
type information:
@Override
public V get(String key) {
Preconditions.checkNotNull(key, "Null id object");
Map<String, Object> map = null;
String oldNamespace = NamespaceManager.get();
NamespaceManager.set(_namespace);
try {
map = store().get(KeyStructure.createKey("V.class.getName()", key)); // here
} catch (Exception e) {
// TODO Handle exception
e.printStackTrace();
} finally {
if (oldNamespace != null)
NamespaceManager.set(oldNamespace);
}
return (V) Mapper.createTObject(V.class, map); // and here
}
Considering that I need to keep the get
method parameters just that.
I think this is possible because at compile time the method will be used:
KVS<String,Friend> kvs = new KVS<String,Friend>();
Friend f = kvs.get("user.123");
Then V
would be of class Friend
Update:
I tried:
public class StringObjectKeyValueStore<V> extends AbstractObjectStore<V>
implements KeyValueStore<String, V> {
}
public abstract class AbstractObjectStore<V> {
private final Class<V> type;
public AbstractObjectStore(String namespace, String collection) {
this.type = (Class<V>) ((ParameterizedType) getClass()
.getGenericSuperclass()).getActualTypeArguments()[0];
}
However it throws:
java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.TypeVariableImpl cannot be cast to java.lang.Class