今天产品给了个需求,五六十个字段的修改表单记录下修改记录,于是有了这个demo。
先来注解
@Documented
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ContrastName {
String value();
}
@Documented
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ContrastType {
ConType type() default ConType.DEFAULT;
}
ConType
public enum ConType {
DEFAULT,
ENUM,
IS_CHANGE
}
@Documented
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ContrastEnum {
int[] keys();
String[] vals();
}
测试的对象
public class People {
@ContrastName("年龄")
private Integer age;
@ContrastName("姓名")
private String name;
@ContrastName("性别")
@ContrastType(type = ConType.ENUM)
@ContrastEnum(keys = {1,2},vals = {"男","女"})
private Integer sex;
@ContrastName("电话")
@ContrastType(type = ConType.IS_CHANGE)
private String mobile;
public People() {
}
public People(Integer age, String name, Integer sex, String mobile) {
this.age = age;
this.name = name;
this.sex = sex;
this.mobile = mobile;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getSex() {
return sex;
}
public void setSex(Integer sex) {
this.sex = sex;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
@Override
public String toString() {
return "people{" +
"age=" + age +
", name='" + name + '\'' +
", sex=" + sex +
", mobile='" + mobile + '\'' +
'}';
}
}
主要代码
public class Utils {
private List<String> checkList;
public String contrast(Object item, Object entity) {
try {
this.checkList = new ArrayList<>();
StringBuffer sb = new StringBuffer();
if (!item.getClass().equals(entity.getClass()))
return null;
Class<?> clazz = item.getClass();
Field[] fields = clazz.getDeclaredFields();
Map<String, Object> itemMap = Obj2Map(item);
Map<String, Object> entityMap = Obj2Map(entity);
for (Field field : fields) {
String fieldName = field.getName();
if (itemMap.get(fieldName).equals(entityMap.get(fieldName))) {
continue;
}
ContrastName contrastName = field.getAnnotation(ContrastName.class);
ContrastType contrastType = field.getAnnotation(ContrastType.class);
if (contrastType != null && contrastType.type().equals(ConType.IS_CHANGE)) {
this.checkList.add(contrastName.value());
continue;
}
sb.append(contrastName.value()).append(":");
if (contrastType != null && contrastType.type().equals(ConType.ENUM)) {
ContrastEnum anEnum = field.getAnnotation(ContrastEnum.class);
int[] keys = anEnum.keys();
String[] vals = anEnum.vals();
Map<Integer, String> map = new HashMap<>();
for (int i = 0; i < keys.length; i++) {
map.put(keys[i], vals[i]);
}
sb.append("【").append(map.get(itemMap.get(fieldName))).append("】")
.append("更变为")
.append("【").append(map.get(entityMap.get(fieldName))).append("】")
.append("\r\n");
continue;
}
sb.append("【").append(itemMap.get(fieldName)).append("】")
.append("更变为")
.append("【").append(entityMap.get(fieldName)).append("】")
.append("\r\n");
}
if (checkList.size() > 0) {
sb.append("更变了信息:");
for (String check : checkList) {
sb.append("【").append(check).append("】");
}
}
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public Map<String, Object> Obj2Map(Object obj) throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
Field[] fields = obj.getClass().getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
map.put(field.getName(), field.get(obj));
}
return map;
}
}
测试方法
public static void main(String[] args) {
People item = new People(18, "张三", 1, "123");
People entity = new People(18, "李四", 2, "456");
String contrast = new Utils().contrast(item, entity);
System.out.println("res ===> " + contrast);
}
结果
说明
该例子只是demo,不作为最终成品。