Re: how to get the return type of a Generic method
Sideswipe wrote:
This results in "class java.lang.Object" as I suspected it would (I
had to try). is there a way (without adding a parameter) to determine
this?? This is legacy code and while I have access to it, I have
limited flexibility as to what i can do.
If it's legacy code, how did they get the run type before? I think your
best bet is to rethink the problem...
However:
public class SomeClass<T> {
public T getValue(Object whatever) {
}
}
public static void main(String[] args) {
SomeClass<String> instance = new SomeClass<String>();
// For input use whatever works...
Object obj = instance.getVale( null );
Class class = obj.getClass()
System.out.println( class.getName() );
}
If you truly don't know, this is the only way I can think of, is to
actually obtain an object, and query it's class.
If that can't work, then I think you are going to have to refactor the
code and pass a class object in as a parameter when you create the class.