package com.moral.util;
|
|
import java.lang.reflect.Field;
|
|
/**
|
* @ClassName ClassUtils
|
* @Description TODO
|
* @Author 陈凯裕
|
* @Date 2021/9/29 11:11
|
* @Version TODO
|
**/
|
public class ClassUtils {
|
|
/**
|
* @Description: 获取对象的属性值
|
* @Param: [obj, propertyName]
|
* @return: java.lang.Object
|
* @Author: 陈凯裕
|
* @Date: 2021/9/29
|
*/
|
public static Object getPropertyValue(Object obj, String propertyName) {
|
Class<?> Clazz = obj.getClass();
|
Field field;
|
if ((field = getField(Clazz, propertyName)) == null)
|
return null;
|
field.setAccessible(true);
|
try {
|
Object o = field.get(obj);
|
return o;
|
} catch (IllegalAccessException e) {
|
return null;
|
}
|
}
|
|
private static Field getField(Class<?> clazz, String propertyName) {
|
if (clazz == null)
|
return null;
|
try {
|
return clazz.getDeclaredField(propertyName);
|
} catch (NoSuchFieldException e) {
|
return getField(clazz.getSuperclass(), propertyName);
|
}
|
}
|
|
}
|