|
18 | 18 | package net.devh.boot.grpc.server.advice;
|
19 | 19 |
|
20 | 20 | import java.lang.reflect.Method;
|
21 |
| -import java.util.Arrays; |
| 21 | +import java.util.Collection; |
22 | 22 | import java.util.Map;
|
23 | 23 | import java.util.Set;
|
24 |
| -import java.util.function.Function; |
25 | 24 | import java.util.stream.Collectors;
|
26 |
| -import java.util.stream.Stream; |
27 | 25 |
|
28 | 26 | import org.springframework.beans.factory.InitializingBean;
|
29 | 27 | import org.springframework.context.ApplicationContext;
|
30 | 28 | import org.springframework.context.ApplicationContextAware;
|
| 29 | +import org.springframework.core.MethodIntrospector; |
| 30 | +import org.springframework.core.annotation.AnnotatedElementUtils; |
31 | 31 | import org.springframework.util.Assert;
|
| 32 | +import org.springframework.util.ReflectionUtils.MethodFilter; |
32 | 33 |
|
33 | 34 | import lombok.extern.slf4j.Slf4j;
|
34 | 35 |
|
35 | 36 | /**
|
36 | 37 | * A discovery class to find all Beans annotated with {@link GrpcAdvice @GrpcAdvice} and for all found beans a second
|
37 |
| - * search is performed looking for methods with {@link GrpcExceptionHandler @GrpcExceptionHandler}.<br> |
38 |
| - * <p> |
39 |
| - * |
| 38 | + * search is performed looking for methods with {@link GrpcExceptionHandler @GrpcExceptionHandler}. |
| 39 | + * |
40 | 40 | * @author Andjelko Perisic ([email protected])
|
41 | 41 | * @see GrpcAdvice
|
42 | 42 | * @see GrpcExceptionHandler
|
43 | 43 | */
|
44 | 44 | @Slf4j
|
45 | 45 | public class GrpcAdviceDiscoverer implements InitializingBean, ApplicationContextAware {
|
46 | 46 |
|
| 47 | + /** |
| 48 | + * A filter for selecting {@code @GrpcExceptionHandler} methods. |
| 49 | + */ |
| 50 | + public static final MethodFilter EXCEPTION_HANDLER_METHODS = |
| 51 | + method -> AnnotatedElementUtils.hasAnnotation(method, GrpcExceptionHandler.class); |
| 52 | + |
47 | 53 | private ApplicationContext applicationContext;
|
48 | 54 | private Map<String, Object> annotatedBeans;
|
49 | 55 | private Set<Class<?>> annotatedClasses;
|
@@ -72,13 +78,16 @@ private Set<Class<?>> extractClassType() {
|
72 | 78 | }
|
73 | 79 |
|
74 | 80 | private Set<Method> findAnnotatedMethods() {
|
75 |
| - Function<Class<?>, Stream<Method>> extractMethodsFromClass = clazz -> Arrays.stream(clazz.getDeclaredMethods()); |
76 |
| - return annotatedClasses.stream() |
77 |
| - .flatMap(extractMethodsFromClass) |
78 |
| - .filter(method -> method.isAnnotationPresent(GrpcExceptionHandler.class)) |
| 81 | + return this.annotatedClasses.stream() |
| 82 | + .map(this::findAnnotatedMethods) |
| 83 | + .flatMap(Collection::stream) |
79 | 84 | .collect(Collectors.toSet());
|
80 | 85 | }
|
81 | 86 |
|
| 87 | + private Set<Method> findAnnotatedMethods(final Class<?> clazz) { |
| 88 | + return MethodIntrospector.selectMethods(clazz, EXCEPTION_HANDLER_METHODS); |
| 89 | + } |
| 90 | + |
82 | 91 | boolean isAnnotationPresent() {
|
83 | 92 | return !annotatedClasses.isEmpty() && !annotatedMethods.isEmpty();
|
84 | 93 | }
|
|
0 commit comments