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

The following examples show how to use org.springframework.core.MethodParameter#getGenericParameterType() . 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: HandlerMethodInvoker.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private Class<?> getHttpEntityType(MethodParameter methodParam) {
	Assert.isAssignable(HttpEntity.class, methodParam.getParameterType());
	ParameterizedType type = (ParameterizedType) methodParam.getGenericParameterType();
	if (type.getActualTypeArguments().length == 1) {
		Type typeArgument = type.getActualTypeArguments()[0];
		if (typeArgument instanceof Class) {
			return (Class<?>) typeArgument;
		}
		else if (typeArgument instanceof GenericArrayType) {
			Type componentType = ((GenericArrayType) typeArgument).getGenericComponentType();
			if (componentType instanceof Class) {
				// Surely, there should be a nicer way to do this
				Object array = Array.newInstance((Class<?>) componentType, 0);
				return array.getClass();
			}
		}
	}
	throw new IllegalArgumentException(
			"HttpEntity parameter (" + methodParam.getParameterName() + ") is not parameterized");

}
 
Example 2
Source File: HttpEntityMethodProcessor.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private Type getHttpEntityType(MethodParameter parameter) {
	Assert.isAssignable(HttpEntity.class, parameter.getParameterType());
	Type parameterType = parameter.getGenericParameterType();
	if (parameterType instanceof ParameterizedType) {
		ParameterizedType type = (ParameterizedType) parameterType;
		if (type.getActualTypeArguments().length != 1) {
			throw new IllegalArgumentException("Expected single generic parameter on '" +
					parameter.getParameterName() + "' in method " + parameter.getMethod());
		}
		return type.getActualTypeArguments()[0];
	}
	else if (parameterType instanceof Class) {
		return Object.class;
	}
	else {
		return null;
	}
}
 
Example 3
Source File: HttpEntityMethodProcessor.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
private Type getHttpEntityType(MethodParameter parameter) {
	Assert.isAssignable(HttpEntity.class, parameter.getParameterType());
	Type parameterType = parameter.getGenericParameterType();
	if (parameterType instanceof ParameterizedType) {
		ParameterizedType type = (ParameterizedType) parameterType;
		if (type.getActualTypeArguments().length != 1) {
			throw new IllegalArgumentException("Expected single generic parameter on '" +
					parameter.getParameterName() + "' in method " + parameter.getMethod());
		}
		return type.getActualTypeArguments()[0];
	}
	else if (parameterType instanceof Class) {
		return Object.class;
	}
	throw new IllegalArgumentException("HttpEntity parameter '" + parameter.getParameterName() +
			"' in method " + parameter.getMethod() + " is not parameterized");
}
 
Example 4
Source File: HttpEntityMethodProcessor.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Nullable
private Type getHttpEntityType(MethodParameter parameter) {
	Assert.isAssignable(HttpEntity.class, parameter.getParameterType());
	Type parameterType = parameter.getGenericParameterType();
	if (parameterType instanceof ParameterizedType) {
		ParameterizedType type = (ParameterizedType) parameterType;
		if (type.getActualTypeArguments().length != 1) {
			throw new IllegalArgumentException("Expected single generic parameter on '" +
					parameter.getParameterName() + "' in method " + parameter.getMethod());
		}
		return type.getActualTypeArguments()[0];
	}
	else if (parameterType instanceof Class) {
		return Object.class;
	}
	else {
		return null;
	}
}
 
Example 5
Source File: HandlerMethodInvoker.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
private Class<?> getHttpEntityType(MethodParameter methodParam) {
	Assert.isAssignable(HttpEntity.class, methodParam.getParameterType());
	ParameterizedType type = (ParameterizedType) methodParam.getGenericParameterType();
	if (type.getActualTypeArguments().length == 1) {
		Type typeArgument = type.getActualTypeArguments()[0];
		if (typeArgument instanceof Class) {
			return (Class<?>) typeArgument;
		}
		else if (typeArgument instanceof GenericArrayType) {
			Type componentType = ((GenericArrayType) typeArgument).getGenericComponentType();
			if (componentType instanceof Class) {
				// Surely, there should be a nicer way to do this
				Object array = Array.newInstance((Class<?>) componentType, 0);
				return array.getClass();
			}
		}
	}
	throw new IllegalArgumentException(
			"HttpEntity parameter (" + methodParam.getParameterName() + ") is not parameterized");

}
 
