public String getSimpleName(Type type){
//how?
}
I can't controll variable passed into getSimpleName(Type type)
except that it is instance of Type
.
Of course I know that I can use p.getType().getSimpleName()
in the following example, but it's not important to solve the following exmaple. what I want to show from it is that if the user (out of my control) passes p.getParameterizedType()
calling getSimpleName(Type type)
,he still can get correct result (i.e. List).
public class App {
public static void main(String[] args) throws NoSuchMethodException {
final Method m = App.class.getMethod("foo", List.class);
final Parameter p = m.getParameters()[0];
System.out.println(p.getParameterizedType());
}
public void foo(List<String> strings){
}
}
the above will print "java.util.List<java.lang.String>", but I want to get "List".
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.List;
public class App {
public static void main(String[] args) throws NoSuchMethodException {
final Method m = App.class.getMethod("foo", List.class);
final Parameter p = m.getParameters()[0];
System.out.println(((Class<?>)p.getParameterizedType()).getSimpleName());
}
public void foo(List<String> strings){
}
}
the above will throw java.lang.ClassCastException
.
Exception in thread "main" java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl cannot be cast to java.lang.Class
Thanks a lot.