Java Code Examples for org.springframework.core.MethodParameter#getParameterType()

The following examples show how to use org.springframework.core.MethodParameter#getParameterType() . 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: AbstractWebArgumentResolverAdapter.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Delegate to the {@link WebArgumentResolver} instance.
 * @throws IllegalStateException if the resolved value is not assignable
 * to the method parameter.
 */
@Override
@Nullable
public Object resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer,
		NativeWebRequest webRequest, @Nullable WebDataBinderFactory binderFactory) throws Exception {

	Class<?> paramType = parameter.getParameterType();
	Object result = this.adaptee.resolveArgument(parameter, webRequest);
	if (result == WebArgumentResolver.UNRESOLVED || !ClassUtils.isAssignableValue(paramType, result)) {
		throw new IllegalStateException(
				"Standard argument type [" + paramType.getName() + "] in method " + parameter.getMethod() +
				"resolved to incompatible value of type [" + (result != null ? result.getClass() : null) +
				"]. Consider declaring the argument type in a less specific fashion.");
	}
	return result;
}
 
Example 2
Source File: AnnotationMethodHandlerExceptionResolver.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Resolves the arguments for the given method. Delegates to {@link #resolveCommonArgument}.
 */
private Object[] resolveHandlerArguments(Method handlerMethod, Object handler,
		NativeWebRequest webRequest, Exception thrownException) throws Exception {

	Class<?>[] paramTypes = handlerMethod.getParameterTypes();
	Object[] args = new Object[paramTypes.length];
	Class<?> handlerType = handler.getClass();
	for (int i = 0; i < args.length; i++) {
		MethodParameter methodParam = new SynthesizingMethodParameter(handlerMethod, i);
		GenericTypeResolver.resolveParameterType(methodParam, handlerType);
		Class<?> paramType = methodParam.getParameterType();
		Object argValue = resolveCommonArgument(methodParam, webRequest, thrownException);
		if (argValue != WebArgumentResolver.UNRESOLVED) {
			args[i] = argValue;
		}
		else {
			throw new IllegalStateException("Unsupported argument [" + paramType.getName() +
					"] for @ExceptionHandler method: " + handlerMethod);
		}
	}
	return args;
}
 
Example 3
Source File: HttpEntityMethodArgumentResolver.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public Mono<Object> resolveArgument(
		MethodParameter parameter, BindingContext bindingContext, ServerWebExchange exchange) {

	Class<?> entityType = parameter.getParameterType();
	return readBody(parameter.nested(), parameter, false, bindingContext, exchange)
			.map(body -> createEntity(body, entityType, exchange.getRequest()))
			.defaultIfEmpty(createEntity(null, entityType, exchange.getRequest()));
}
 
Example 4
Source File: HandlerMethodArgumentResolverSupport.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Evaluate the {@code Predicate} on the method parameter type or on
 * the generic type within a reactive type wrapper.
 */
protected boolean checkParameterType(MethodParameter parameter, Predicate<Class<?>> predicate) {
	Class<?> type = parameter.getParameterType();
	ReactiveAdapter adapter = getAdapterRegistry().getAdapter(type);
	if (adapter != null) {
		assertHasValues(adapter, parameter);
		type = parameter.nested().getNestedParameterType();
	}
	return predicate.test(type);
}
 
Example 5
Source File: HandlerMethodArgumentResolverSupport.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Evaluate the {@code Predicate} on the method parameter type or on
 * the generic type within a reactive type wrapper.
 */
protected boolean checkParameterType(MethodParameter parameter, Predicate<Class<?>> predicate) {
	Class<?> type = parameter.getParameterType();
	ReactiveAdapter adapter = getAdapterRegistry().getAdapter(type);
	if (adapter != null) {
		assertHasValues(adapter, parameter);
		type = parameter.nested().getNestedParameterType();
	}
	return predicate.test(type);
}
 
Example 6
Source File: DeferredResultMethodReturnValueHandler.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public boolean supportsReturnType(MethodParameter returnType) {
	Class<?> type = returnType.getParameterType();
	return (DeferredResult.class.isAssignableFrom(type) ||
			ListenableFuture.class.isAssignableFrom(type) ||
			CompletionStage.class.isAssignableFrom(type));
}
 
Example 7
Source File: KafkaStreamsStreamListenerSetupMethodOrchestrator.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 5 votes vote down vote up
private boolean methodParameterSupports(Method method) {
	boolean supports = false;
	for (int i = 0; i < method.getParameterCount(); i++) {
		MethodParameter methodParameter = MethodParameter.forExecutable(method, i);
		Class<?> parameterType = methodParameter.getParameterType();
		if (parameterType.equals(KStream.class) || parameterType.equals(KTable.class)
				|| parameterType.equals(GlobalKTable.class)) {
			supports = true;
		}
	}
	return supports;
}
 
Example 8
Source File: AbstractNamedValueMethodArgumentResolver.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public Object resolveArgument(MethodParameter parameter, Message<?> message) throws Exception {
	Class<?> paramType = parameter.getParameterType();
	NamedValueInfo namedValueInfo = getNamedValueInfo(parameter);

	Object arg = resolveArgumentInternal(parameter, message, namedValueInfo.name);
	if (arg == null) {
		if (namedValueInfo.defaultValue != null) {
			arg = resolveDefaultValue(namedValueInfo.defaultValue);
		}
		else if (namedValueInfo.required && !parameter.getParameterType().getName().equals("java.util.Optional")) {
			handleMissingValue(namedValueInfo.name, parameter, message);
		}
		arg = handleNullValue(namedValueInfo.name, arg, paramType);
	}
	else if ("".equals(arg) && namedValueInfo.defaultValue != null) {
		arg = resolveDefaultValue(namedValueInfo.defaultValue);
	}

	if (!ClassUtils.isAssignableValue(paramType, arg)) {
		arg = this.conversionService.convert(
				arg, TypeDescriptor.valueOf(arg.getClass()), new TypeDescriptor(parameter));
	}

	handleResolvedValue(arg, namedValueInfo.name, parameter, message);

	return arg;
}
 
Example 9
Source File: ResponseBodyEmitterReturnValueHandler.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public boolean supportsReturnType(MethodParameter returnType) {
	Class<?> bodyType = ResponseEntity.class.isAssignableFrom(returnType.getParameterType()) ?
			ResolvableType.forMethodParameter(returnType).getGeneric().resolve() :
			returnType.getParameterType();

	return (bodyType != null && (ResponseBodyEmitter.class.isAssignableFrom(bodyType) ||
			this.reactiveHandler.isReactiveType(bodyType)));
}
 
Example 10
Source File: HandlerMethodReturnValueHandlerComposite.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public void handleReturnValue(@Nullable Object returnValue, MethodParameter returnType, Message<?> message)
		throws Exception {

	HandlerMethodReturnValueHandler handler = getReturnValueHandler(returnType);
	if (handler == null) {
		throw new IllegalStateException("No handler for return value type: " + returnType.getParameterType());
	}
	if (logger.isTraceEnabled()) {
		logger.trace("Processing return value with " + handler);
	}
	handler.handleReturnValue(returnValue, returnType, message);
}
 
Example 11
Source File: JsonParamResolver.java    From hsweb-framework with Apache License 2.0 5 votes vote down vote up
@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
    JsonParam jsonParam = parameter.getParameterAnnotation(JsonParam.class);
    String object = webRequest.getParameter(jsonParam.value());
    if (null != object) {
        Class type = jsonParam.type() != Void.class ? jsonParam.type() : parameter.getParameterType();
        return fastJsonHttpMessageConverter.readByString(type, object);
    }
    return null;
}
 
