Skip to content

Commit

Permalink
Merge pull request #100 from qqxx6661/dev_diff_extend_class
Browse files Browse the repository at this point in the history
feat: 子类DIFF包含父类字段
  • Loading branch information
qqxx6661 authored Apr 6, 2024
2 parents c34ed64 + a9b9234 commit 7ebe8f9
Show file tree
Hide file tree
Showing 22 changed files with 200 additions and 62 deletions.
2 changes: 1 addition & 1 deletion log-record-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<groupId>cn.monitor4all</groupId>
<artifactId>log-record-core</artifactId>
<version>1.6.1</version>
<version>1.6.2</version>

<properties>
<maven.compiler.source>8</maven.compiler.source>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ public static String objectDiff(Object oldObject, Object newObject) throws LogRe
Map<String, Object> newValueMap = new LinkedHashMap<>();

// 遍历旧对象
Field[] fields = oldObject.getClass().getDeclaredFields();
for (Field oldField : fields) {
Field[] oldObjectFields = getAllFields(oldObject.getClass());
for (Field oldField : oldObjectFields) {
try {
// 获取老字段值
oldField.setAccessible(true);
Expand All @@ -95,36 +95,36 @@ public static String objectDiff(Object oldObject, Object newObject) throws LogRe
log.debug("oldField [{}] not need to diff, skip", oldField.getName());
continue;
}
try {
// 在新字段中寻找同名字段,若找不到则抛出NoSuchFieldException异常跳过本次遍历
Field newField = newObject.getClass().getDeclaredField(oldField.getName());
LogRecordDiffField newFieldLogRecordDiffField = newField.getDeclaredAnnotation(LogRecordDiffField.class);
// 获取新字段值
newField.setAccessible(true);
Object newValue = newField.get(newObject);
// 根据新字段判断是否需要进行diff
if (!judgeFieldDiffNeeded(newValue, true, newClassEnableAllFields, newFieldLogRecordDiffField)) {
log.debug("newField [{}] not need to diff, skip", newField.getName());
continue;
}
// 在新字段中寻找同名字段,若找不到则抛出NoSuchFieldException异常跳过本次遍历
Field newField = getFieldByName(newObject.getClass(), oldField.getName());
if (newField == null) {
log.info("no field named [{}] in newObject, skip", oldField.getName());
continue;
}
LogRecordDiffField newFieldLogRecordDiffField = newField.getDeclaredAnnotation(LogRecordDiffField.class);
// 获取新字段值
newField.setAccessible(true);
Object newValue = newField.get(newObject);
// 根据新字段判断是否需要进行diff
if (!judgeFieldDiffNeeded(newValue, true, newClassEnableAllFields, newFieldLogRecordDiffField)) {
log.debug("newField [{}] not need to diff, skip", newField.getName());
continue;
}

// 通过LogRecordDiffField获取字段别名
if (oldFieldLogRecordDiffField != null && newFieldLogRecordDiffField != null) {
String oldFieldAlias = StringUtils.isNotBlank(oldFieldLogRecordDiffField.alias()) ? oldFieldLogRecordDiffField.alias() : null;
String newFieldAlias = StringUtils.isNotBlank(newFieldLogRecordDiffField.alias()) ? newFieldLogRecordDiffField.alias() : null;
oldFieldAliasMap.put(oldField.getName(), oldFieldAlias);
newFieldAliasMap.put(newField.getName(), newFieldAlias);
log.debug("field [{}] has annotation oldField alias [{}] newField alias [{}]", oldField.getName(), oldFieldAlias, newFieldAlias);
}
// 通过LogRecordDiffField获取字段别名
if (oldFieldLogRecordDiffField != null && newFieldLogRecordDiffField != null) {
String oldFieldAlias = StringUtils.isNotBlank(oldFieldLogRecordDiffField.alias()) ? oldFieldLogRecordDiffField.alias() : null;
String newFieldAlias = StringUtils.isNotBlank(newFieldLogRecordDiffField.alias()) ? newFieldLogRecordDiffField.alias() : null;
oldFieldAliasMap.put(oldField.getName(), oldFieldAlias);
newFieldAliasMap.put(newField.getName(), newFieldAlias);
log.debug("field [{}] has annotation oldField alias [{}] newField alias [{}]", oldField.getName(), oldFieldAlias, newFieldAlias);
}

// 对比新老字段值
if (!fieldValueEquals(oldValue, newValue)) {
log.debug("field [{}] is different between oldObject [{}] newObject [{}]", oldField.getName(), oldValue, newValue);
oldValueMap.put(oldField.getName(), oldValue);
newValueMap.put(newField.getName(), newValue);
}
} catch (NoSuchFieldException e) {
log.info("no field named [{}] in newObject, skip", oldField.getName());
// 对比新老字段值
if (!fieldValueEquals(oldValue, newValue)) {
log.debug("field [{}] is different between oldObject [{}] newObject [{}]", oldField.getName(), oldValue, newValue);
oldValueMap.put(oldField.getName(), oldValue);
newValueMap.put(newField.getName(), newValue);
}
} catch (Exception e) {
log.error("objectDiff error", e);
Expand Down Expand Up @@ -254,4 +254,29 @@ private static boolean isJsonArray(Object obj) {
return obj.getClass().isArray() || obj instanceof Collection;
}

/**
* 获取类所有字段 循环遍历所有父类
*/
private static Field[] getAllFields(Class<?> type) {
List<Field> fields = new ArrayList<>();
for (Class<?> c = type; c != null; c = c.getSuperclass()) {
Collections.addAll(fields, c.getDeclaredFields());
}
return fields.toArray(new Field[0]);
}

/**
* 获取类指定字段
* 使用了一个设计为通过异常来指示特定条件的Java标准库方法,这确实是一个特殊情况,通常不会使用异常来做业务逻辑。
*/
private static Field getFieldByName(Class<?> type, String fieldName) {
for (Class<?> c = type; c != null; c = c.getSuperclass()) {
try {
return c.getDeclaredField(fieldName);
} catch (NoSuchFieldException ignored) {
}
}
return null;
}

}
6 changes: 3 additions & 3 deletions log-record-springboot3-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<groupId>cn.monitor4all</groupId>
<artifactId>log-record-springboot3-starter</artifactId>
<version>1.6.1</version>
<version>1.6.2</version>

<properties>
<maven.compiler.source>17</maven.compiler.source>
Expand All @@ -35,7 +35,7 @@
<dependency>
<groupId>cn.monitor4all</groupId>
<artifactId>log-record-core</artifactId>
<version>1.6.1</version>
<version>1.6.2</version>
</dependency>

<!-- 单元测试依赖 -->
Expand Down Expand Up @@ -96,7 +96,7 @@
<id>user</id>
<properties>
<!-- 可自行更改 -->
<doc.path>${java.home}/../bin/javadoc</doc.path>
<doc.path>${java.home}/bin/javadoc</doc.path>
</properties>
</profile>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
import cn.monitor4all.logRecord.springboot3.LogRecordAutoConfiguration;
import cn.monitor4all.logRecord.springboot3.test.bean.TestComplexUser;
import cn.monitor4all.logRecord.springboot3.test.bean.TestUser;
import cn.monitor4all.logRecord.springboot3.test.bean.diff.TestDiffDuty;
import cn.monitor4all.logRecord.springboot3.test.bean.diff.TestDiffJob;
import cn.monitor4all.logRecord.springboot3.test.bean.diff.TestDiffUserParam;
import cn.monitor4all.logRecord.springboot3.test.bean.diff.extend.TestDiffChildClass;
import cn.monitor4all.logRecord.springboot3.test.bean.diff.nested.TestDiffDuty;
import cn.monitor4all.logRecord.springboot3.test.bean.diff.nested.TestDiffJob;
import cn.monitor4all.logRecord.springboot3.test.bean.diff.nested.TestDiffUserParam;
import cn.monitor4all.logRecord.springboot3.test.service.OperatorIdGetService;
import cn.monitor4all.logRecord.springboot3.test.service.TestService;
import cn.monitor4all.logRecord.springboot3.test.utils.TestHelper;
Expand Down Expand Up @@ -320,9 +321,9 @@ public void testLogRecordDiffNestedClass() {
Assertions.assertEquals(logDTO.getMsg(), "【id】从【2】变成了【3】 【name】从【小张三】变成了【小李四】" +
" 【jobList】从【[TestDiffJob(jobId=22, jobName=222, dutyList=[TestDiffDuty(dutyId=222, dutyName=222)])]】" +
"变成了【[TestDiffJob(jobId=22, jobName=222, dutyList=[TestDiffDuty(dutyId=333, dutyName=222)])]】");
Assertions.assertEquals(logDTO.getDiffDTOList().get(0).getOldClassName(), "cn.monitor4all.logRecord.springboot3.test.bean.diff.TestDiffUserVO");
Assertions.assertEquals(logDTO.getDiffDTOList().get(0).getOldClassName(), "cn.monitor4all.logRecord.springboot3.test.bean.diff.nested.TestDiffUserVO");
Assertions.assertEquals(logDTO.getDiffDTOList().get(0).getOldClassAlias(), "用户信息嵌套展示实体");
Assertions.assertEquals(logDTO.getDiffDTOList().get(0).getNewClassName(), "cn.monitor4all.logRecord.springboot3.test.bean.diff.TestDiffUserParam");
Assertions.assertEquals(logDTO.getDiffDTOList().get(0).getNewClassName(), "cn.monitor4all.logRecord.springboot3.test.bean.diff.nested.TestDiffUserParam");
Assertions.assertEquals(logDTO.getDiffDTOList().get(0).getNewClassAlias(), "用户信息嵌套入参实体");
Assertions.assertEquals(logDTO.getDiffDTOList().get(0).getDiffFieldDTOList().get(0).getFieldName(), "id");
Assertions.assertEquals(logDTO.getDiffDTOList().get(0).getDiffFieldDTOList().get(0).getOldValue(), 2);
Expand All @@ -346,6 +347,24 @@ public void testMultipleDiff() {
Assertions.assertEquals(logDTO.getDiffDTOList().get(0).getDiffFieldDTOList().get(0).getNewValue(), 2);
}

@Test
public void testExtendClassDiff() {
TestHelper.addLock("testExtendClassDiff");
TestDiffChildClass testDiffChildClass = new TestDiffChildClass();
testDiffChildClass.setParamFromParent("newObject parentParam");
testDiffChildClass.setParamFromChild("newObject childParam");
testService.testExtendClassDiff(testDiffChildClass);
TestHelper.await("testExtendClassDiff");
LogDTO logDTO = TestHelper.getLogDTO("testExtendClassDiff");

Assertions.assertEquals(logDTO.getMsg(), "【paramFromChild】从【oldObject childParam】变成了【newObject childParam】 【paramFromParent】从【oldObject parentParam】变成了【newObject parentParam】");
Assertions.assertEquals(logDTO.getDiffDTOList().get(0).getOldClassName(), "cn.monitor4all.logRecord.springboot3.test.bean.diff.extend.TestDiffChildClass");
Assertions.assertEquals(logDTO.getDiffDTOList().get(0).getOldClassAlias(), "DIFF测试类子类");
Assertions.assertEquals(logDTO.getDiffDTOList().get(0).getDiffFieldDTOList().get(0).getFieldName(), "paramFromChild");
Assertions.assertEquals(logDTO.getDiffDTOList().get(0).getDiffFieldDTOList().get(0).getOldValue(), "oldObject childParam");
Assertions.assertEquals(logDTO.getDiffDTOList().get(0).getDiffFieldDTOList().get(0).getNewValue(), "newObject childParam");
}

@Test
public void testConditionTrue() {
TestHelper.addLock("testConditionTrue");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package cn.monitor4all.logRecord.springboot3.test.bean.diff.extend;

import cn.monitor4all.logRecord.annotation.LogRecordDiffObject;
import lombok.Data;
import lombok.EqualsAndHashCode;

@EqualsAndHashCode(callSuper = true)
@Data
@LogRecordDiffObject(alias = "DIFF测试类子类")
public class TestDiffChildClass extends TestDiffParentClass {

private String paramFromChild;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package cn.monitor4all.logRecord.springboot3.test.bean.diff.extend;

import cn.monitor4all.logRecord.annotation.LogRecordDiffObject;
import lombok.Data;

@Data
@LogRecordDiffObject(alias = "DIFF测试类父类")
public class TestDiffParentClass {

private String paramFromParent;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.monitor4all.logRecord.springboot.test.bean.diff;
package cn.monitor4all.logRecord.springboot3.test.bean.diff.nested;

import lombok.Data;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.monitor4all.logRecord.springboot3.test.bean.diff;
package cn.monitor4all.logRecord.springboot3.test.bean.diff.nested;

import lombok.Data;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.monitor4all.logRecord.springboot3.test.bean.diff;
package cn.monitor4all.logRecord.springboot3.test.bean.diff.nested;

import cn.monitor4all.logRecord.annotation.LogRecordDiffObject;
import lombok.Data;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.monitor4all.logRecord.springboot3.test.bean.diff;
package cn.monitor4all.logRecord.springboot3.test.bean.diff.nested;

import cn.monitor4all.logRecord.annotation.LogRecordDiffObject;
import lombok.Data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ public boolean createLog(LogDTO logDTO) {
TestHelper.releaseLock("testMultipleDiff");
}

if ("testExtendClassDiff".equals(logDTO.getBizType())) {
TestHelper.putLogDTO("testExtendClassDiff", logDTO);
TestHelper.releaseLock("testExtendClassDiff");
}

if ("testConditionTrue".equals(logDTO.getBizType())) {
TestHelper.putLogDTO("testConditionTrue", logDTO);
TestHelper.releaseLock("testConditionTrue");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package cn.monitor4all.logRecord.springboot3.test.service;


import cn.monitor4all.logRecord.springboot3.test.bean.diff.TestDiffDuty;
import cn.monitor4all.logRecord.springboot3.test.bean.diff.TestDiffJob;
import cn.monitor4all.logRecord.springboot3.test.bean.diff.extend.TestDiffChildClass;
import cn.monitor4all.logRecord.springboot3.test.bean.diff.nested.TestDiffDuty;
import cn.monitor4all.logRecord.springboot3.test.bean.diff.nested.TestDiffJob;
import cn.monitor4all.logRecord.annotation.OperationLog;
import cn.monitor4all.logRecord.context.LogRecordContext;
import cn.monitor4all.logRecord.springboot3.test.bean.TestComplexUser;
import cn.monitor4all.logRecord.springboot3.test.bean.TestUser;
import cn.monitor4all.logRecord.springboot3.test.bean.diff.TestDiffUserParam;
import cn.monitor4all.logRecord.springboot3.test.bean.diff.TestDiffUserVO;
import cn.monitor4all.logRecord.springboot3.test.bean.diff.nested.TestDiffUserParam;
import cn.monitor4all.logRecord.springboot3.test.bean.diff.nested.TestDiffUserVO;
import org.springframework.boot.test.context.TestComponent;

import java.util.Arrays;
Expand Down Expand Up @@ -164,6 +165,13 @@ public void testMultipleDiff(TestUser testUser) {
LogRecordContext.putVariable("oldObject2", new TestUser(3, "王五"));
}

@OperationLog(bizId = "'1'", bizType = "'testExtendClassDiff'", msg = "#_DIFF(#oldObject, #p0)")
public void testExtendClassDiff(TestDiffChildClass testDiffChildClass) {
TestDiffChildClass oldObject = new TestDiffChildClass();
oldObject.setParamFromParent("oldObject parentParam");
oldObject.setParamFromChild("oldObject childParam");
LogRecordContext.putVariable("oldObject", oldObject);
}

@OperationLog(bizId = "'1'", bizType = "'testConditionTrue'", condition = "#p0 != null")
public void testConditionTrue(TestUser testUser) {
Expand Down
4 changes: 2 additions & 2 deletions log-record-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<groupId>cn.monitor4all</groupId>
<artifactId>log-record-starter</artifactId>
<version>1.6.1</version>
<version>1.6.2</version>

<properties>
<maven.compiler.source>8</maven.compiler.source>
Expand All @@ -35,7 +35,7 @@
<dependency>
<groupId>cn.monitor4all</groupId>
<artifactId>log-record-core</artifactId>
<version>1.6.1</version>
<version>1.6.2</version>
</dependency>

<!-- 单元测试依赖 -->
Expand Down
Loading

0 comments on commit 7ebe8f9

Please sign in to comment.