Java Code Examples for org.springframework.web.method.ControllerAdviceBean#resolveBean()

The following examples show how to use org.springframework.web.method.ControllerAdviceBean#resolveBean() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RequestResponseBodyAdviceChain.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@SuppressWarnings("unchecked")
private <A> List<A> getMatchingAdvice(MethodParameter parameter, Class<? extends A> adviceType) {
	List<Object> availableAdvice = getAdvice(adviceType);
	if (CollectionUtils.isEmpty(availableAdvice)) {
		return Collections.emptyList();
	}
	List<A> result = new ArrayList<>(availableAdvice.size());
	for (Object advice : availableAdvice) {
		if (advice instanceof ControllerAdviceBean) {
			ControllerAdviceBean adviceBean = (ControllerAdviceBean) advice;
			if (!adviceBean.isApplicableToBeanType(parameter.getContainingClass())) {
				continue;
			}
			advice = adviceBean.resolveBean();
		}
		if (adviceType.isAssignableFrom(advice.getClass())) {
			result.add((A) advice);
		}
	}
	return result;
}
 
Example 2
Source File: RequestResponseBodyAdviceChain.java    From java-technology-stack with MIT License 6 votes vote down vote up
@SuppressWarnings("unchecked")
private <A> List<A> getMatchingAdvice(MethodParameter parameter, Class<? extends A> adviceType) {
	List<Object> availableAdvice = getAdvice(adviceType);
	if (CollectionUtils.isEmpty(availableAdvice)) {
		return Collections.emptyList();
	}
	List<A> result = new ArrayList<>(availableAdvice.size());
	for (Object advice : availableAdvice) {
		if (advice instanceof ControllerAdviceBean) {
			ControllerAdviceBean adviceBean = (ControllerAdviceBean) advice;
			if (!adviceBean.isApplicableToBeanType(parameter.getContainingClass())) {
				continue;
			}
			advice = adviceBean.resolveBean();
		}
		if (adviceType.isAssignableFrom(advice.getClass())) {
			result.add((A) advice);
		}
	}
	return result;
}
 
Example 3
Source File: RequestResponseBodyAdviceChain.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private <A> List<A> getMatchingAdvice(MethodParameter parameter, Class<? extends A> adviceType) {
	List<Object> availableAdvice = getAdvice(adviceType);
	if (CollectionUtils.isEmpty(availableAdvice)) {
		return Collections.emptyList();
	}
	List<A> result = new ArrayList<A>(availableAdvice.size());
	for (Object advice : availableAdvice) {
		if (advice instanceof ControllerAdviceBean) {
			ControllerAdviceBean adviceBean = (ControllerAdviceBean) advice;
			if (!adviceBean.isApplicableToBeanType(parameter.getContainingClass())) {
				continue;
			}
			advice = adviceBean.resolveBean();
		}
		if (adviceType.isAssignableFrom(advice.getClass())) {
			result.add((A) advice);
		}
	}
	return result;
}
 
Example 4
Source File: RequestResponseBodyAdviceChain.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private <A> List<A> getMatchingAdvice(MethodParameter parameter, Class<? extends A> adviceType) {
	List<Object> availableAdvice = getAdvice(adviceType);
	if (CollectionUtils.isEmpty(availableAdvice)) {
		return Collections.emptyList();
	}
	List<A> result = new ArrayList<A>(availableAdvice.size());
	for (Object advice : availableAdvice) {
		if (advice instanceof ControllerAdviceBean) {
			ControllerAdviceBean adviceBean = (ControllerAdviceBean) advice;
			if (!adviceBean.isApplicableToBeanType(parameter.getContainingClass())) {
				continue;
			}
			advice = adviceBean.resolveBean();
		}
		if (adviceType.isAssignableFrom(advice.getClass())) {
			result.add((A) advice);
		}
	}
	return result;
}
 
Example 5
Source File: ControllerMethodResolver.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Find an {@code @ExceptionHandler} method in {@code @ControllerAdvice}
 * components or in the controller of the given {@code @RequestMapping} method.
 */
@Nullable
public InvocableHandlerMethod getExceptionHandlerMethod(Throwable ex, HandlerMethod handlerMethod) {
	Class<?> handlerType = handlerMethod.getBeanType();

	// Controller-local first...
	Object targetBean = handlerMethod.getBean();
	Method targetMethod = this.exceptionHandlerCache
			.computeIfAbsent(handlerType, ExceptionHandlerMethodResolver::new)
			.resolveMethodByThrowable(ex);

	if (targetMethod == null) {
		// Global exception handlers...
		for (ControllerAdviceBean advice : this.exceptionHandlerAdviceCache.keySet()) {
			if (advice.isApplicableToBeanType(handlerType)) {
				targetBean = advice.resolveBean();
				targetMethod = this.exceptionHandlerAdviceCache.get(advice).resolveMethodByThrowable(ex);
				if (targetMethod != null) {
					break;
				}
			}
		}
	}

	if (targetMethod == null) {
		return null;
	}

	InvocableHandlerMethod invocable = new InvocableHandlerMethod(targetBean, targetMethod);
	invocable.setArgumentResolvers(this.exceptionHandlerResolvers);
	return invocable;
}
 
Example 6
Source File: ControllerMethodResolver.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Find an {@code @ExceptionHandler} method in {@code @ControllerAdvice}
 * components or in the controller of the given {@code @RequestMapping} method.
 */
@Nullable
public InvocableHandlerMethod getExceptionHandlerMethod(Throwable ex, HandlerMethod handlerMethod) {

	Class<?> handlerType = handlerMethod.getBeanType();

	// Controller-local first...
	Object targetBean = handlerMethod.getBean();
	Method targetMethod = this.exceptionHandlerCache
			.computeIfAbsent(handlerType, ExceptionHandlerMethodResolver::new)
			.resolveMethodByThrowable(ex);

	if (targetMethod == null) {
		// Global exception handlers...
		for (ControllerAdviceBean advice : this.exceptionHandlerAdviceCache.keySet()) {
			if (advice.isApplicableToBeanType(handlerType)) {
				targetBean = advice.resolveBean();
				targetMethod = this.exceptionHandlerAdviceCache.get(advice).resolveMethodByThrowable(ex);
				if (targetMethod != null) {
					break;
				}
			}
		}
	}

	if (targetMethod == null) {
		return null;
	}

	InvocableHandlerMethod invocable = new InvocableHandlerMethod(targetBean, targetMethod);
	invocable.setArgumentResolvers(this.exceptionHandlerResolvers);
	return invocable;
}