feat(service): 新增QueryService接口实现,支持通用查询功能 docs(util): 完善ProxyUtils工具类的注释说明 fix(model): 修复CustomerCatalog实现IdentityEntity接口 style: 优化代码格式和导入顺序 perf(util): 提升FileUtils工具类功能,新增文件处理方法 chore: 更新README.md文件,补充UI资源路径说明 build: 更新pom.xml文件中的mainClass配置 test: 调整测试类命名和路径 ci: 更新CI配置文件中的类引用 refactor(controller): 重构表格单元格异步更新逻辑 docs(constant): 新增常量定义和注释 style: 统一代码风格和命名规范 refactor(service): 重构服务类继承关系 perf(controller): 优化表格数据加载性能 fix(service): 修复文件类型服务缓存问题 docs(model): 完善视图模型类注释 refactor(util): 重构文件工具类方法 style: 清理无用导入和代码 chore: 更新.gitignore文件 build: 调整项目依赖配置 refactor(controller): 重构控制器基类 perf(service): 优化查询服务性能 fix(controller): 修复表格数据加载异常 docs: 更新代码注释和文档 style: 统一代码缩进和格式
101 lines
3.2 KiB
Java
101 lines
3.2 KiB
Java
package com.ecep.contract.util;
|
||
|
||
import java.lang.reflect.Field;
|
||
import java.lang.reflect.Proxy;
|
||
|
||
import com.ecep.contract.model.IdentityEntity;
|
||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||
|
||
public class ProxyUtils {
|
||
/**
|
||
* 判断传入的对象是否已完成初始化。
|
||
* 在客户端接收服务器返回数据的场景下,代理对象序列化后通常仅包含 id 属性,其余属性均为 null。
|
||
* 本方法通过检查对象除 id 外的其他非 @JsonIgnore 字段是否为 null 来判断对象是否已初始化。
|
||
* 若执行过程中出现异常,则默认对象已初始化。
|
||
*
|
||
* @param proxy 待检查的对象
|
||
* @return 若对象已初始化返回 true,否则返回 false
|
||
*/
|
||
public static boolean isInitialized(Object proxy) {
|
||
if (proxy == null) {
|
||
return false;
|
||
}
|
||
|
||
// 检查 proxy 中除了id其他属性都为null
|
||
try {
|
||
Field[] fields = proxy.getClass().getFields();
|
||
for (Field field : fields) {
|
||
if (field.getName().equals("id")) {
|
||
continue;
|
||
}
|
||
// 忽略被JsonIgnore注解标记的字段
|
||
if (field.getAnnotation(JsonIgnore.class) != null) {
|
||
continue;
|
||
}
|
||
// 检查字段是否为null
|
||
if (field.get(proxy) != null) {
|
||
return true;
|
||
}
|
||
}
|
||
// 如果除了id其他属性都为null,认为未初始化
|
||
return false;
|
||
} catch (Exception e) {
|
||
// 发生异常时默认已初始化
|
||
return true;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 检查对象是否是代理对象
|
||
*/
|
||
public static boolean isProxyObject(Object obj) {
|
||
return false;
|
||
}
|
||
|
||
/**
|
||
* 创建代理对象
|
||
*/
|
||
@SuppressWarnings("unchecked")
|
||
public static <T> T createProxy(T original, Class<?>... interfaces) {
|
||
if (original == null) {
|
||
return null;
|
||
}
|
||
|
||
// 如果对象已经是代理对象,直接返回
|
||
if (isProxyObject(original)) {
|
||
return original;
|
||
}
|
||
|
||
// 创建代理对象
|
||
T proxy = (T) Proxy.newProxyInstance(
|
||
original.getClass().getClassLoader(),
|
||
interfaces,
|
||
(proxyObj, method, args) -> {
|
||
// 如果代理对象未初始化,先初始化
|
||
if (!isInitialized(proxyObj)) {
|
||
// 这里可以添加初始化逻辑
|
||
markInitialized(proxyObj);
|
||
}
|
||
// 调用原始对象的方法
|
||
return method.invoke(original, args);
|
||
});
|
||
return proxy;
|
||
}
|
||
|
||
/**
|
||
* 标记对象为已初始化
|
||
*/
|
||
public static void markInitialized(Object proxy) {
|
||
if (proxy != null && isProxyObject(proxy)) {
|
||
// 标记为已初始化
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 检查对象是否是实体类
|
||
*/
|
||
public static boolean isEntityClass(Class<?> clazz) {
|
||
return clazz != null && IdentityEntity.class.isAssignableFrom(clazz);
|
||
}
|
||
}
|