Skip to content

Commit a926211

Browse files
committed
Fix GrpcAdviceDiscoverer
1 parent 9aca507 commit a926211

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

grpc-server-spring-boot-autoconfigure/src/main/java/net/devh/boot/grpc/server/advice/GrpcAdviceDiscoverer.java

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,38 @@
1818
package net.devh.boot.grpc.server.advice;
1919

2020
import java.lang.reflect.Method;
21-
import java.util.Arrays;
21+
import java.util.Collection;
2222
import java.util.Map;
2323
import java.util.Set;
24-
import java.util.function.Function;
2524
import java.util.stream.Collectors;
26-
import java.util.stream.Stream;
2725

2826
import org.springframework.beans.factory.InitializingBean;
2927
import org.springframework.context.ApplicationContext;
3028
import org.springframework.context.ApplicationContextAware;
29+
import org.springframework.core.MethodIntrospector;
30+
import org.springframework.core.annotation.AnnotatedElementUtils;
3131
import org.springframework.util.Assert;
32+
import org.springframework.util.ReflectionUtils.MethodFilter;
3233

3334
import lombok.extern.slf4j.Slf4j;
3435

3536
/**
3637
* 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+
*
4040
* @author Andjelko Perisic ([email protected])
4141
* @see GrpcAdvice
4242
* @see GrpcExceptionHandler
4343
*/
4444
@Slf4j
4545
public class GrpcAdviceDiscoverer implements InitializingBean, ApplicationContextAware {
4646

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+
4753
private ApplicationContext applicationContext;
4854
private Map<String, Object> annotatedBeans;
4955
private Set<Class<?>> annotatedClasses;
@@ -72,13 +78,16 @@ private Set<Class<?>> extractClassType() {
7278
}
7379

7480
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)
7984
.collect(Collectors.toSet());
8085
}
8186

87+
private Set<Method> findAnnotatedMethods(final Class<?> clazz) {
88+
return MethodIntrospector.selectMethods(clazz, EXCEPTION_HANDLER_METHODS);
89+
}
90+
8291
boolean isAnnotationPresent() {
8392
return !annotatedClasses.isEmpty() && !annotatedMethods.isEmpty();
8493
}

0 commit comments

Comments
 (0)