------解决方案-------------------- 如果用反射: public class Test { public static void main(String[] args){ String className = "java.lang.Integer"; String value = "123"; try { Class c = Class.forName(className); Method m = c.getMethod("parseInt", value.getClass()); Integer i = (Integer)m.invoke(className.getClass() , value); System.out.println(i); } catch (Exception e) { e.printStackTrace(); } } 不用反射:Integer i = Integer.parseInt(value);
------解决方案-------------------- common-beanutils.jar里有个ConvertUtils.convert完全可以实现 以你的为例
Java code
for (String str : map.keySet()) {
String type = map.get(str);
try {
Class clazz = Class.forName(type);
Object returnObj = ConvertUtils.convert(str, clazz);
System.out.println(returnObj);
} catch (Exception e) {
e.printStackTrace();
}
}
------解决方案-------------------- Integer i = Integer.parseInt(value)
------解决方案--------------------
------解决方案-------------------- Class clazz = Class.forName("java.lang.Integer"); Object o = clazz.newIntance(); Field field = o.getField("value"); field.setAccessable(true); field.set(o, 123);