util之ReflectUtil反射工具類
阿新 • • 發佈:2020-11-18
package com.xxx.util; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.math.BigDecimal; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import java.util.List; /** * * 反射工具類 */ public class ReflectUtil {private ReflectUtil(){}; /** * 獲取欄位對應值,並轉為String型別,空值返回空字串 * @param fieldName * @param obj * @return */ public static synchronized String getStringValue(String fieldName,Object obj) throws ReflectiveOperationException{ Object objectValue = getValueByGetter(fieldName,obj);if (objectValue == null){ return ""; } String result = objectValue.toString(); //如果型別為BigDecimal,去掉末尾的0 if (objectValue instanceof BigDecimal){ BigDecimal value = (BigDecimal) objectValue; value = value.stripTrailingZeros(); result= value.toPlainString(); }else if (objectValue instanceof Date){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); result = sdf.format((Date)objectValue).replace(" 00:00:00", ""); } return result.trim(); } public static Object getValueByGetter (String fieldName,Object obj) throws ReflectiveOperationException { Method getter = getGetter(fieldName, obj.getClass()); if (getter != null){ return getter.invoke(obj); } return null; } public static Object setValueBySetter (String fieldName,Object obj) throws ReflectiveOperationException { Method setter = getSetter(fieldName, obj.getClass()); if (setter == null){ throw new ReflectiveOperationException("沒有set方法"); } return setter.invoke(obj); } /** * 獲取get方法 * @param fieldName * @param cls * @return */ public static Method getGetter(String fieldName,Class<?> cls){ for (Method method : cls.getMethods()) { if (method.getName().equalsIgnoreCase("get".concat(fieldName)) && method.getParameterTypes().length == 0){ return method; } } return null; } /** * 獲取set方法 * @param fieldName * @param cls * @return */ public static Method getSetter(String fieldName,Class<?> cls){ for (Method method : cls.getMethods()) { if (method.getName().equalsIgnoreCase("set".concat(fieldName)) && method.getParameterTypes().length == 0){ return method; } } return null; } /** * 通過屬性名獲取Field物件 * @param fieldName * @param cls * @return */ public static synchronized Field getFieldByName(String fieldName,Class<?> cls){ Field[] fields =cls.getDeclaredFields(); for (Field field : fields){ if (field.getName().equals(fieldName)){ return field; } } if (cls.getSuperclass() != null){ return getFieldByName(fieldName,cls.getSuperclass()); } return null; } /** * 通過物件.class獲取所有Fields,包括父類 * @param cls * @return */ public static List<Field> listFields(Class<?> cls){ Field[] fs = cls.getDeclaredFields(); List<Field> fields = new ArrayList<>(Arrays.asList(fs)); if (cls.getSuperclass() !=null){ fields.addAll(listFields(cls.getSuperclass())); } return fields; } public static boolean fieldExist(String fieldName,Class<?> cls){ return getFieldByName(fieldName, cls) !=null; } }
本文摘自
https://www.cnblogs.com/kdx-2/p/11491780.html