1.反射工具類,代碼如下
package com.pmo.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; /** * Created by ASUS on 2019/9/9. * 反射工具類 */ public class ReflectTool { private ReflectTool(){}; /** * 獲取字段對應值,並轉為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; } }
2.寫一個Persion類,和一個Students類測試
Persion代碼如下:
package com.pmo.vo; /** * Created by ASUS on 2019/9/9. */ public class Persion { private String name; private int age; public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
Students類代碼如下:
package com.pmo.vo; /** * Created by ASUS on 2019/9/9. */ public class Students extends Persion { private int score;//總分數 public int getScore() { return score; } public void setScore(int score) { this.score = score; } }
package com.pmo.vo; /** * Created by ASUS on 2019/9/9. */ public class Students extends Persion { private int score;//總分數 public int getScore() { return score; } public void setScore(int score) { this.score = score; } }
3.寫一個Test測試
package com.pmo.vo; import com.pmo.util.ReflectTool; import java.lang.reflect.Field; import java.util.List; /** * Created by ASUS on 2019/9/9. */ public class Test { public static void main(String[] args) throws ReflectiveOperationException { Students students = new Students(); students.setName("張三"); students.setAge(100); students.setScore(700); Field field = ReflectTool.getFieldByName("name", students.getClass()); System.out.println("反射獲取Field對象:" + field); String name1 = ReflectTool.getStringValue("name", students); System.out.println("反射獲取name值:" + name1); List<Field> fields = ReflectTool.listFields(students.getClass()); System.out.println("反射獲取所有filed,包括父類"+fields.toString()); } }
效果如下:
反射獲取Field對象:private java.lang.String com.pmo.vo.Persion.name
反射獲取name值:張三
反射獲取所有filed,包括父類[private int com.pmo.vo.Students.score, private java.lang.String com.pmo.vo.Persion.name, private int com.pmo.vo.Persion.age]