@@ -76,7 +76,7 @@ public Object intercept(Object obj, Method method, Object[] args, MethodProxy pr
76
76
}
77
77
78
78
if (fields .isEmpty ()){
79
- this .readFields ();
79
+ this .readFields (fields , target , propertyDescriptors );
80
80
}
81
81
Object result = method .invoke (target , args );
82
82
this .compareAndUpdateField ();
@@ -88,14 +88,28 @@ public Object intercept(Object obj, Method method, Object[] args, MethodProxy pr
88
88
* @throws InvocationTargetException InvocationTargetException
89
89
* @throws IllegalAccessException InvocationTargetException
90
90
*/
91
- private void readFields () throws InvocationTargetException , IllegalAccessException {
91
+ private void readFields (Map < String , Object > fields , Object target , PropertyDescriptor [] propertyDescriptors ) throws InvocationTargetException , IllegalAccessException {
92
92
for (PropertyDescriptor propertyDescriptor : propertyDescriptors ) {
93
93
String name = propertyDescriptor .getName ();
94
94
Object value = propertyDescriptor .getReadMethod ().invoke (target );
95
- fields .put (name , value );
95
+ if (isPrimitive (value )) {
96
+ fields .put (name , value );
97
+ }else {
98
+ Map <String ,Object > childFields = new HashMap <>();
99
+ this .readFields (childFields ,value ,BeanUtils .getPropertyDescriptors (value .getClass ()));
100
+ fields .put (name , childFields );
101
+ }
96
102
}
97
103
}
98
104
105
+
106
+ private boolean isPrimitive (Object obj ) {
107
+ return obj instanceof String || obj instanceof Integer || obj instanceof Long
108
+ || obj instanceof Double || obj instanceof Float || obj instanceof Boolean
109
+ || obj instanceof Short || obj instanceof Byte || obj instanceof Character
110
+ || obj instanceof Enum || obj instanceof Class ;
111
+ }
112
+
99
113
/**
100
114
* 对比字段
101
115
* @throws InvocationTargetException InvocationTargetException
@@ -106,10 +120,27 @@ private void compareAndUpdateField() throws InvocationTargetException, IllegalAc
106
120
String name = propertyDescriptor .getName ();
107
121
Object newValue = propertyDescriptor .getReadMethod ().invoke (target );
108
122
Object oldValue = fields .get (name );
109
- if (!newValue .equals (oldValue )) {
110
- pushEvent (name , oldValue , newValue );
123
+ if (isPrimitive (newValue )) {
124
+ if (!newValue .equals (oldValue )) {
125
+ pushEvent (name , oldValue , newValue );
126
+ }
127
+ fields .put (name , newValue );
128
+ }else {
129
+ Map <String ,Object > newFields = new HashMap <>();
130
+ this .readFields (newFields ,newValue ,BeanUtils .getPropertyDescriptors (newValue .getClass ()));
131
+
132
+ Map <String ,Object > oldFields = (Map <String ,Object >)oldValue ;
133
+ for (String key :oldFields .keySet ()){
134
+ Object oldChildValue = oldFields .get (key );
135
+ Object newChildValue = newFields .get (key );
136
+ if (!oldChildValue .equals (newChildValue )){
137
+ String namePrefix = name + "." ;
138
+ pushEvent (namePrefix +key , oldChildValue , newChildValue );
139
+ }
140
+ }
141
+ fields .put (name , newFields );
111
142
}
112
- fields . put ( name , newValue );
143
+
113
144
}
114
145
}
115
146
0 commit comments