public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) { return FastJsonHttpMessageConverter.class.isAssignableFrom(converterType) && (returnType.getContainingClass().isAnnotationPresent(ResponseJSONP.class) || returnType.hasMethodAnnotation(ResponseJSONP.class)); }
@SuppressWarnings("ConstantConditions") private void logExecutorWarning(MethodParameter returnType) { if (this.taskExecutorWarning && logger.isWarnEnabled()) { synchronized (this) { if (this.taskExecutorWarning) { String executorTypeName = this.taskExecutor.getClass().getSimpleName(); logger.warn("\n!!!\n" + "Streaming through a reactive type requires an Executor to write to the response.\n" + "Please, configure a TaskExecutor in the MVC config under \"async support\".\n" + "The " + executorTypeName + " currently in use is not suitable under load.\n" + "-------------------------------\n" + "Controller:\t" + returnType.getContainingClass().getName() + "\n" + "Method:\t\t" + returnType.getMethod().getName() + "\n" + "Returning:\t" + ResolvableType.forMethodParameter(returnType).toString() + "\n" + "!!!"); this.taskExecutorWarning = false; } } } }
@Override public boolean supportsReturnType(MethodParameter returnType) { return (AnnotatedElementUtils.hasAnnotation(returnType.getContainingClass(), ResponseBody.class) || returnType.hasMethodAnnotation(ResponseBody.class)); }
@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; }
private JavaType getJavaType(Class<?> targetClass, @Nullable Object conversionHint) { if (conversionHint instanceof MethodParameter) { MethodParameter param = (MethodParameter) conversionHint; param = param.nestedIfOptional(); if (Message.class.isAssignableFrom(param.getParameterType())) { param = param.nested(); } Type genericParameterType = param.getNestedGenericParameterType(); Class<?> contextClass = param.getContainingClass(); Type type = GenericTypeResolver.resolveType(genericParameterType, contextClass); return this.objectMapper.getTypeFactory().constructType(type); } return this.objectMapper.constructType(targetClass); }
@Override public boolean supports(HandlerResult result) { MethodParameter returnType = result.getReturnTypeSource(); Class<?> containingClass = returnType.getContainingClass(); return (AnnotatedElementUtils.hasAnnotation(containingClass, ResponseBody.class) || returnType.hasMethodAnnotation(ResponseBody.class)); }
/** * Return whether this method indicates a parameter which is not required: * either in the form of Java 8's {@link java.util.Optional}, any variant * of a parameter-level {@code Nullable} annotation (such as from JSR-305 * or the FindBugs set of annotations), or a language-level nullable type * declaration in Kotlin. * @since 4.3 */ public boolean isOptional() { return (getParameterType() == Optional.class || hasNullableAnnotation() || (KotlinDetector.isKotlinReflectPresent() && KotlinDetector.isKotlinType(getContainingClass()) && KotlinDelegate.isOptional(this))); }
@Override public boolean supportsReturnType(MethodParameter returnType) { return (AnnotatedElementUtils.hasAnnotation(returnType.getContainingClass(), ResponseBody.class) || returnType.hasMethodAnnotation(ResponseBody.class)); }
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) { ResponseJSONP responseJsonp = returnType.getMethodAnnotation(ResponseJSONP.class); if(responseJsonp == null){ responseJsonp = returnType.getContainingClass().getAnnotation(ResponseJSONP.class); } HttpServletRequest servletRequest = ((ServletServerHttpRequest) request).getServletRequest(); String callbackMethodName = servletRequest.getParameter(responseJsonp.callback()); if (!IOUtils.isValidJsonpQueryParam(callbackMethodName)) { if (logger.isDebugEnabled()) { logger.debug("Invalid jsonp parameter value:" + callbackMethodName); } callbackMethodName = null; } JSONPObject jsonpObject = new JSONPObject(callbackMethodName); jsonpObject.addParameter(body); beforeBodyWriteInternal(jsonpObject, selectedContentType, returnType, request, response); return jsonpObject; }
/** * Derive the model attribute name for the given return value. Results will be * based on: * <ol> * <li>the method {@code ModelAttribute} annotation value * <li>the declared return type if it is more specific than {@code Object} * <li>the actual return value type * </ol> * @param returnValue the value returned from a method invocation * @param returnType a descriptor for the return type of the method * @return the derived name (never {@code null} or empty String) */ public static String getNameForReturnValue(@Nullable Object returnValue, MethodParameter returnType) { ModelAttribute ann = returnType.getMethodAnnotation(ModelAttribute.class); if (ann != null && StringUtils.hasText(ann.value())) { return ann.value(); } else { Method method = returnType.getMethod(); Assert.state(method != null, "No handler method"); Class<?> containingClass = returnType.getContainingClass(); Class<?> resolvedType = GenericTypeResolver.resolveReturnType(method, containingClass); return Conventions.getVariableNameForReturnType(method, resolvedType, returnValue); } }
/** * Return a {@link ResolvableType} for the specified {@link MethodParameter}, * overriding the target type to resolve with a specific given type. * @param methodParameter the source method parameter (must not be {@code null}) * @param targetType the type to resolve (a part of the method parameter's type) * @return a {@link ResolvableType} for the specified method parameter * @see #forMethodParameter(Method, int) */ public static ResolvableType forMethodParameter(MethodParameter methodParameter, @Nullable Type targetType) { Assert.notNull(methodParameter, "MethodParameter must not be null"); ResolvableType owner = forType(methodParameter.getContainingClass()).as(methodParameter.getDeclaringClass()); return forType(targetType, new MethodParameterTypeProvider(methodParameter), owner.asVariableResolver()). getNested(methodParameter.getNestingLevel(), methodParameter.typeIndexesPerLevel); }
/** * Return whether this method indicates a parameter which is not required: * either in the form of Java 8's {@link java.util.Optional}, any variant * of a parameter-level {@code Nullable} annotation (such as from JSR-305 * or the FindBugs set of annotations), or a language-level nullable type * declaration in Kotlin. * @since 4.3 */ public boolean isOptional() { return (getParameterType() == Optional.class || hasNullableAnnotation() || (KotlinDetector.isKotlinReflectPresent() && KotlinDetector.isKotlinType(getContainingClass()) && KotlinDelegate.isOptional(this))); }
/** * Resolve the top-level parameter type of the given {@code MethodParameter}. * @param methodParameter the method parameter to resolve * @since 4.1.9 * @see MethodParameter#setParameterType */ static void resolveMethodParameter(MethodParameter methodParameter) { Assert.notNull(methodParameter, "MethodParameter must not be null"); ResolvableType owner = forType(methodParameter.getContainingClass()).as(methodParameter.getDeclaringClass()); methodParameter.setParameterType( forType(null, new MethodParameterTypeProvider(methodParameter), owner.asVariableResolver()).resolve()); }
/** * Create a new descriptor for a method or constructor parameter. * @param methodParameter the MethodParameter to wrap * @param required whether the dependency is required * @param eager whether this dependency is 'eager' in the sense of * eagerly resolving potential target beans for type matching */ public DependencyDescriptor(MethodParameter methodParameter, boolean required, boolean eager) { super(methodParameter); this.declaringClass = methodParameter.getDeclaringClass(); if (methodParameter.getMethod() != null) { this.methodName = methodParameter.getMethod().getName(); } this.parameterTypes = methodParameter.getExecutable().getParameterTypes(); this.parameterIndex = methodParameter.getParameterIndex(); this.containingClass = methodParameter.getContainingClass(); this.required = required; this.eager = eager; }
/** * Return a {@link ResolvableType} for the specified {@link MethodParameter} with a * given implementation type. Use this variant when the class that declares the method * includes generic parameter variables that are satisfied by the implementation type. * @param methodParameter the source method parameter (must not be {@code null}) * @param implementationType the implementation type * @return a {@link ResolvableType} for the specified method parameter * @see #forMethodParameter(MethodParameter) */ public static ResolvableType forMethodParameter(MethodParameter methodParameter, @Nullable ResolvableType implementationType) { Assert.notNull(methodParameter, "MethodParameter must not be null"); implementationType = (implementationType != null ? implementationType : forType(methodParameter.getContainingClass())); ResolvableType owner = implementationType.as(methodParameter.getDeclaringClass()); return forType(null, new MethodParameterTypeProvider(methodParameter), owner.asVariableResolver()). getNested(methodParameter.getNestingLevel(), methodParameter.typeIndexesPerLevel); }
@Override public void handleReturnValue(@Nullable Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception { if (this.mavResolvers != null) { for (ModelAndViewResolver mavResolver : this.mavResolvers) { Class<?> handlerType = returnType.getContainingClass(); Method method = returnType.getMethod(); Assert.state(method != null, "No handler method"); ExtendedModelMap model = (ExtendedModelMap) mavContainer.getModel(); ModelAndView mav = mavResolver.resolveModelAndView(method, handlerType, returnValue, model, webRequest); if (mav != ModelAndViewResolver.UNRESOLVED) { mavContainer.addAllAttributes(mav.getModel()); mavContainer.setViewName(mav.getViewName()); if (!mav.isReference()) { mavContainer.setView(mav.getView()); } return; } } } // No suitable ModelAndViewResolver... if (this.modelAttributeProcessor.supportsReturnType(returnType)) { this.modelAttributeProcessor.handleReturnValue(returnValue, returnType, mavContainer, webRequest); } else { throw new UnsupportedOperationException("Unexpected return type: " + returnType.getParameterType().getName() + " in method: " + returnType.getMethod()); } }
/** * Return a {@link ResolvableType} for the specified {@link MethodParameter}, * overriding the target type to resolve with a specific given type. * @param methodParameter the source method parameter (must not be {@code null}) * @param targetType the type to resolve (a part of the method parameter's type) * @return a {@link ResolvableType} for the specified method parameter * @see #forMethodParameter(Method, int) */ public static ResolvableType forMethodParameter(MethodParameter methodParameter, @Nullable Type targetType) { Assert.notNull(methodParameter, "MethodParameter must not be null"); ResolvableType owner = forType(methodParameter.getContainingClass()).as(methodParameter.getDeclaringClass()); return forType(targetType, new MethodParameterTypeProvider(methodParameter), owner.asVariableResolver()). getNested(methodParameter.getNestingLevel(), methodParameter.typeIndexesPerLevel); }
/** * Return a {@link ResolvableType} for the specified {@link MethodParameter} with a * given implementation type. Use this variant when the class that declares the method * includes generic parameter variables that are satisfied by the implementation type. * @param methodParameter the source method parameter (must not be {@code null}) * @param implementationType the implementation type * @return a {@link ResolvableType} for the specified method parameter * @see #forMethodParameter(MethodParameter) */ public static ResolvableType forMethodParameter(MethodParameter methodParameter, @Nullable ResolvableType implementationType) { Assert.notNull(methodParameter, "MethodParameter must not be null"); implementationType = (implementationType != null ? implementationType : forType(methodParameter.getContainingClass())); ResolvableType owner = implementationType.as(methodParameter.getDeclaringClass()); return forType(null, new MethodParameterTypeProvider(methodParameter), owner.asVariableResolver()). getNested(methodParameter.getNestingLevel(), methodParameter.typeIndexesPerLevel); }
/** * Resolve the top-level parameter type of the given {@code MethodParameter}. * @param methodParameter the method parameter to resolve * @since 4.1.9 * @see MethodParameter#setParameterType */ static void resolveMethodParameter(MethodParameter methodParameter) { Assert.notNull(methodParameter, "MethodParameter must not be null"); ResolvableType owner = forType(methodParameter.getContainingClass()).as(methodParameter.getDeclaringClass()); methodParameter.setParameterType( forType(null, new MethodParameterTypeProvider(methodParameter), owner.asVariableResolver()).resolve()); }
Class<?> contextClass = (param != null ? param.getContainingClass() : null); JavaType javaType = getJavaType(elementType.getType(), contextClass); Class<?> jsonView = (hints != null ? (Class<?>) hints.get(Jackson2CodecSupport.JSON_VIEW_HINT) : null);