Skip to content
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
import org.lognet.springboot.grpc.recovery.GRpcExceptionHandler;
import org.lognet.springboot.grpc.recovery.GRpcServiceAdvice;
import org.lognet.springboot.grpc.recovery.HandlerMethod;
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.MethodIntrospector;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.core.type.MethodMetadata;
import org.springframework.util.ReflectionUtils;

import java.lang.reflect.Method;
Expand All @@ -24,26 +27,35 @@ public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeM
.get("value");

ReflectionUtils.MethodFilter f = method -> AnnotatedElementUtils.hasAnnotation(method, GRpcExceptionHandler.class);
for(String adviceBeanName:context.getBeanFactory().getBeanNamesForAnnotation(GRpcServiceAdvice.class)){
final String beanClassName = context.getBeanFactory().getBeanDefinition(adviceBeanName)
.getBeanClassName();

for (String adviceBeanName : context.getBeanFactory().getBeanNamesForAnnotation(GRpcServiceAdvice.class)) {
BeanDefinition beanDefinition = context.getBeanFactory().getBeanDefinition(adviceBeanName);
String beanClassName = getBeanClassName(beanDefinition);
try {
for (Method method : MethodIntrospector.selectMethods(Class.forName(beanClassName), f)) {
final Optional<Class<? extends Throwable>> handledException = HandlerMethod.getHandledException(method, false);
if(handledException.isPresent() && handledException.get().isAssignableFrom(exc)){
if (handledException.isPresent() && handledException.get().isAssignableFrom(exc)) {
return ConditionOutcome.noMatch(String.format("Found %s handler at %s.%s",
handledException.get().getName(),
beanClassName,
method.getName()
));
handledException.get().getName(),
beanClassName,
method.getName()
));
}
}
} catch (ClassNotFoundException e) {
throw new IllegalStateException(e);
throw new IllegalStateException(e);
}
};
}

return ConditionOutcome.match();
}

private String getBeanClassName(BeanDefinition beanDefinition) {
if (beanDefinition instanceof AnnotatedBeanDefinition) {
MethodMetadata factoryMethodMetadata = ((AnnotatedBeanDefinition) beanDefinition).getFactoryMethodMetadata();
if (factoryMethodMetadata != null) {
return factoryMethodMetadata.getReturnTypeName();
}
}
return beanDefinition.getBeanClassName();
}
}