mxd 3 жил өмнө
parent
commit
1e228da45a

+ 5 - 6
magic-api/src/main/java/org/ssssssss/magicapi/controller/MagicWebSocketDispatcher.java

@@ -12,7 +12,6 @@ import org.ssssssss.magicapi.model.Constants;
 import org.ssssssss.magicapi.model.MagicConsoleSession;
 import org.ssssssss.magicapi.model.MagicNotify;
 import org.ssssssss.magicapi.provider.MagicNotifyService;
-import org.ssssssss.magicapi.utils.Invoker;
 import org.ssssssss.magicapi.utils.JsonUtils;
 import org.ssssssss.script.reflection.MethodInvoker;
 
@@ -32,7 +31,7 @@ public class MagicWebSocketDispatcher extends TextWebSocketHandler {
 
 	private static final Logger logger = LoggerFactory.getLogger(MagicWebSocketDispatcher.class);
 
-	private static final Map<String, Invoker> HANDLERS = new HashMap<>();
+	private static final Map<String, MethodInvoker> HANDLERS = new HashMap<>();
 
 	private final String instanceId;
 
@@ -45,7 +44,7 @@ public class MagicWebSocketDispatcher extends TextWebSocketHandler {
 		WebSocketSessionManager.setInstanceId(instanceId);
 		websocketMessageHandlers.forEach(websocketMessageHandler ->
 				Stream.of(websocketMessageHandler.getClass().getDeclaredMethods())
-						.forEach(method -> HANDLERS.put(method.getAnnotation(Message.class).value().name().toLowerCase(), Invoker.from(new MethodInvoker(method, websocketMessageHandler))))
+						.forEach(method -> HANDLERS.put(method.getAnnotation(Message.class).value().name().toLowerCase(), new MethodInvoker(method, websocketMessageHandler)))
 		);
 	}
 
@@ -53,14 +52,14 @@ public class MagicWebSocketDispatcher extends TextWebSocketHandler {
 		// messageType[, data][,data]
 		int index = payload.indexOf(",");
 		String msgType = index == -1 ? payload : payload.substring(0, index);
-		Invoker invoker = HANDLERS.get(msgType);
+		MethodInvoker invoker = HANDLERS.get(msgType);
 		if (invoker != null) {
 			Object returnValue;
 			try {
 				Class<?>[] pTypes = invoker.getParameterTypes();
 				int pCount = pTypes.length;
 				if (pCount == 0) {
-					returnValue = invoker.invoke(null, null, EMPTY_OBJECT_ARRAY);
+					returnValue = invoker.invoke0(invoker.getDefaultTarget(), null, EMPTY_OBJECT_ARRAY);
 				} else {
 					Object[] pValues = new Object[pCount];
 					for (int i = 0; i < pCount; i++) {
@@ -78,7 +77,7 @@ public class MagicWebSocketDispatcher extends TextWebSocketHandler {
 							pValues[i] = JsonUtils.readValue(payload, pType);
 						}
 					}
-					returnValue = invoker.invoke(null, null, pValues);
+					returnValue = invoker.invoke0(invoker.getDefaultTarget(), null, pValues);
 				}
 				return returnValue;
 			} catch (Throwable e) {

+ 5 - 4
magic-api/src/main/java/org/ssssssss/magicapi/controller/RequestHandler.java

@@ -27,7 +27,6 @@ import org.ssssssss.magicapi.model.*;
 import org.ssssssss.magicapi.modules.ResponseModule;
 import org.ssssssss.magicapi.provider.ResultProvider;
 import org.ssssssss.magicapi.script.ScriptManager;
-import org.ssssssss.magicapi.utils.Invoker;
 import org.ssssssss.magicapi.utils.JsonUtils;
 import org.ssssssss.magicapi.utils.PatternUtils;
 import org.ssssssss.script.MagicScriptContext;
@@ -37,10 +36,12 @@ import org.ssssssss.script.exception.MagicScriptException;
 import org.ssssssss.script.functions.ObjectConvertExtension;
 import org.ssssssss.script.parsing.Span;
 import org.ssssssss.script.parsing.ast.literal.BooleanLiteral;
+import org.ssssssss.script.reflection.JavaInvoker;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import java.io.IOException;
+import java.lang.reflect.Method;
 import java.math.BigDecimal;
 import java.util.*;
 import java.util.stream.Collectors;
@@ -248,9 +249,9 @@ public class RequestHandler extends MagicController {
 				if (decimal == null) {
 					throw new IllegalArgumentException();
 				}
-				return dataType.getInvoker().invoke(decimal, null, EMPTY_OBJECT_ARRAY);
+				return dataType.getInvoker().invoke0(decimal, null, EMPTY_OBJECT_ARRAY);
 			} else {
-				Invoker invoker = dataType.getInvoker();
+				JavaInvoker<Method> invoker = dataType.getInvoker();
 				if (invoker != null) {
 					List<Object> params = new ArrayList<>();
 					if (dataType.isNeedName()) {
@@ -259,7 +260,7 @@ public class RequestHandler extends MagicController {
 					if (dataType.isNeedValue()) {
 						params.add(value);
 					}
-					return invoker.invoke(null, null, params.toArray());
+					return invoker.invoke0(null, null, params.toArray());
 				}
 			}
 			return value;

+ 3 - 4
magic-api/src/main/java/org/ssssssss/magicapi/model/DataType.java

@@ -1,7 +1,6 @@
 package org.ssssssss.magicapi.model;
 
 import org.ssssssss.magicapi.modules.RequestModule;
-import org.ssssssss.magicapi.utils.Invoker;
 import org.ssssssss.script.reflection.JavaInvoker;
 
 import java.lang.reflect.Method;
@@ -75,7 +74,7 @@ public enum DataType {
 
 	private boolean isNumber;
 
-	private Invoker invoker;
+	private JavaInvoker<Method> invoker;
 
 	private boolean needName;
 
@@ -85,7 +84,7 @@ public enum DataType {
 
 	DataType(boolean isNumber, JavaInvoker<Method> invoker, boolean needName, boolean needValue, String javascriptType) {
 		this.isNumber = isNumber;
-		this.invoker = Invoker.from(invoker);
+		this.invoker = invoker;
 		this.needName = needName;
 		this.needValue = needValue;
 		this.javascriptType = javascriptType;
@@ -108,7 +107,7 @@ public enum DataType {
 		return isNumber;
 	}
 
-	public Invoker getInvoker() {
+	public JavaInvoker<Method> getInvoker() {
 		return invoker;
 	}
 

+ 10 - 9
magic-api/src/main/java/org/ssssssss/magicapi/modules/MongoModule.java

@@ -9,9 +9,10 @@ import org.slf4j.LoggerFactory;
 import org.springframework.data.mongodb.core.MongoTemplate;
 import org.ssssssss.magicapi.config.MagicModule;
 import org.ssssssss.magicapi.model.Constants;
-import org.ssssssss.magicapi.utils.Invoker;
+import org.ssssssss.script.reflection.JavaInvoker;
 import org.ssssssss.script.reflection.JavaReflection;
 
+import java.lang.reflect.Method;
 import java.util.HashMap;
 
 /**
@@ -24,18 +25,18 @@ public class MongoModule extends HashMap<String, Object> implements MagicModule
 	private static final Logger logger = LoggerFactory.getLogger(MongoModule.class);
 
 	private final MongoTemplate mongoTemplate;
-	private final Invoker mongoDbFactoryInvoker;
-	private Invoker invoker;
+	private final JavaInvoker<Method> mongoDbFactoryInvoker;
+	private JavaInvoker<Method> invoker;
 
 	public MongoModule(MongoTemplate mongoTemplate) {
 		this.mongoTemplate = mongoTemplate;
-		mongoDbFactoryInvoker = Invoker.from(JavaReflection.getMethod(this.mongoTemplate, "getMongoDbFactory"));
+		mongoDbFactoryInvoker = JavaReflection.getMethod(this.mongoTemplate, "getMongoDbFactory");
 		if (mongoDbFactoryInvoker != null) {
 			try {
-				Object factory = mongoDbFactoryInvoker.invoke(this.mongoTemplate, null, Constants.EMPTY_OBJECT_ARRAY);
-				invoker = Invoker.from(JavaReflection.getMethod(factory, "getDb", StringUtils.EMPTY));
+				Object factory = mongoDbFactoryInvoker.invoke0(this.mongoTemplate, null, Constants.EMPTY_OBJECT_ARRAY);
+				invoker = JavaReflection.getMethod(factory, "getDb", StringUtils.EMPTY);
 				if (invoker == null) {
-					invoker = Invoker.from(JavaReflection.getMethod(factory, "getMongoDatabase", StringUtils.EMPTY));
+					invoker = JavaReflection.getMethod(factory, "getMongoDatabase", StringUtils.EMPTY);
 				}
 			} catch (Throwable e) {
 				logger.error("mongo模块初始化失败", e);
@@ -54,8 +55,8 @@ public class MongoModule extends HashMap<String, Object> implements MagicModule
 					return null;
 				}
 				try {
-					Object factory = mongoDbFactoryInvoker.invoke(mongoTemplate, null, Constants.EMPTY_OBJECT_ARRAY);
-					MongoDatabase database = (MongoDatabase) invoker.invoke(factory, null, new Object[]{databaseName.toString()});
+					Object factory = mongoDbFactoryInvoker.invoke0(mongoTemplate, null, Constants.EMPTY_OBJECT_ARRAY);
+					MongoDatabase database = (MongoDatabase) invoker.invoke0(factory, null, new Object[]{databaseName.toString()});
 					return database.getCollection(collection.toString());
 				} catch (Throwable throwable) {
 					throw new RuntimeException(throwable);

+ 0 - 39
magic-api/src/main/java/org/ssssssss/magicapi/utils/Invoker.java

@@ -1,39 +0,0 @@
-package org.ssssssss.magicapi.utils;
-
-import org.ssssssss.script.reflection.JavaInvoker;
-
-import java.lang.reflect.Method;
-import java.util.Arrays;
-
-/**
- * TODO 此类兼容ASM分支处理,待切换至ASM分支时删除。
- */
-public class Invoker {
-
-	private static final Method invokeMethod;
-
-	static {
-		invokeMethod = Arrays.stream(JavaInvoker.class.getDeclaredMethods())
-				.filter(it -> "invoke0".equals(it.getName()))
-				.findFirst()
-				.orElse(null);
-	}
-
-	private final JavaInvoker<?> target;
-
-	private Invoker(JavaInvoker<?> target) {
-		this.target = target;
-	}
-
-	public static Invoker from(JavaInvoker<?> target) {
-		return target == null ? null : new Invoker(target);
-	}
-
-	public Class<?>[] getParameterTypes() {
-		return this.target.getParameterTypes();
-	}
-
-	public Object invoke(Object target, Object context, Object... args) throws Throwable {
-		return invokeMethod.invoke(this.target, target, context, args);
-	}
-}