DataSqlMapperBuilder

This commit is contained in:
redkale
2024-06-05 22:39:33 +08:00
parent f3f5f53535
commit 698e6c6ee7
3 changed files with 28 additions and 7 deletions

View File

@@ -4,9 +4,11 @@
package org.redkale.asm; package org.redkale.asm;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.redkale.annotation.Param;
import org.redkale.convert.json.JsonConvert; import org.redkale.convert.json.JsonConvert;
/** /**
@@ -64,23 +66,37 @@ public class AsmMethodBean {
} }
public List<String> fieldNameList() { public List<String> fieldNameList() {
return paramNameList(null);
}
public List<String> paramNameList(Method method) {
if (params == null) { if (params == null) {
return new ArrayList<>(); return new ArrayList<>();
} }
int index = 0;
Parameter[] ps = method == null ? null : method.getParameters();
List<String> rs = new ArrayList<>(params.size()); List<String> rs = new ArrayList<>(params.size());
for (AsmMethodParam p : params) { for (AsmMethodParam p : params) {
rs.add(p.getName()); Param pann = ps == null ? null : ps[index].getAnnotation(Param.class);
rs.add(pann == null ? p.getName() : pann.value());
index++;
} }
return rs; return rs;
} }
public String[] fieldNameArray() { public String[] fieldNameArray() {
return paramNameArray(null);
}
public String[] paramNameArray(Method method) {
if (params == null) { if (params == null) {
return null; return null;
} }
Parameter[] ps = method == null ? null : method.getParameters();
String[] rs = new String[params.size()]; String[] rs = new String[params.size()];
for (int i = 0; i < rs.length; i++) { for (int i = 0; i < rs.length; i++) {
rs[i] = params.get(i).getName(); Param pann = ps == null ? null : ps[i].getAnnotation(Param.class);
rs[i] = pann == null ? params.get(i).getName() : pann.value();
} }
return rs; return rs;
} }

View File

@@ -169,7 +169,7 @@ public class CacheAsmMethodBoost extends AsmMethodBoost {
mv.visitMaxs(20, 20); mv.visitMaxs(20, 20);
mv.visitEnd(); mv.visitEnd();
CacheAction action = new CacheAction( CacheAction action = new CacheAction(
new CacheEntry(cached), method, serviceType, methodBean.fieldNameArray(), dynFieldName); new CacheEntry(cached), method, serviceType, methodBean.paramNameArray(method), dynFieldName);
actions.put(dynFieldName, action); actions.put(dynFieldName, action);
} }
{ // ThrowSupplier { // ThrowSupplier
@@ -217,7 +217,11 @@ public class CacheAsmMethodBoost extends AsmMethodBoost {
String dynFieldName = cached.dynField(); String dynFieldName = cached.dynField();
AsmMethodBean methodBean = AsmMethodBean.get(methodBeans, method); AsmMethodBean methodBean = AsmMethodBean.get(methodBeans, method);
CacheAction action = new CacheAction( CacheAction action = new CacheAction(
new CacheEntry(cached), method, serviceType, methodBean.fieldNameArray(), dynFieldName); new CacheEntry(cached),
method,
serviceType,
methodBean.paramNameArray(method),
dynFieldName);
actionMap.put(dynFieldName, action); actionMap.put(dynFieldName, action);
} }
} }

View File

@@ -69,12 +69,12 @@ public final class DataSqlMapperBuilder {
Class clz = RedkaleClassLoader.findDynClass(newDynName.replace('/', '.')); Class clz = RedkaleClassLoader.findDynClass(newDynName.replace('/', '.'));
Class newClazz = clz == null ? loader.loadClass(newDynName.replace('/', '.')) : clz; Class newClazz = clz == null ? loader.loadClass(newDynName.replace('/', '.')) : clz;
M mapper = (M) newClazz.getDeclaredConstructor().newInstance(); M mapper = (M) newClazz.getDeclaredConstructor().newInstance();
{ { // DataSqlSource
Field c = newClazz.getDeclaredField("_source"); Field c = newClazz.getDeclaredField("_source");
c.setAccessible(true); c.setAccessible(true);
c.set(mapper, source); c.set(mapper, source);
} }
{ { // Entity Class
Field c = newClazz.getDeclaredField("_type"); Field c = newClazz.getDeclaredField("_type");
c.setAccessible(true); c.setAccessible(true);
c.set(mapper, entityType); c.set(mapper, entityType);
@@ -120,7 +120,7 @@ public final class DataSqlMapperBuilder {
} }
DataNativeSqlInfo sqlInfo = nativeSqlParser.parse(signFunc, source.getType(), sql.value()); DataNativeSqlInfo sqlInfo = nativeSqlParser.parse(signFunc, source.getType(), sql.value());
AsmMethodBean methodBean = selfMethodBeans.get(AsmMethodBoost.getMethodBeanKey(method)); AsmMethodBean methodBean = selfMethodBeans.get(AsmMethodBoost.getMethodBeanKey(method));
List<String> fieldNames = methodBean.fieldNameList(); List<String> fieldNames = methodBean.paramNameList(method);
Class resultClass = resultClass(method); Class resultClass = resultClass(method);
int flipperIndex = -1; int flipperIndex = -1;
if (resultClass.isAssignableFrom(Sheet.class)) { if (resultClass.isAssignableFrom(Sheet.class)) {
@@ -397,6 +397,7 @@ public final class DataSqlMapperBuilder {
throw new SourceException( throw new SourceException(
"Entity Class " + entityClass.getName() + " must be on Annotation @Entity"); "Entity Class " + entityClass.getName() + " must be on Annotation @Entity");
} }
return entityClass;
} }
} }
throw new SourceException("Not found entity class from " + mapperType.getName()); throw new SourceException("Not found entity class from " + mapperType.getName());