优化Rest.createRestServlet
This commit is contained in:
@@ -256,17 +256,19 @@ public class HttpServer extends Server<String, HttpContext, HttpRequest, HttpRes
|
|||||||
final boolean sncp = Sncp.isSncpDyn(service);
|
final boolean sncp = Sncp.isSncpDyn(service);
|
||||||
final String resname = name == null ? (sncp ? Sncp.getResourceName(service) : "") : name;
|
final String resname = name == null ? (sncp ? Sncp.getResourceName(service) : "") : name;
|
||||||
final Class<S> serviceType = Sncp.getServiceType(service);
|
final Class<S> serviceType = Sncp.getServiceType(service);
|
||||||
for (final HttpServlet item : ((HttpPrepareServlet) this.prepare).getServlets()) {
|
if (name != null) {
|
||||||
if (!(item instanceof HttpServlet)) continue;
|
for (final HttpServlet item : ((HttpPrepareServlet) this.prepare).getServlets()) {
|
||||||
if (item.getClass().getAnnotation(Rest.RestDyn.class) == null) continue;
|
if (!(item instanceof HttpServlet)) continue;
|
||||||
try {
|
if (item.getClass().getAnnotation(Rest.RestDyn.class) == null) continue;
|
||||||
Field field = item.getClass().getDeclaredField(Rest.REST_SERVICE_FIELD_NAME);
|
try {
|
||||||
if (serviceType.equals(field.getType())) {
|
Field field = item.getClass().getDeclaredField(Rest.REST_SERVICE_FIELD_NAME);
|
||||||
servlet = (T) item;
|
if (serviceType.equals(field.getType())) {
|
||||||
break;
|
servlet = (T) item;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} catch (NoSuchFieldException | SecurityException e) {
|
||||||
|
logger.log(Level.SEVERE, "serviceType = " + serviceType + ", servletClass = " + item.getClass(), e);
|
||||||
}
|
}
|
||||||
} catch (NoSuchFieldException | SecurityException e) {
|
|
||||||
logger.log(Level.SEVERE, "serviceType = " + serviceType + ", servletClass = " + item.getClass(), e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
final boolean first = servlet == null;
|
final boolean first = servlet == null;
|
||||||
|
|||||||
@@ -39,6 +39,8 @@ public final class Rest {
|
|||||||
|
|
||||||
static final String REST_SERVICE_FIELD_NAME = "_redkale_service";
|
static final String REST_SERVICE_FIELD_NAME = "_redkale_service";
|
||||||
|
|
||||||
|
static final String REST_TOSTRINGOBJ_FIELD_NAME = "_redkale_tostringsupplier";
|
||||||
|
|
||||||
static final String REST_JSONCONVERT_FIELD_PREFIX = "_redkale_jsonconvert_";
|
static final String REST_JSONCONVERT_FIELD_PREFIX = "_redkale_jsonconvert_";
|
||||||
|
|
||||||
static final String REST_SERVICEMAP_FIELD_NAME = "_redkale_servicemap"; //如果只有name=""的Service资源,则实例中_servicemap必须为null
|
static final String REST_SERVICEMAP_FIELD_NAME = "_redkale_servicemap"; //如果只有name=""的Service资源,则实例中_servicemap必须为null
|
||||||
@@ -251,7 +253,19 @@ public final class Rest {
|
|||||||
final String resourceGenericDescriptor = sb1.length() == sb2.length() ? null : sb2.toString();
|
final String resourceGenericDescriptor = sb1.length() == sb2.length() ? null : sb2.toString();
|
||||||
|
|
||||||
//----------------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------------
|
||||||
final Map<String, List<String>> asmParamMap = MethodParamClassVisitor.getMethodParamNames(new HashMap<>(), webSocketType);
|
boolean namePresent = false;
|
||||||
|
try {
|
||||||
|
Method m0 = null;
|
||||||
|
for (Method method : webSocketType.getMethods()) {
|
||||||
|
if (method.getParameterCount() > 0) {
|
||||||
|
m0 = method;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
namePresent = m0 == null ? true : m0.getParameters()[0].isNamePresent();
|
||||||
|
} catch (Exception e) {
|
||||||
|
}
|
||||||
|
final Map<String, List<String>> asmParamMap = namePresent ? null : MethodParamClassVisitor.getMethodParamNames(new HashMap<>(), webSocketType);
|
||||||
final Set<String> messageNames = new HashSet<>();
|
final Set<String> messageNames = new HashSet<>();
|
||||||
final List<Method> messageMethods = new ArrayList<>();
|
final List<Method> messageMethods = new ArrayList<>();
|
||||||
for (Method method : webSocketType.getMethods()) {
|
for (Method method : webSocketType.getMethods()) {
|
||||||
@@ -849,6 +863,10 @@ public final class Rest {
|
|||||||
fv = cw.visitField(ACC_PRIVATE, REST_PARAMTYPES_FIELD_NAME, "[[Ljava/lang/reflect/Type;", null, null);
|
fv = cw.visitField(ACC_PRIVATE, REST_PARAMTYPES_FIELD_NAME, "[[Ljava/lang/reflect/Type;", null, null);
|
||||||
fv.visitEnd();
|
fv.visitEnd();
|
||||||
}
|
}
|
||||||
|
{ //_redkale_tostringsupplier字段 Supplier<String>
|
||||||
|
fv = cw.visitField(ACC_PRIVATE, REST_TOSTRINGOBJ_FIELD_NAME, "Ljava/util/function/Supplier;", "Ljava/util/function/Supplier<Ljava/lang/String;>;", null);
|
||||||
|
fv.visitEnd();
|
||||||
|
}
|
||||||
{ //构造函数
|
{ //构造函数
|
||||||
mv = new MethodDebugVisitor(cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null));
|
mv = new MethodDebugVisitor(cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null));
|
||||||
//mv.setDebug(true);
|
//mv.setDebug(true);
|
||||||
@@ -860,7 +878,19 @@ public final class Rest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//将每个Service可转换的方法生成HttpServlet对应的HttpMapping方法
|
//将每个Service可转换的方法生成HttpServlet对应的HttpMapping方法
|
||||||
final Map<String, List<String>> asmParamMap = MethodParamClassVisitor.getMethodParamNames(new HashMap<>(), serviceType);
|
boolean namePresent = false;
|
||||||
|
try {
|
||||||
|
Method m0 = null;
|
||||||
|
for (final MappingEntry entry : entrys) {
|
||||||
|
if (entry.mappingMethod.getParameterCount() > 0) {
|
||||||
|
m0 = entry.mappingMethod;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
namePresent = m0 == null ? true : m0.getParameters()[0].isNamePresent();
|
||||||
|
} catch (Exception e) {
|
||||||
|
}
|
||||||
|
final Map<String, List<String>> asmParamMap = namePresent ? null : MethodParamClassVisitor.getMethodParamNames(new HashMap<>(), serviceType);
|
||||||
final Map<String, java.lang.reflect.Type> bodyTypes = new HashMap<>();
|
final Map<String, java.lang.reflect.Type> bodyTypes = new HashMap<>();
|
||||||
|
|
||||||
final List<Object[]> restConverts = new ArrayList<>();
|
final List<Object[]> restConverts = new ArrayList<>();
|
||||||
@@ -1729,11 +1759,14 @@ public final class Rest {
|
|||||||
fv.visitEnd();
|
fv.visitEnd();
|
||||||
}
|
}
|
||||||
|
|
||||||
//classMap.put("mappings", mappingMaps); //不显示太多信息
|
//classMap.put("mappings", mappingMaps); //不显示太多信息
|
||||||
{ //toString函数
|
{ //toString函数
|
||||||
mv = new MethodDebugVisitor(cw.visitMethod(ACC_PUBLIC, "toString", "()Ljava/lang/String;", null, null));
|
mv = new MethodDebugVisitor(cw.visitMethod(ACC_PUBLIC, "toString", "()Ljava/lang/String;", null, null));
|
||||||
//mv.setDebug(true);
|
//mv.setDebug(true);
|
||||||
mv.visitLdcInsn(JsonConvert.root().convertTo(classMap));
|
mv.visitVarInsn(ALOAD, 0);
|
||||||
|
mv.visitFieldInsn(GETFIELD, newDynName, REST_TOSTRINGOBJ_FIELD_NAME, "Ljava/util/function/Supplier;");
|
||||||
|
mv.visitMethodInsn(INVOKEINTERFACE, "java/util/function/Supplier", "get", "()Ljava/lang/Object;", true);
|
||||||
|
mv.visitTypeInsn(CHECKCAST, "java/lang/String");
|
||||||
mv.visitInsn(ARETURN);
|
mv.visitInsn(ARETURN);
|
||||||
mv.visitMaxs(1, 1);
|
mv.visitMaxs(1, 1);
|
||||||
mv.visitEnd();
|
mv.visitEnd();
|
||||||
@@ -1767,6 +1800,11 @@ public final class Rest {
|
|||||||
paramtypeArray = paramtypes.toArray(paramtypeArray);
|
paramtypeArray = paramtypes.toArray(paramtypeArray);
|
||||||
typesfield.set(obj, paramtypeArray);
|
typesfield.set(obj, paramtypeArray);
|
||||||
|
|
||||||
|
Field tostringfield = newClazz.getDeclaredField(REST_TOSTRINGOBJ_FIELD_NAME);
|
||||||
|
tostringfield.setAccessible(true);
|
||||||
|
java.util.function.Supplier<String> sSupplier = () -> JsonConvert.root().convertTo(classMap);
|
||||||
|
tostringfield.set(obj, sSupplier);
|
||||||
|
|
||||||
return obj;
|
return obj;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
|
|||||||
Reference in New Issue
Block a user