Skip to content

Commit 3ad166d

Browse files
committed
add test
1 parent 5f52322 commit 3ad166d

File tree

4 files changed

+65
-8
lines changed

4 files changed

+65
-8
lines changed

springboot-starter/src/main/java/com/codingapi/springboot/framework/domain/field/FieldValueInterceptor.java

+37-6
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public Object intercept(Object obj, Method method, Object[] args, MethodProxy pr
7676
}
7777

7878
if(fields.isEmpty()){
79-
this.readFields();
79+
this.readFields(fields,target,propertyDescriptors);
8080
}
8181
Object result = method.invoke(target, args);
8282
this.compareAndUpdateField();
@@ -88,14 +88,28 @@ public Object intercept(Object obj, Method method, Object[] args, MethodProxy pr
8888
* @throws InvocationTargetException InvocationTargetException
8989
* @throws IllegalAccessException InvocationTargetException
9090
*/
91-
private void readFields() throws InvocationTargetException, IllegalAccessException {
91+
private void readFields(Map<String,Object> fields, Object target,PropertyDescriptor[] propertyDescriptors) throws InvocationTargetException, IllegalAccessException {
9292
for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
9393
String name = propertyDescriptor.getName();
9494
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+
}
96102
}
97103
}
98104

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+
99113
/**
100114
* 对比字段
101115
* @throws InvocationTargetException InvocationTargetException
@@ -106,10 +120,27 @@ private void compareAndUpdateField() throws InvocationTargetException, IllegalAc
106120
String name = propertyDescriptor.getName();
107121
Object newValue = propertyDescriptor.getReadMethod().invoke(target);
108122
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);
111142
}
112-
fields.put(name, newValue);
143+
113144
}
114145
}
115146

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.codingapi.springboot.framework.domain;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
6+
@Setter
7+
@Getter
8+
public class Ainimal {
9+
10+
private String name;
11+
}

springboot-starter/src/test/java/com/codingapi/springboot/framework/domain/Demo.java

+13-1
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,28 @@ public class Demo implements JsonSerializable, MapSerializable {
1414
@Getter
1515
private String name;
1616

17+
@Getter
18+
private Ainimal ainimal;
19+
1720
public Demo(String name) {
1821
this.name = name;
1922
this.id = System.currentTimeMillis();
23+
this.ainimal = new Ainimal();
24+
this.ainimal.setName("cat");
2025
}
2126

2227
public void changeName(String name) {
2328
String beforeName = this.name;
2429
this.name = name;
25-
// push event
30+
31+
if (beforeName.equals(name)) {
32+
return;
33+
}
34+
// push event
2635
EventPusher.push(new DemoChangeEvent(beforeName, name));
2736
}
2837

38+
public void changeAinimalName(String name) {
39+
this.ainimal.setName(name);
40+
}
2941
}

springboot-starter/src/test/java/com/codingapi/springboot/framework/domain/FieldProxyFactoryTest.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ class FieldProxyFactoryTest {
1010
@Test
1111
void createEntity() {
1212
Demo demo = FieldProxyFactory.create(Demo.class, "test");
13-
demo.changeName("123");
13+
demo.changeAinimalName("123");
14+
demo.changeAinimalName("234");
15+
demo.changeName("test");
16+
demo.changeName("test123");
1417
}
1518
}

0 commit comments

Comments
 (0)