|
| 1 | +package com.codingapi.springboot.framework.domain.field; |
| 2 | + |
| 3 | +import com.codingapi.springboot.framework.event.EventPusher; |
| 4 | +import lombok.extern.slf4j.Slf4j; |
| 5 | +import org.springframework.beans.BeanUtils; |
| 6 | +import org.springframework.cglib.proxy.Enhancer; |
| 7 | +import org.springframework.cglib.proxy.MethodInterceptor; |
| 8 | +import org.springframework.cglib.proxy.MethodProxy; |
| 9 | + |
| 10 | +import java.beans.PropertyDescriptor; |
| 11 | +import java.lang.reflect.InvocationTargetException; |
| 12 | +import java.lang.reflect.Method; |
| 13 | +import java.util.HashMap; |
| 14 | +import java.util.Map; |
| 15 | + |
| 16 | +/** |
| 17 | + * 实体代理 |
| 18 | + */ |
| 19 | +@Slf4j |
| 20 | +public class FieldValueInterceptor implements MethodInterceptor { |
| 21 | + |
| 22 | + // 目标类 |
| 23 | + private final Class<?> targetClass; |
| 24 | + // 目标类构造函数参数类型 |
| 25 | + private final Class<?>[] parameterTypes; |
| 26 | + // 目标类构造函数参数 |
| 27 | + private final Object[] args; |
| 28 | + |
| 29 | + // 目标类实例 |
| 30 | + private final Object target; |
| 31 | + // 目标类属性描述 |
| 32 | + private final PropertyDescriptor[] propertyDescriptors; |
| 33 | + // 目标类属性值 |
| 34 | + private final Map<String, Object> fields; |
| 35 | + |
| 36 | + public FieldValueInterceptor(Class<?> targetClass, Object... args) throws NoSuchMethodException, |
| 37 | + InvocationTargetException, InstantiationException, IllegalAccessException { |
| 38 | + this.targetClass = targetClass; |
| 39 | + this.args = args; |
| 40 | + this.parameterTypes = new Class<?>[args.length]; |
| 41 | + for (int i = 0; i < args.length; i++) { |
| 42 | + parameterTypes[i] = args[i].getClass(); |
| 43 | + } |
| 44 | + this.target = targetClass.getConstructor(parameterTypes).newInstance(args); |
| 45 | + this.propertyDescriptors = BeanUtils.getPropertyDescriptors(targetClass); |
| 46 | + this.fields = new HashMap<>(); |
| 47 | + } |
| 48 | + |
| 49 | + |
| 50 | + /** |
| 51 | + * 创建代理 |
| 52 | + * @return 代理对象 |
| 53 | + */ |
| 54 | + public Object createProxy() { |
| 55 | + Enhancer enhancer = new Enhancer(); |
| 56 | + enhancer.setSuperclass(targetClass); |
| 57 | + enhancer.setCallback(this); |
| 58 | + return enhancer.create(parameterTypes, args); |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | + /** |
| 63 | + * 拦截方法 |
| 64 | + * @param obj 代理对象 |
| 65 | + * @param method 方法 |
| 66 | + * @param args 参数 |
| 67 | + * @param proxy 代理 |
| 68 | + * @return 方法返回值 |
| 69 | + * @throws Throwable 异常 |
| 70 | + */ |
| 71 | + @Override |
| 72 | + public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { |
| 73 | + // 更新函数肯定有参数,如果没有参数,直接返回 |
| 74 | + if (method.getParameterCount() <= 0) { |
| 75 | + return method.invoke(target, args); |
| 76 | + } |
| 77 | + |
| 78 | + if(fields.isEmpty()){ |
| 79 | + this.readFields(); |
| 80 | + } |
| 81 | + Object result = method.invoke(target, args); |
| 82 | + this.compareAndUpdateField(); |
| 83 | + return result; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * 读取Entity字段 |
| 88 | + * @throws InvocationTargetException InvocationTargetException |
| 89 | + * @throws IllegalAccessException InvocationTargetException |
| 90 | + */ |
| 91 | + private void readFields() throws InvocationTargetException, IllegalAccessException { |
| 92 | + for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { |
| 93 | + String name = propertyDescriptor.getName(); |
| 94 | + Object value = propertyDescriptor.getReadMethod().invoke(target); |
| 95 | + fields.put(name, value); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * 对比字段 |
| 101 | + * @throws InvocationTargetException InvocationTargetException |
| 102 | + * @throws IllegalAccessException InvocationTargetException |
| 103 | + */ |
| 104 | + private void compareAndUpdateField() throws InvocationTargetException, IllegalAccessException { |
| 105 | + for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { |
| 106 | + String name = propertyDescriptor.getName(); |
| 107 | + Object newValue = propertyDescriptor.getReadMethod().invoke(target); |
| 108 | + Object oldValue = fields.get(name); |
| 109 | + if (!newValue.equals(oldValue)) { |
| 110 | + pushEvent(name, oldValue, newValue); |
| 111 | + } |
| 112 | + fields.put(name, newValue); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + private void pushEvent(String field, Object oldValue, Object newValue) { |
| 117 | + FieldChangeEvent event = new FieldChangeEvent(); |
| 118 | + event.setEntity(target); |
| 119 | + event.setSimpleName(targetClass.getSimpleName()); |
| 120 | + event.setFieldName(field); |
| 121 | + event.setOldValue(oldValue); |
| 122 | + event.setNewValue(newValue); |
| 123 | + event.setTimestamp(System.currentTimeMillis()); |
| 124 | + EventPusher.push(event); |
| 125 | + } |
| 126 | +} |
0 commit comments