java判斷實體內中屬性值內容是否有變更(包含父類屬性值)

红尘沙漏發表於2024-11-27

程式碼內容

/**
 * @author
 * java判斷實體內中屬性值內容是否有變更(包含父類屬性值)
 */
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
public class EntityModifiedChecker {
    public static Map<String, Object> getModifiedFields(Object original, Object modified) throws IllegalAccessException {
        Map<String, Object> changes = new HashMap<>();
        if (original == null || modified == null) {
            return changes;
        }
        Class<?> clazz = original.getClass();
        while(null !=clazz){
            Field[] fields = clazz.getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);
                String name = field.getName();
                System.out.println("name===="+name);
                Object originalValue = field.get(original);
                Object modifiedValue = field.get(modified);
                if (!originalValue.equals(modifiedValue)) {
                    String changStr = "變更內容:"+name+"值由【"+originalValue+"】變更為【"+modifiedValue+"】";
                    changes.put(field.getName(), changStr);
                }
            }
            //處理父類
            clazz = clazz.getSuperclass();
        }


        return changes;
    }

    public static void main(String[] args) throws IllegalAccessException {
        // 假設有一個實體類Entity
        Entity originalEntity = new Entity("original", 1);
        originalEntity.setAddress("北京東城區");
        originalEntity.setAge(15);
        Entity modifiedEntity = new Entity(originalEntity);

        // 模擬修改實體類中的屬性
        modifiedEntity.setName("modified");
        modifiedEntity.setValue(2);
        modifiedEntity.setAddress("上海長寧區");
        modifiedEntity.setAge(10);

        // 獲取變更內容
        Map<String, Object> changes = getModifiedFields(originalEntity, modifiedEntity);

        // 輸出變更內容
        for (Map.Entry<String, Object> entry : changes.entrySet()) {
            System.out.println("屬性名: " + entry.getKey()  + entry.getValue());
        }
    }
}

// 實體類示例
class Entity extends BaseEntity{
    private String name;
    private int value;

    public Entity(String name, int value) {
        this.name = name;
        this.value = value;
    }

    public Entity(Entity entity) {
        this.name = entity.name;
        this.value = entity.value;
    }

    //getters and setters
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }
}

class BaseEntity {
    private String address;
    private int age;

    public BaseEntity(String address, int age) {
        this.address = address;
        this.age = age;
    }

    public BaseEntity(BaseEntity entity) {
        this.address = entity.address;
        this.age = entity.age;
    }
    public BaseEntity() {

    }

    //getters and setters
    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

  

相關文章