@Override public Type getType() { return this.methodParameter.getGenericParameterType(); }
@Override public Type getType() { return this.methodParameter.getGenericParameterType(); }
private IllegalStateException buildReactiveWrapperException(MethodParameter parameter) { return new IllegalStateException(getClass().getSimpleName() + " doesn't support reactive type wrapper: " + parameter.getGenericParameterType()); }
private void assertHasValues(ReactiveAdapter adapter, MethodParameter param) { if (adapter.isNoValue()) { throw new IllegalArgumentException( "No value reactive types not supported: " + param.getGenericParameterType()); } }
/** * Return the nested generic type of the method/constructor parameter. * @return the parameter type (never {@code null}) * @since 4.2 * @see #getNestingLevel() */ public Type getNestedGenericParameterType() { if (this.nestingLevel > 1) { Type type = getGenericParameterType(); for (int i = 2; i <= this.nestingLevel; i++) { if (type instanceof ParameterizedType) { Type[] args = ((ParameterizedType) type).getActualTypeArguments(); Integer index = getTypeIndexForLevel(i); type = args[index != null ? index : args.length - 1]; } } return type; } else { return getGenericParameterType(); } }
private Type type(MethodParameter parameter) { Type type = parameter.getGenericParameterType(); if (type instanceof ParameterizedType) { ParameterizedType param = (ParameterizedType) type; type = param.getActualTypeArguments()[0]; } return type; }
private Type type(MethodParameter parameter) { Type type = parameter.getGenericParameterType(); if (type instanceof ParameterizedType) { ParameterizedType param = (ParameterizedType) type; type = param.getActualTypeArguments()[0]; } if (type instanceof TypeVariable || type instanceof WildcardType) { type = Object.class; } 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(); } }
private static boolean isAsyncVoidReturnType(MethodParameter returnType, @Nullable ReactiveAdapter adapter) { if (adapter != null && adapter.supportsEmpty()) { if (adapter.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; }
Type type = getGenericParameterType(); for (int i = 2; i <= this.nestingLevel; i++) { if (type instanceof ParameterizedType) {
/** * Return the nested generic type of the method/constructor parameter. * @return the parameter type (never {@code null}) * @since 4.2 * @see #getNestingLevel() */ public Type getNestedGenericParameterType() { if (this.nestingLevel > 1) { Type type = getGenericParameterType(); for (int i = 2; i <= this.nestingLevel; i++) { if (type instanceof ParameterizedType) { Type[] args = ((ParameterizedType) type).getActualTypeArguments(); Integer index = getTypeIndexForLevel(i); type = args[index != null ? index : args.length - 1]; } } return type; } else { return getGenericParameterType(); } }
@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; } }
/** * 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(); } }
@Override protected Object resolveNamedValue(String name, MethodParameter parameter, ServerWebExchange exchange) { Object value = exchange.getAttribute(name); ReactiveAdapter toAdapter = getAdapterRegistry().getAdapter(parameter.getParameterType()); if (toAdapter != null) { if (value == null) { Assert.isTrue(toAdapter.supportsEmpty(), () -> "No request attribute '" + name + "' and target type " + parameter.getGenericParameterType() + " doesn't support empty values."); return toAdapter.fromPublisher(Mono.empty()); } if (parameter.getParameterType().isAssignableFrom(value.getClass())) { return value; } ReactiveAdapter fromAdapter = getAdapterRegistry().getAdapter(value.getClass()); Assert.isTrue(fromAdapter != null, () -> getClass().getSimpleName() + " doesn't support " + "reactive type wrapper: " + parameter.getGenericParameterType()); return toAdapter.fromPublisher(fromAdapter.toPublisher(value)); } return value; }
@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<?> getPayloadType(MethodParameter parameter) { Type genericParamType = parameter.getGenericParameterType(); ResolvableType resolvableType = ResolvableType.forType(genericParamType).as(Message.class); return resolvableType.getGeneric().toClass(); }
@Test // SPR-16734 public void genericConstructorParameterInInnerClass() throws Exception { Constructor<?> constructor = InnerClass.class.getConstructor(getClass(), String.class, Callable.class); MethodParameter methodParameter = MethodParameter.forExecutable(constructor, 0); assertEquals(getClass(), methodParameter.getParameterType()); assertEquals(getClass(), methodParameter.getGenericParameterType()); methodParameter = MethodParameter.forExecutable(constructor, 1); assertEquals(String.class, methodParameter.getParameterType()); assertEquals(String.class, methodParameter.getGenericParameterType()); methodParameter = MethodParameter.forExecutable(constructor, 2); assertEquals(Callable.class, methodParameter.getParameterType()); assertEquals(ResolvableType.forClassWithGenerics(Callable.class, Integer.class).getType(), methodParameter.getGenericParameterType()); }
@Test public void testResolveType() { Method intMessageMethod = findMethod(MyTypeWithMethods.class, "readIntegerInputMessage", MyInterfaceType.class); MethodParameter intMessageMethodParam = new MethodParameter(intMessageMethod, 0); assertEquals(MyInterfaceType.class, resolveType(intMessageMethodParam.getGenericParameterType(), new HashMap<>())); Method intArrMessageMethod = findMethod(MyTypeWithMethods.class, "readIntegerArrayInputMessage", MyInterfaceType[].class); MethodParameter intArrMessageMethodParam = new MethodParameter(intArrMessageMethod, 0); assertEquals(MyInterfaceType[].class, resolveType(intArrMessageMethodParam.getGenericParameterType(), new HashMap<>())); Method genericArrMessageMethod = findMethod(MySimpleTypeWithMethods.class, "readGenericArrayInputMessage", Object[].class); MethodParameter genericArrMessageMethodParam = new MethodParameter(genericArrMessageMethod, 0); Map<TypeVariable, Type> varMap = getTypeVariableMap(MySimpleTypeWithMethods.class); assertEquals(Integer[].class, resolveType(genericArrMessageMethodParam.getGenericParameterType(), varMap)); }
@Test // SPR-14520 public void resolveArgumentTypeVariableWithGenericInterface() throws Exception { this.servletRequest.setContent("\"foo\"".getBytes("UTF-8")); this.servletRequest.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE); Method method = MyControllerImplementingInterface.class.getMethod("handle", Object.class); HandlerMethod handlerMethod = new HandlerMethod(new MyControllerImplementingInterface(), method); MethodParameter methodParameter = handlerMethod.getMethodParameters()[0]; List<HttpMessageConverter<?>> converters = new ArrayList<>(); converters.add(new MappingJackson2HttpMessageConverter()); RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters); String value = (String) processor.readWithMessageConverters( this.request, methodParameter, methodParameter.getGenericParameterType()); assertEquals("foo", value); }