public class ObjectUtils {
private static final Logger LOG = LoggerFactory.getLogger(ObjectUtils.class);
/**
* 反射取得bean中对应属性的值
* @param bean
* @param fieldName
* @return
*/
public static Object getProperty(Object bean, String fieldName) {
Object obj = getProperty(bean.getClass(),bean,fieldName);
return obj;
}
private static Object getProperty(Class _class,Object bean,String fieldName){
Object obj = null;
Field[] fields = _class.getDeclaredFields();
Field.setAccessible(fields, true);
for (int i = 0; i < fields.length; i++) {
Field field = fields[i];
if (fieldName.equals(field.getName())) {
try {
obj = field.get(bean);
} catch (IllegalArgumentException e) {
LOG.error(e.getMessage());
} catch (IllegalAccessException e) {
LOG.error(e.getMessage());
}
break;
}
}
if(obj == null && _class.getGenericSuperclass()!=null){
obj = getProperty(_class.getSuperclass(), bean, fieldName);
}
return obj;
}
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。