From f311eb66b52e326cd516864adae016d3970858d7 Mon Sep 17 00:00:00 2001 From: robdu Date: Wed, 2 Sep 2020 19:14:33 +0200 Subject: [PATCH] add setAccessible in Protocol for performance. --- .../codingchili/core/protocol/Protocol.java | 30 ++++++++++++------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/core/main/java/com/codingchili/core/protocol/Protocol.java b/core/main/java/com/codingchili/core/protocol/Protocol.java index aee11fe0..a6f4394e 100644 --- a/core/main/java/com/codingchili/core/protocol/Protocol.java +++ b/core/main/java/com/codingchili/core/protocol/Protocol.java @@ -126,6 +126,15 @@ private void setHandlerProperties(Class handlerClass) { private void setHandlerRoutes(Receiver handler) { for (Method method : handler.getClass().getDeclaredMethods()) { + if (!getClass().getModule().isNamed()) { + handler.getClass().getModule().addOpens( + handler.getClass().getPackageName(), + getClass().getModule() + ); + } + // improve reflection performance by up to 50%. + method.setAccessible(true); + readMapper(method, handler); readAuthenticator(method, handler); readApi(method, handler); @@ -211,17 +220,16 @@ public Protocol setRole(RoleType... role) { } private void wrap(String route, Receiver handler, Method method, RoleType[] role) { - use(route, request -> { - try { - method.invoke(handler, request); - } catch (Throwable e) { - if (e instanceof RuntimeException) { - throw (RuntimeException) e; - } else { - throw new RuntimeException(e); - } - } - }, role); + use(route, request -> invokeMethod(method, handler, request), role); + } + + @SuppressWarnings("unchecked") + private E invokeMethod(Method method, Object instance, Object argument) { + try { + return (E) method.invoke(instance, argument); + } catch (IllegalAccessException | InvocationTargetException e) { + throw new RuntimeException(e); + } } /**