Example 12
Source File: ServletResponseMethodArgumentResolver.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Set {@link ModelAndViewContainer#setRequestHandled(boolean)} to
 * {@code false} to indicate that the method signature provides access
 * to the response. If subsequently the underlying method returns
 * {@code null}, the request is considered directly handled.
 */
@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
		NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {

	if (mavContainer != null) {
		mavContainer.setRequestHandled(true);
	}

	HttpServletResponse response = webRequest.getNativeResponse(HttpServletResponse.class);
	Class<?> paramType = parameter.getParameterType();

	if (ServletResponse.class.isAssignableFrom(paramType)) {
		Object nativeResponse = webRequest.getNativeResponse(paramType);
		if (nativeResponse == null) {
			throw new IllegalStateException(
					"Current response is not of type [" + paramType.getName() + "]: " + response);
		}
		return nativeResponse;
	}
	else if (OutputStream.class.isAssignableFrom(paramType)) {
		return response.getOutputStream();
	}
	else if (Writer.class.isAssignableFrom(paramType)) {
		return response.getWriter();
	}
	else {
		// should not happen
		Method method = parameter.getMethod();
		throw new UnsupportedOperationException("Unknown parameter type: " + paramType + " in method: " + method);
	}
}
 
Example 13
Source File: ChannelOnConnectResolver.java    From spring-boot-netty with MIT License 4 votes vote down vote up
@Override
public boolean canResolve(final MethodParameter methodParameter) {
    final Class<?> parameterType = methodParameter.getParameterType();
    return Channel.class.isAssignableFrom(parameterType);
}
 
Example 14
Source File: HttpEntityMethodProcessor.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public boolean supportsParameter(MethodParameter parameter) {
	return (HttpEntity.class == parameter.getParameterType() ||
			RequestEntity.class == parameter.getParameterType());
}
 
Example 15
Source File: UserArgumentResolver.java    From goods-seckill with Apache License 2.0 4 votes vote down vote up
@Override
public boolean supportsParameter(MethodParameter parameter) {
	Class<?> clazz = parameter.getParameterType();
	return clazz == MiaoshaUserEntity.class;
}
 
Example 16
Source File: HeaderMethodArgumentResolver.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
protected void handleMissingValue(String headerName, MethodParameter parameter, Message<?> message) {
	throw new MessageHandlingException(message, "Missing header '" + headerName +
			"' for method parameter type [" + parameter.getParameterType() + "]");
}
 
Example 17
Source File: RandomLongOnMessageResolver.java    From spring-boot-netty with MIT License 4 votes vote down vote up
@Override
public boolean canResolve(final MethodParameter methodParameter) {
    final Class<?> parameterType = methodParameter.getParameterType();
    return long.class.isAssignableFrom(parameterType) || Long.class.isAssignableFrom(parameterType);
}
 
Example 18
Source File: DestinationVariableMethodArgumentResolver.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
protected void handleMissingValue(String name, MethodParameter parameter, Message<?> message) {
	throw new MessageHandlingException(message, "Missing path template variable '" + name +
			"' for method parameter type [" + parameter.getParameterType() + "]");
}
 
Example 19
Source File: AbstractMessageConverterMethodProcessor.java    From java-technology-stack with MIT License 2 votes vote down vote up
/**
 * Return the type of the value to be written to the response. Typically this is
 * a simple check via getClass on the value but if the value is null, then the
 * return type needs to be examined possibly including generic type determination
 * (e.g. {@code ResponseEntity<T>}).
 */
protected Class<?> getReturnValueType(@Nullable Object value, MethodParameter returnType) {
	return (value != null ? value.getClass() : returnType.getParameterType());
}
 
Example 20
Source File: AbstractMessageConverterMethodProcessor.java    From spring-analysis-note with MIT License 2 votes vote down vote up
/**
 * Return the type of the value to be written to the response. Typically this is
 * a simple check via getClass on the value but if the value is null, then the
 * return type needs to be examined possibly including generic type determination
 * (e.g. {@code ResponseEntity<T>}).
 */
protected Class<?> getReturnValueType(@Nullable Object value, MethodParameter returnType) {
	return (value != null ? value.getClass() : returnType.getParameterType());
}