Use stream to scan for annotations and to execute the methods

This commit is contained in:
chase 2019-01-19 09:49:44 +01:00
parent 61d1eb3319
commit 05c92d4037

View File

@ -51,7 +51,8 @@ public class TelegramBotInitializer implements InitializingBean {
}
}
private void handleAnnotatedMethod(Object bot, Method method, BotSession session) throws InvocationTargetException, IllegalAccessException {
private void handleAnnotatedMethod(Object bot, Method method, BotSession session) {
try {
if (method.getParameterCount() > 1) {
BotLogger.warn(this.getClass().getSimpleName(),
format("Method %s of Type %s has too many parameters",
@ -75,22 +76,19 @@ public class TelegramBotInitializer implements InitializingBean {
method.getDeclaringClass().getCanonicalName()
)
);
}
private void handleAfterRegistrationHook(Object bot, BotSession botSession) {
for (Method m : bot.getClass().getMethods()) {
Stream.of(m.getAnnotations()).forEach(annotation -> System.out.println(annotation.annotationType().getName()));
if (m.getAnnotation(AfterBotRegistration.class) != null) {
try {
handleAnnotatedMethod(bot, m, botSession);
} catch (InvocationTargetException | IllegalAccessException e) {
BotLogger.error(this.getClass().getSimpleName(),
format("Couldn't invoke Method %s of Type %s",
m.getName(), m.getDeclaringClass().getCanonicalName()
method.getName(), method.getDeclaringClass().getCanonicalName()
)
);
}
}
}
private void handleAfterRegistrationHook(Object bot, BotSession botSession) {
Stream.of(bot.getClass().getMethods())
.filter(method -> method.getAnnotation(AfterBotRegistration.class) != null)
.forEach(method -> handleAnnotatedMethod(bot, method, botSession));
}
}