下面列出了org.springframework.core.MethodParameter#getGenericParameterType ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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;
}
}
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");
}
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");
}
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;
}
}
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");
}
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;
}
/**
* 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;
}
/**
* 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();
}
}
/**
* 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);
}
}
/**
* 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();
}
}
/**
* 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();
}
}
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();
}
private void assertHasValues(ReactiveAdapter adapter, MethodParameter param) {
if (adapter.isNoValue()) {
throw new IllegalArgumentException(
"No value reactive types not supported: " + param.getGenericParameterType());
}
}
private IllegalStateException buildReactiveWrapperException(MethodParameter parameter) {
return new IllegalStateException(getClass().getSimpleName() +
" does not support reactive type wrapper: " + parameter.getGenericParameterType());
}
private Class<?> getPayloadType(MethodParameter parameter) {
Type genericParamType = parameter.getGenericParameterType();
ResolvableType resolvableType = ResolvableType.forType(genericParamType).as(Message.class);
return resolvableType.getGeneric(0).resolve(Object.class);
}
private Class<?> getPayloadType(MethodParameter parameter) {
Type genericParamType = parameter.getGenericParameterType();
ResolvableType resolvableType = ResolvableType.forType(genericParamType).as(Message.class);
return resolvableType.getGeneric().toClass();
}
private void assertHasValues(ReactiveAdapter adapter, MethodParameter param) {
if (adapter.isNoValue()) {
throw new IllegalArgumentException(
"No value reactive types not supported: " + param.getGenericParameterType());
}
}
private IllegalStateException buildReactiveWrapperException(MethodParameter parameter) {
return new IllegalStateException(getClass().getSimpleName() +
" doesn't support reactive type wrapper: " + parameter.getGenericParameterType());
}
/**
* 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();
}
/**
* 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();
}