for(Field f:fields)
{
f.setAccessible(true);
try {
f.set(obj,element.attributeValue(f.getName()) );
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}java反射field

解决方案 »

  1.   

    错误如下
    java.lang.IllegalArgumentException: Can not set int field reflection.Test.ia to java.lang.String
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
    at sun.reflect.UnsafeIntegerFieldAccessorImpl.set(Unknown Source)
      

  2.   

    我看了很多,基本解决的办法都是构建一个类型转换函数,在这个函数中,与已有的类型比较,也挺麻烦的。我想是既然有了f。Class,就能判断这个类型了。应该有简介统一的解决方法吧。
      

  3.   

    Can not set int field reflection.Test.ia to java.lang.String不能把int反射成string
      

  4.   

    JDK文档中关于Field类的set方法抛出IllegalArgumentException异常的解释:IllegalArgumentException - if the specified object is not an instance of the class or interface declaring the underlying field (or a subclass or implementor thereof), or if an unwrapping conversion fails.