代码:
ObjectUtil类:
import java.lang.reflect.Field; import java.math.BigDecimal; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; /** * 利用Java反射机制对实体类的常用操作 * @author 【】 * */ public class ObjectUtil { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat sfCsv = new SimpleDateFormat("yyyyMMdd"); /** * 根据类的名称获取类的属性信息(不包括父类) */ public List<Field> getFields(String className) throws ClassNotFoundException { List<Field> list = new ArrayList<>(); Class<?> clazz = Class.forName(className); Field[] fields = clazz.getDeclaredFields(); list.addAll(Arrays.asList(fields)); return list; } /** * 根据类的名称获取属性信息和父类的属性信息 */ public List<Field> getAllFields(String className) throws ClassNotFoundException { List<Field> list = new ArrayList<>(); Class<?> clazz = Class.forName(className); Field[] fields = clazz.getDeclaredFields(); list.addAll(Arrays.asList(fields)); Class<?> superClazz = clazz.getSuperclass(); if (superClazz != null) { Field[] superFields = superClazz.getDeclaredFields(); list.addAll(Arrays.asList(superFields)); } return list; } /** * 根据类名和字段名获取字段在类中的类型 */ public String getFieldClassName(String className ,String fieldName) throws ClassNotFoundException { Class<?> clazz = Class.forName(className); Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { if (field.getName().equals(fieldName)) { return field.getType().getSimpleName(); } } return ""; } /** * 获取传入对象的指定属性的值 */ public String getValueByPropName(Object obj, String propName) { String value = null; try { // 获取对象的属性 Field field = obj.getClass().getDeclaredField(propName); // 对象的属性的访问权限设置为可访问 field.setAccessible(true); value = field.get(obj).toString(); } catch (Exception e) { return null; } return value; } /** * 根据传入对象获取其属性、类型、属性值的集合 */ public List<Map<String, Object>> getFiledsInfo(Object o) { Field[] fields = o.getClass().getDeclaredFields(); List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); Map<String, Object> infoMap = null; for (int i = 0; i < fields.length; i++) { infoMap = new HashMap<String, Object>(); infoMap.put("type", fields[i].getType().toString()); infoMap.put("name", fields[i].getName()); infoMap.put("value", getValueByPropName(o, fields[i].getName())); list.add(infoMap); } return list; } /** * 根据类的名称获取该类和该注解值的map集合(包括父类) * (注:注解Comment是为代码完整自定义的注解,可根据实际需求要获取的注解直接替换即可) */ public Map<String, String> getAnnotationVal(String className) { Map<String, String> map = new HashMap<>(); try { List<Field> lists = getAllFields(className); for (Field field : lists) { // 设置访问对象权限,保证对私有属性的访问 field.setAccessible(true); // 判断该字段是否存在@Comment注解 if (!field.isAnnotationPresent(Comment.class)) { continue; } // 获取字段中对应的注解@Comment对象 Comment comment = field.getAnnotation(Comment.class); // 获取自定义注解的值 String value = comment.value(); if (value != null && !"".equals(value)) { map.put(field.getName(), value); } } } catch (ClassNotFoundException e) { e.printStackTrace(); } return map; } /** * 判断该类是否存在该属性(不包括父类) */ public boolean existsField(String className, String fieldName) throws ClassNotFoundException { boolean flag = false; Class<?> clazz = Class.forName(className); Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { if (field.getName().equals(fieldName)) { flag = true; break; } } return flag; } /** * 给该类的该字段设值 */ public void setFieldValue(String fieldName, String fieldValue, Object object) { Field field = null; try { boolean isExist = false; Field[] fields = object.getClass().getDeclaredFields(); for (Field var : fields) { if (var.getName().equals(fieldName)) { isExist = true; field = object.getClass().getDeclaredField(fieldName); break; } } if (!isExist) { field = object.getClass().getSuperclass().getDeclaredField(fieldName); } // 设置对象的访问权限,保证对private的属性的访问 field.setAccessible(true); if ("class java.math.BigDecimal".equals(field.getType().toString())) { field.set(object, new BigDecimal(fieldValue.trim())); } else if ("Date".equals(field.getType().getSimpleName())) { if (fieldValue.toString().indexOf("/") > 0) { fieldValue = fieldValue.toString().replace('/', '-'); } if (fieldValue.toString().length() == 8) { Date date = sfCsv.parse(fieldValue.toString()); field.set(object, date); } else if (fieldValue.toString().length() == 10) { Date date = sf.parse(fieldValue.toString()); field.set(object, date); } else { Date date = sdf.parse(fieldValue.toString()); field.set(object, date); } // field.set(object, sdf.parse(fieldValue)); } else if ("class java.lang.Integer".equals(field.getType().toString())) { field.set(object, Integer.valueOf(fieldValue)); } else { field.set(object, fieldValue); } } catch (Exception e) { } } public static void main(String[] args) throws Exception { ObjectUtil util = new ObjectUtil(); /*List<Field> fields = util.getFields(Student.class.getName()); for (Field field : fields) { // 设置访问对象权限,保证对私有属性的访问 field.setAccessible(true); System.out.println(field.getName()); }*/ /*List<Field> fields = util.getAllFields(Student.class.getName()); for (Field field : fields) { // 设置访问对象权限,保证对私有属性的访问 field.setAccessible(true); System.out.println(field.getName()); }*/ /*String str = util.getFieldClassName(Student.class.getName(), "name"); System.out.println(str);*/ /*Student stu = new Student(); stu.setName("Jack"); stu.setSex("男"); String value = util.getValueByPropName(stu, "name"); System.out.println(value);*/ /*Student stu = new Student(); stu.setName("Jack"); stu.setSex("男"); List<Map<String, Object>> lists = util.getFiledsInfo(stu); for (Map<String, Object> map : lists) { System.out.println(map.get("name")); System.out.println(map.get("type")); System.out.println(map.get("value")); }*/ /*Map<String, String> map = util.getAnnotationVal(Student.class.getName()); for (String key : map.keySet()) { System.out.println(map.get(key) + "——————" + key); }*/ /*boolean flag = util.existsField(Student.class.getName(), "name"); System.out.println(flag);*/ Student stu = new Student(); util.setFieldValue("name", "Rose", stu); System.out.println(stu.getName()); } }
测试实体类Student:
public class Student { @Comment(value = "姓名") private String name; @Comment(value = "性别") private String sex; @Comment(value = "年龄") private String age; @Comment(value = "班级") private String stuClass; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getStuClass() { return stuClass; } public void setStuClass(String stuClass) { this.stuClass = stuClass; } }
测试自定义注解@Comment:
import static java.lang.annotation.RetentionPolicy.RUNTIME; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.Target; /** * 自定义属性注解 * @author 【】 * */ @Target({ ElementType.FIELD, ElementType.TYPE }) @Retention(RUNTIME) public @interface Comment { // 定义注解中的属性,默认值为空字符串 String value() default ""; }