我有一個簡單的問題。我想要String.TYPE
作爲parameterType
在getDeclaredMethod()
但我找不到它。例如對於Long數據類型有Long.TYPE
,但對於String類型沒有任何類似的東西。 有人可以幫助我嗎? 感謝Java getDeclaredMethod()parameterTypes字符串參數
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
}
我會建議使用.class,將工作。您不必進入.TYPE。 – cody123