I'm looking for a way, or a correct syntax, for 'genericizing' the
class instantiation via Class#newInstance() method using reflection.
The code shown below is an half-baked solution in that compiler emits
unchecked cast warning against the cast on the 'obj' variable at the
last part of the code.
I think I have tried everyting conceivable for Class variable declarations
and other part of the code but they were all futile. I lost one night
sleep for that.
If there are Java Generics gurus on the forum, please help!
TIA.
import java.util.*;
import java.lang.reflect.*;
public class GetMethod{
public static void main(String[] args) {
Object obj = null;
Class<Hashtable> clazz = Hashtable.class;
Class<Class> claxx = Class.class;
try {
Method method = claxx.getMethod("newInstance");
obj = method.invoke(clazz);
}
catch (Exception e) {
e.printStackTrace();
}
Hashtable<String,String> ht = (Hashtable<String,String>)obj;
ht.put("foo", "bar");
System.out.println(ht.get("foo"));
}
}