Majid Hojati Majid Hojati - 2 months ago 7
Java Question

Java getDeclaredMethod() parameterTypes for string parameter

I have a simple question. I want to get

String.TYPE
as
parameterType
in
getDeclaredMethod()
but I can not find it.There is for example
Long.TYPE
for Long data type but there is not any thing similar for String type.
Can some one help me about this?
thanks

Object newClass;
newClass = Class.forName(cls.getName()).getConstructor().newInstance();
for (Method m : cls.getClass().getMethods())
if (m.getName().startsWith("get") && m.getParameterTypes().length == 0) {
try {
//final Object r = m.invoke(cls);
String MethodName=m.getName().replace("get","");

if (m.getReturnType().equals(Long.TYPE)){
// Class<?> c = Class.forName("class name");

}else
{
String value =this.getString(j,MethodName);
Method method = cls.getDeclaredMethod (MethodName, String);//Here I need Parameter Type for string imput parameter
method.invoke (newClass, value);
}


} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
// do your thing with r
}

Answer

Long.TYPE is for the primitive type long.

There is no such thing for reference types such as String.

You want String.class. (Long.class would be for the boxed type Long).

Comments