Example 6
Source File: InvocableHandlerMethod.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private boolean isAsyncVoidReturnType(MethodParameter returnType, @Nullable ReactiveAdapter reactiveAdapter) {
	if (reactiveAdapter != null && reactiveAdapter.supportsEmpty()) {
		if (reactiveAdapter.isNoValue()) {
			return true;
		}
		Type parameterType = returnType.getGenericParameterType();
		if (parameterType instanceof ParameterizedType) {
			ParameterizedType type = (ParameterizedType) parameterType;
			if (type.getActualTypeArguments().length == 1) {
				return Void.class.equals(type.getActualTypeArguments()[0]);
			}
		}
	}
	return false;
}
 
Example 7
Source File: RestHandlerExceptionResolver.java    From spring-rest-exception-handler with Apache License 2.0 5 votes vote down vote up
public RestHandlerExceptionResolver() {

        Method method = ClassUtils.getMethod(
            RestExceptionHandler.class, "handleException", Exception.class, HttpServletRequest.class);

        returnTypeMethodParam = new MethodParameter(method, -1);
        // This method caches the resolved value, so it's convenient to initialize it
        // only once here.
        returnTypeMethodParam.getGenericParameterType();
    }
 
Example 8
Source File: AbstractMessageConverterMethodProcessor.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Return the generic type of the {@code returnType} (or of the nested type
 * if it is an {@link HttpEntity}).
 */
private Type getGenericType(MethodParameter returnType) {
	if (HttpEntity.class.isAssignableFrom(returnType.getParameterType())) {
		return ResolvableType.forType(returnType.getGenericParameterType()).getGeneric(0).getType();
	}
	else {
		return returnType.getGenericParameterType();
	}
}
 
Example 9
Source File: AbstractMessageConverterMethodProcessor.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Return the generic type of the {@code returnType} (or of the nested type
 * if it is an {@link HttpEntity}).
 */
private Type getGenericType(MethodParameter returnType) {
	if (HttpEntity.class.isAssignableFrom(returnType.getParameterType())) {
		return ResolvableType.forType(returnType.getGenericParameterType()).getGeneric().getType();
	}
	else {
		return returnType.getGenericParameterType();
	}
}
 
Example 10
Source File: GenericParameterBuilder.java    From springdoc-openapi with Apache License 2.0 5 votes vote down vote up
/**
 * Is file boolean.
 *
 * @param methodParameter the method parameter
 * @return the boolean
 */
public boolean isFile(MethodParameter methodParameter) {
	if (methodParameter.getGenericParameterType() instanceof ParameterizedType) {
		ParameterizedType parameterizedType = (ParameterizedType) methodParameter.getGenericParameterType();
		return isFile(parameterizedType);
	}
	else {
		Class type = methodParameter.getParameterType();
		return isFile(type);
	}
}
 
Example 11
Source File: AbstractMessageConverterMethodProcessor.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Return the generic type of the {@code returnType} (or of the nested type
 * if it is an {@link HttpEntity}).
 */
private Type getGenericType(MethodParameter returnType) {
	if (HttpEntity.class.isAssignableFrom(returnType.getParameterType())) {
		return ResolvableType.forType(returnType.getGenericParameterType()).getGeneric().getType();
	}
	else {
		return returnType.getGenericParameterType();
	}
}
 
Example 12
Source File: AbstractMessageConverterMethodProcessor.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Return the generic type of the {@code returnType} (or of the nested type if it is
 * a {@link HttpEntity}).
 */
