I'm smart by the way

Forgot the files :)
This commit is contained in:
Daniel E 2019-11-10 11:38:49 -07:00
parent 20085e4e7c
commit 592f7feae4
2 changed files with 21 additions and 11 deletions

View File

@ -1,5 +1,15 @@
package team.stiff.pomelo.impl.annotated.handler.scan;
public class AnnotatedListenerPredicate {
import team.stiff.pomelo.impl.annotated.handler.annotation.Listener;
import java.lang.reflect.Method;
import java.util.function.Predicate;
public final class AnnotatedListenerPredicate implements Predicate<Method> {
@Override
public boolean test(final Method method) {
return method.isAnnotationPresent(Listener.class) &&
method.getParameterCount() == 1;
}
}

View File

@ -5,10 +5,12 @@ import team.stiff.pomelo.handler.EventHandler;
import team.stiff.pomelo.handler.scan.EventHandlerScanner;
import team.stiff.pomelo.impl.annotated.filter.MethodFilterScanner;
import team.stiff.pomelo.impl.annotated.handler.MethodEventHandler;
import team.stiff.pomelo.impl.annotated.handler.annotation.Listener;
import java.lang.reflect.Method;
import java.util.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Stream;
/**
@ -19,20 +21,18 @@ import java.util.stream.Stream;
* @since May 31, 2017
*/
public final class MethodHandlerScanner implements EventHandlerScanner {
private final AnnotatedListenerPredicate annotatedListenerPredicate =
new AnnotatedListenerPredicate();
private final EventFilterScanner<Method> filterScanner = new MethodFilterScanner();
@Override
public Map<Class<?>, Set<EventHandler>> locate(final Object listenerContainer) {
final Map<Class<?>, Set<EventHandler>> eventHandlers = new HashMap<>();
// todo; this could totally be faster right?
Stream.of(listenerContainer.getClass().getDeclaredMethods())
.filter(method -> method.isAnnotationPresent(Listener.class))
.filter(method -> method.getParameterCount() == 1)
.forEach(method -> eventHandlers
.computeIfAbsent(method.getParameterTypes()[0], obj -> new TreeSet<>())
.add(new MethodEventHandler(listenerContainer, method,
filterScanner.scan(method))));
.filter(annotatedListenerPredicate).forEach(method -> eventHandlers
.computeIfAbsent(method.getParameterTypes()[0], obj -> new TreeSet<>())
.add(new MethodEventHandler(listenerContainer, method,
filterScanner.scan(method))));
return eventHandlers;
}
}