DataSqlMapperBuilder

This commit is contained in:
redkale
2024-01-09 23:39:06 +08:00
parent ce630ee253
commit 7d327e7864
2 changed files with 30 additions and 8 deletions

View File

@@ -10,6 +10,7 @@ import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.function.IntFunction;
import org.redkale.asm.AnnotationVisitor;
import org.redkale.asm.AsmMethodBean;
@@ -38,6 +39,7 @@ import org.redkale.asm.Type;
import org.redkale.persistence.Sql;
import org.redkale.source.AbstractDataSqlSource;
import org.redkale.source.DataNativeSqlInfo;
import static org.redkale.source.DataNativeSqlInfo.SqlMode.SELECT;
import org.redkale.source.DataNativeSqlParser;
import org.redkale.source.DataSqlMapper;
import org.redkale.source.DataSqlSource;
@@ -118,6 +120,9 @@ public final class DataSqlMapperBuilder {
if (!Modifier.isAbstract(method.getModifiers())) {
throw new SourceException(method + " is not abstract, but contains @" + Sql.class.getSimpleName());
}
if (method.getExceptionTypes().length > 0) {
throw new SourceException("@" + Sql.class.getSimpleName() + " cannot on throw-exception method, but " + method);
}
IntFunction<String> signFunc = null;
if (source instanceof AbstractDataSqlSource) {
signFunc = ((AbstractDataSqlSource) source).getSignFunc();
@@ -127,6 +132,14 @@ public final class DataSqlMapperBuilder {
if (!Utility.equalsElement(sqlInfo.getRootParamNames(), methodBean.fieldNameList())) {
throw new SourceException(method + " parameters not match @" + Sql.class.getSimpleName() + "(" + sql.value() + ")");
}
Class returnType = returnType(method);
if (sqlInfo.getSqlMode() != SELECT) {
if (returnType != Integer.class && returnType != int.class
&& returnType != Void.class && returnType != void.class) {
throw new SourceException("@" + Sql.class.getSimpleName()
+ "(" + sql.value() + ") must on return int or void method, but " + method);
}
}
items.add(new Item(method, sqlInfo, methodBean));
}
//------------------------------------------------------------------------------
@@ -157,9 +170,9 @@ public final class DataSqlMapperBuilder {
mv.visitEnd();
}
{
mv = cw.visitMethod(ACC_PUBLIC, "dataSource", "()Lorg/redkale/source/DataSqlSource;", null, null);
mv = cw.visitMethod(ACC_PUBLIC, "dataSource", "()" + sqlSourceDesc, null, null);
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, "org/redkale/test/source/parser/DynForumInfoMapperImpl", "source", sqlSourceDesc);
mv.visitFieldInsn(GETFIELD, newDynName, "_source", sqlSourceDesc);
mv.visitInsn(ARETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
@@ -167,7 +180,7 @@ public final class DataSqlMapperBuilder {
{
mv = cw.visitMethod(ACC_PUBLIC, "entityType", "()Ljava/lang/Class;", "()Ljava/lang/Class<" + entityDesc + ">;", null);
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, newDynName, "type", "Ljava/lang/Class;");
mv.visitFieldInsn(GETFIELD, newDynName, "_type", "Ljava/lang/Class;");
mv.visitInsn(ARETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
@@ -180,7 +193,7 @@ public final class DataSqlMapperBuilder {
AsmMethodBean methodBean = item.methodBean;
Sql sql = method.getAnnotation(Sql.class);
mv = cw.visitMethod(ACC_PUBLIC, "queryForumResultAsync", "(Lorg/redkale/test/source/parser/ForumBean;)Ljava/util/concurrent/CompletableFuture;", "(Lorg/redkale/test/source/parser/ForumBean;)Ljava/util/concurrent/CompletableFuture<Ljava/util/List<Lorg/redkale/test/source/parser/ForumResult;>;>;", null);
mv = cw.visitMethod(ACC_PUBLIC, method.getName(), methodBean.getDesc(), methodBean.getSignature(), null);
Label l0 = new Label();
mv.visitLabel(l0);
mv.visitLdcInsn(sql.value());
@@ -255,6 +268,15 @@ public final class DataSqlMapperBuilder {
throw new SourceException("Not found entity class from " + mapperType.getName());
}
private static Class returnType(Method method) {
Class type = method.getReturnType();
if (type.isAssignableFrom(CompletableFuture.class)) {
ParameterizedType pt = (ParameterizedType) method.getGenericReturnType();
return TypeToken.typeToClass(pt.getActualTypeArguments()[0]);
}
return type;
}
private static class Item {
public Method method;

View File

@@ -15,9 +15,9 @@ import org.redkale.source.DataSqlSource;
*/
public class DynForumInfoMapperImpl implements ForumInfoMapper {
private DataSqlSource source;
private DataSqlSource _source;
private Class type;
private Class _type;
@Override
public ForumResult findForumResult(ForumBean bean) {
@@ -51,12 +51,12 @@ public class DynForumInfoMapperImpl implements ForumInfoMapper {
@Override
public DataSqlSource dataSource() {
return source;
return _source;
}
@Override
public Class<ForumInfo> entityType() {
return type;
return _type;
}
}