Skip to content

Commit

Permalink
代码优化
Browse files Browse the repository at this point in the history
  • Loading branch information
javamxd committed Oct 6, 2021
1 parent 9a6cc40 commit 1e228da
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
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;

Expand All @@ -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;

Expand All @@ -45,22 +44,22 @@ public MagicWebSocketDispatcher(String instanceId, MagicNotifyService magicNotif
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)))
);
}

private static Object findHandleAndInvoke(MagicConsoleSession session, String payload) {
// 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++) {
Expand All @@ -78,7 +77,7 @@ private static Object findHandleAndInvoke(MagicConsoleSession session, String pa
pValues[i] = JsonUtils.readValue(payload, pType);
}
}
returnValue = invoker.invoke(null, null, pValues);
returnValue = invoker.invoke0(invoker.getDefaultTarget(), null, pValues);
}
return returnValue;
} catch (Throwable e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
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;
Expand All @@ -37,10 +36,12 @@
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;
Expand Down Expand Up @@ -248,9 +249,9 @@ private Object convertValue(DataType dataType, String name, String value) {
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()) {
Expand All @@ -259,7 +260,7 @@ private Object convertValue(DataType dataType, String name, String value) {
if (dataType.isNeedValue()) {
params.add(value);
}
return invoker.invoke(null, null, params.toArray());
return invoker.invoke0(null, null, params.toArray());
}
}
return value;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -75,7 +74,7 @@ public enum DataType {

private boolean isNumber;

private Invoker invoker;
private JavaInvoker<Method> invoker;

private boolean needName;

Expand All @@ -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;
Expand All @@ -108,7 +107,7 @@ public boolean isNumber() {
return isNumber;
}

public Invoker getInvoker() {
public JavaInvoker<Method> getInvoker() {
return invoker;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
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;

/**
Expand All @@ -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);
Expand All @@ -54,8 +55,8 @@ public MongoCollection<Document> get(Object collection) {
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);
Expand Down
39 changes: 0 additions & 39 deletions magic-api/src/main/java/org/ssssssss/magicapi/utils/Invoker.java

This file was deleted.

0 comments on commit 1e228da

Please sign in to comment.