refactor: optimize ModifyAttrsRecordProxyFactory.java

This commit is contained in:
开源海哥 2023-07-30 12:27:09 +08:00
parent 6d50a6228b
commit b76db561f1
4 changed files with 20 additions and 14 deletions

View File

@ -25,7 +25,9 @@ import java.util.Map;
class ModifyAttrsRecordHandler implements MethodHandler { class ModifyAttrsRecordHandler implements MethodHandler {
//更新内容 /**
* 更新的字段和内容
*/
private final Map<String, Object> updates = new LinkedHashMap<>(); private final Map<String, Object> updates = new LinkedHashMap<>();
public Map<String, Object> getUpdates() { public Map<String, Object> getUpdates() {

View File

@ -19,8 +19,11 @@ import com.mybatisflex.core.util.ClassUtil;
import org.apache.ibatis.javassist.util.proxy.ProxyFactory; import org.apache.ibatis.javassist.util.proxy.ProxyFactory;
import org.apache.ibatis.javassist.util.proxy.ProxyObject; import org.apache.ibatis.javassist.util.proxy.ProxyObject;
import org.apache.ibatis.logging.LogFactory; import org.apache.ibatis.logging.LogFactory;
import org.apache.ibatis.util.MapUtil;
import java.util.Arrays; import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/** /**
@ -28,37 +31,38 @@ import java.util.Arrays;
*/ */
public class ModifyAttrsRecordProxyFactory { public class ModifyAttrsRecordProxyFactory {
private static final ModifyAttrsRecordProxyFactory instance = new ModifyAttrsRecordProxyFactory(); protected static final Map<Class<?>, Class<?>> CACHE = new ConcurrentHashMap<>();
private static final ModifyAttrsRecordProxyFactory INSTANCE = new ModifyAttrsRecordProxyFactory();
public static ModifyAttrsRecordProxyFactory getInstance() { public static ModifyAttrsRecordProxyFactory getInstance() {
return instance; return INSTANCE;
} }
private ModifyAttrsRecordProxyFactory() { private ModifyAttrsRecordProxyFactory() {
} }
public <T> T get(Class<T> target) { public <T> T get(Class<T> target) {
ProxyFactory factory = new ProxyFactory(); Class<?> proxyClass = MapUtil.computeIfAbsent(CACHE, target, aClass -> {
factory.setSuperclass(target); ProxyFactory factory = new ProxyFactory();
factory.setSuperclass(target);
Class<?>[] interfaces = Arrays.copyOf(target.getInterfaces(), target.getInterfaces().length + 1); Class<?>[] interfaces = Arrays.copyOf(target.getInterfaces(), target.getInterfaces().length + 1);
interfaces[interfaces.length - 1] = UpdateWrapper.class; interfaces[interfaces.length - 1] = UpdateWrapper.class;
factory.setInterfaces(interfaces); factory.setInterfaces(interfaces);
final Class<?> proxyClass = factory.createClass(); return factory.createClass();
});
T proxyObject = null; T proxyObject = null;
try { try {
proxyObject = (T) ClassUtil.newInstance(proxyClass); proxyObject = (T) ClassUtil.newInstance(proxyClass);
((ProxyObject) proxyObject).setHandler(new ModifyAttrsRecordHandler()); ((ProxyObject) proxyObject).setHandler(new ModifyAttrsRecordHandler());
} catch (Exception e){ } catch (Exception e) {
LogFactory.getLog(ModifyAttrsRecordProxyFactory.class).error(e.toString(), e); LogFactory.getLog(ModifyAttrsRecordProxyFactory.class).error(e.toString(), e);
} }
return proxyObject; return proxyObject;
} }
} }

View File

@ -141,7 +141,7 @@ public class ClassUtil {
// 没有任何构造函数的情况下去查找 static 工厂方法满足 lombok 注解的需求 // 没有任何构造函数的情况下去查找 static 工厂方法满足 lombok 注解的需求
else { else {
Method factoryMethod = ClassUtil.getFirstMethod(clazz, m -> m.getParameterCount() == 0 Method factoryMethod = ClassUtil.getFirstMethod(clazz, m -> m.getParameterCount() == 0
&& clazz.isAssignableFrom(m.getReturnType()) && clazz == m.getReturnType()
&& Modifier.isPublic(m.getModifiers()) && Modifier.isPublic(m.getModifiers())
&& Modifier.isStatic(m.getModifiers())); && Modifier.isStatic(m.getModifiers()));
if (factoryMethod != null) { if (factoryMethod != null) {

View File

@ -53,7 +53,7 @@ public class MainSqlTest {
String sql2 = UpdateChain.of(Article.class) String sql2 = UpdateChain.of(Article.class)
.set("xxxx", "xxxx") .set("xxxx", "xxxx")
// .where(Article::getId).ge(100) .where(Article::getId).ge(100)
.toSQL(); .toSQL();
System.out.println(sql2); System.out.println(sql2);