33import org .lognet .springboot .grpc .recovery .GRpcExceptionHandler ;
44import org .lognet .springboot .grpc .recovery .GRpcServiceAdvice ;
55import org .lognet .springboot .grpc .recovery .HandlerMethod ;
6+ import org .springframework .beans .factory .annotation .AnnotatedBeanDefinition ;
7+ import org .springframework .beans .factory .config .BeanDefinition ;
68import org .springframework .boot .autoconfigure .condition .ConditionOutcome ;
79import org .springframework .boot .autoconfigure .condition .SpringBootCondition ;
810import org .springframework .context .annotation .ConditionContext ;
911import org .springframework .core .MethodIntrospector ;
1012import org .springframework .core .annotation .AnnotatedElementUtils ;
1113import org .springframework .core .type .AnnotatedTypeMetadata ;
14+ import org .springframework .core .type .MethodMetadata ;
1215import org .springframework .util .ReflectionUtils ;
1316
1417import java .lang .reflect .Method ;
@@ -24,26 +27,33 @@ public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeM
2427 .get ("value" );
2528
2629 ReflectionUtils .MethodFilter f = method -> AnnotatedElementUtils .hasAnnotation (method , GRpcExceptionHandler .class );
27- for (String adviceBeanName :context .getBeanFactory ().getBeanNamesForAnnotation (GRpcServiceAdvice .class )){
28- final String beanClassName = context .getBeanFactory ().getBeanDefinition (adviceBeanName )
29- .getBeanClassName ();
30-
30+ for (String adviceBeanName : context .getBeanFactory ().getBeanNamesForAnnotation (GRpcServiceAdvice .class )) {
31+ final String beanClassName = getBeanClassName (context .getBeanFactory ().getBeanDefinition (adviceBeanName ));
3132 try {
3233 for (Method method : MethodIntrospector .selectMethods (Class .forName (beanClassName ), f )) {
3334 final Optional <Class <? extends Throwable >> handledException = HandlerMethod .getHandledException (method , false );
34- if (handledException .isPresent () && handledException .get ().isAssignableFrom (exc )){
35+ if (handledException .isPresent () && handledException .get ().isAssignableFrom (exc )) {
3536 return ConditionOutcome .noMatch (String .format ("Found %s handler at %s.%s" ,
36- handledException .get ().getName (),
37- beanClassName ,
38- method .getName ()
39- ));
37+ handledException .get ().getName (),
38+ beanClassName ,
39+ method .getName ()
40+ ));
4041 }
4142 }
4243 } catch (ClassNotFoundException e ) {
43- throw new IllegalStateException (e );
44+ throw new IllegalStateException (e );
4445 }
45- };
46+ }
4647
4748 return ConditionOutcome .match ();
4849 }
50+
51+ private String getBeanClassName (BeanDefinition beanDefinition ) {
52+ if (beanDefinition instanceof AnnotatedBeanDefinition ) { // definition with @Bean Annotation cause this issue
53+ MethodMetadata factoryMethodMetadata = ((AnnotatedBeanDefinition ) beanDefinition ).getFactoryMethodMetadata ();
54+ return factoryMethodMetadata .getReturnTypeName ();
55+ } else {
56+ return beanDefinition .getBeanClassName ();
57+ }
58+ }
4959}
0 commit comments