Skip to content

Commit f311eb6

Browse files
author
robdu
committed
add setAccessible in Protocol for performance.
1 parent 3ef15fb commit f311eb6

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

core/main/java/com/codingchili/core/protocol/Protocol.java

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,15 @@ private void setHandlerProperties(Class<?> handlerClass) {
126126

127127
private void setHandlerRoutes(Receiver<RequestType> handler) {
128128
for (Method method : handler.getClass().getDeclaredMethods()) {
129+
if (!getClass().getModule().isNamed()) {
130+
handler.getClass().getModule().addOpens(
131+
handler.getClass().getPackageName(),
132+
getClass().getModule()
133+
);
134+
}
135+
// improve reflection performance by up to 50%.
136+
method.setAccessible(true);
137+
129138
readMapper(method, handler);
130139
readAuthenticator(method, handler);
131140
readApi(method, handler);
@@ -211,17 +220,16 @@ public Protocol<RequestType> setRole(RoleType... role) {
211220
}
212221

213222
private void wrap(String route, Receiver<RequestType> handler, Method method, RoleType[] role) {
214-
use(route, request -> {
215-
try {
216-
method.invoke(handler, request);
217-
} catch (Throwable e) {
218-
if (e instanceof RuntimeException) {
219-
throw (RuntimeException) e;
220-
} else {
221-
throw new RuntimeException(e);
222-
}
223-
}
224-
}, role);
223+
use(route, request -> invokeMethod(method, handler, request), role);
224+
}
225+
226+
@SuppressWarnings("unchecked")
227+
private <E> E invokeMethod(Method method, Object instance, Object argument) {
228+
try {
229+
return (E) method.invoke(instance, argument);
230+
} catch (IllegalAccessException | InvocationTargetException e) {
231+
throw new RuntimeException(e);
232+
}
225233
}
226234

227235
/**

0 commit comments

Comments
 (0)