Skip to content

Commit

Permalink
add setAccessible in Protocol for performance.
Browse files Browse the repository at this point in the history
  • Loading branch information
robdu committed Sep 2, 2020
1 parent 3ef15fb commit f311eb6
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions core/main/java/com/codingchili/core/protocol/Protocol.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,15 @@ private void setHandlerProperties(Class<?> handlerClass) {

private void setHandlerRoutes(Receiver<RequestType> 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);
Expand Down Expand Up @@ -211,17 +220,16 @@ public Protocol<RequestType> setRole(RoleType... role) {
}

private void wrap(String route, Receiver<RequestType> 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> E invokeMethod(Method method, Object instance, Object argument) {
try {
return (E) method.invoke(instance, argument);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}

/**
Expand Down

0 comments on commit f311eb6

Please sign in to comment.