Java通过反射得到其属性的值包含父类属性

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;

}

}www.lekaowang.com.cn/qga/qys/dvz/

上一篇:Java通过反射得到其属性的值包含父类属性


下一篇:python – MongoDb:$按$in排序