private Type getGenericType(MethodParameter returnType) {
	Type type;
	if (HttpEntity.class.isAssignableFrom(returnType.getParameterType())) {
		returnType.increaseNestingLevel();
		type = returnType.getNestedGenericParameterType();
	}
	else {
		type = returnType.getGenericParameterType();
	}
	return type;
}
 
Example 13
Source File: MessageMethodArgumentResolver.java    From java-technology-stack with MIT License 4 votes vote down vote up
private Class<?> getPayloadType(MethodParameter parameter) {
	Type genericParamType = parameter.getGenericParameterType();
	ResolvableType resolvableType = ResolvableType.forType(genericParamType).as(Message.class);
	return resolvableType.getGeneric().toClass();
}
 
Example 14
Source File: HandlerMethodArgumentResolverSupport.java    From java-technology-stack with MIT License 4 votes vote down vote up
private void assertHasValues(ReactiveAdapter adapter, MethodParameter param) {
	if (adapter.isNoValue()) {
		throw new IllegalArgumentException(
				"No value reactive types not supported: " + param.getGenericParameterType());
	}
}
 
Example 15
Source File: HandlerMethodArgumentResolverSupport.java    From java-technology-stack with MIT License 4 votes vote down vote up
private IllegalStateException buildReactiveWrapperException(MethodParameter parameter) {
	return new IllegalStateException(getClass().getSimpleName() +
			" doesn't support reactive type wrapper: " + parameter.getGenericParameterType());
}
 
Example 16
Source File: MessageMethodArgumentResolver.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
private Class<?> getPayloadType(MethodParameter parameter) {
	Type genericParamType = parameter.getGenericParameterType();
	ResolvableType resolvableType = ResolvableType.forType(genericParamType).as(Message.class);
	return resolvableType.getGeneric(0).resolve(Object.class);
}
 
Example 17
Source File: HandlerMethodArgumentResolverSupport.java    From spring-analysis-note with MIT License 4 votes vote down vote up
private IllegalStateException buildReactiveWrapperException(MethodParameter parameter) {
	return new IllegalStateException(getClass().getSimpleName() +
			" does not support reactive type wrapper: " + parameter.getGenericParameterType());
}
 
Example 18
Source File: HandlerMethodArgumentResolverSupport.java    From spring-analysis-note with MIT License 4 votes vote down vote up
private void assertHasValues(ReactiveAdapter adapter, MethodParameter param) {
	if (adapter.isNoValue()) {
		throw new IllegalArgumentException(
				"No value reactive types not supported: " + param.getGenericParameterType());
	}
}
 
Example 19
Source File: ReturnTypeParser.java    From springdoc-openapi with Apache License 2.0 2 votes vote down vote up
/**
 * Gets type.
 *
 * @param methodParameter the method parameter
 * @return the type
 */
static Type getType(MethodParameter methodParameter) {
	if (methodParameter.getGenericParameterType() instanceof ParameterizedType)
		return ReturnTypeParser.resolveType(methodParameter.getGenericParameterType(), methodParameter.getContainingClass());
	return methodParameter.getParameterType();
}
 
Example 20
Source File: MessageMethodArgumentResolver.java    From spring-analysis-note with MIT License 2 votes vote down vote up
/**
 * Resolve the target class to convert the payload to.
 * <p>By default this is the generic type declared in the {@code Message}
 * method parameter but that can be overridden to select a more specific
 * target type after also taking into account the "Content-Type", e.g.
 * return {@code String} if target type is {@code Object} and
 * {@code "Content-Type:text/**"}.
 * @param parameter the target method parameter
 * @param message the message bring processed
 * @return the target type to use
 * @since 5.2
 */
protected Class<?> getPayloadType(MethodParameter parameter, Message<?> message) {
	Type genericParamType = parameter.getGenericParameterType();
	ResolvableType resolvableType = ResolvableType.forType(genericParamType).as(Message.class);
	return resolvableType.getGeneric().toClass();
}