即使值存储在字符串中,如何使 ConstructorUtils.invokeConstructor 工作?
这是代码:
public static void main(String[] args) {
try {
String type = "java.lang.Character";
//char value = 'c'; //this work
String value = "c"; // this not work,throws[java.lang.NoSuchMethodException: No such accessible constructor on object: java.lang.Character]
Class<?> clazz = ClassUtils.getClass(type);
Object instance = ConstructorUtils.invokeConstructor(clazz, value);
System.out.println(instance);
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | InstantiationException
| ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}