Java Code Examples for org.springframework.core.MethodParameter#getNestedParameterType()
The following examples show how to use
org.springframework.core.MethodParameter#getNestedParameterType() .
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: WxApiParamContributor.java From FastBootWeixin with Apache License 2.0 | 6 votes |
@Override public void contributeMethodArgument(MethodParameter parameter, Object value, UriComponentsBuilder builder, Map<String, Object> uriVariables, ConversionService conversionService) { Class<?> paramType = parameter.getNestedParameterType(); if (Map.class.isAssignableFrom(paramType)) { return; } WxApiParam wxApiParam = parameter.getParameterAnnotation(WxApiParam.class); String name = (wxApiParam == null || StringUtils.isEmpty(wxApiParam.name()) ? parameter.getParameterName() : wxApiParam.name()); WxAppAssert.notNull(name, "请添加编译器的-parameter或者为参数添加注解名称"); if (value == null) { if (wxApiParam != null) { if (!wxApiParam.required() || !wxApiParam.defaultValue().equals(ValueConstants.DEFAULT_NONE)) { return; } } builder.queryParam(name); } else if (value instanceof Collection) { for (Object element : (Collection<?>) value) { element = formatUriValue(conversionService, TypeDescriptor.nested(parameter, 1), element); builder.queryParam(name, element); } } else { builder.queryParam(name, formatUriValue(conversionService, new TypeDescriptor(parameter), value)); } }
Example 2
Source File: RequestJsonToPoHandlerMethodArgumentResolver.java From jframework with Apache License 2.0 | 6 votes |
@Override public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest nativeWebRequest, WebDataBinderFactory binderFactory) throws Exception { RequestJsonToPo parameterAnnotation = parameter.getParameterAnnotation(RequestJsonToPo.class); Objects.requireNonNull(parameterAnnotation); String value = parameterAnnotation.value(); Class<?> clazz = parameter.getNestedParameterType(); String jsonParameter = nativeWebRequest.getParameter(value); if (jsonParameter == null) { if (clazz.isAssignableFrom(List.class)) { return Lists.newArrayList(); } return clazz.newInstance(); } jsonParameter = URLDecoder.decode(jsonParameter, StandardCharsets.UTF_8.name()); if (clazz.isAssignableFrom(List.class) || clazz.isAssignableFrom(ArrayList.class)) { String typeName = ((sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl) parameter.getGenericParameterType()).getActualTypeArguments()[0].getTypeName(); Class<?> aClass = Class.forName(typeName); return JsonUtil.toList(jsonParameter, aClass); } else { return JsonUtil.toObject(jsonParameter, clazz); } }
Example 3
Source File: HandlerMethodArgumentResolverSupport.java From spring-analysis-note with MIT License | 5 votes |
/** * Evaluate the {@code Predicate} on the method parameter type if it has the * given annotation, nesting within {@link java.util.Optional} if necessary, * but raise an {@code IllegalStateException} if the same matches the generic * type within a reactive type wrapper. */ protected <A extends Annotation> boolean checkAnnotatedParamNoReactiveWrapper( MethodParameter parameter, Class<A> annotationType, BiPredicate<A, Class<?>> typePredicate) { A annotation = parameter.getParameterAnnotation(annotationType); if (annotation == null) { return false; } parameter = parameter.nestedIfOptional(); Class<?> type = parameter.getNestedParameterType(); ReactiveAdapter adapter = getAdapterRegistry().getAdapter(type); if (adapter != null) { assertHasValues(adapter, parameter); parameter = parameter.nested(); type = parameter.getNestedParameterType(); } if (typePredicate.test(annotation, type)) { if (adapter == null) { return true; } throw buildReactiveWrapperException(parameter); } return false; }
Example 4
Source File: RequestParamMethodArgumentResolver.java From lams with GNU General Public License v2.0 | 5 votes |
@Override public void contributeMethodArgument(MethodParameter parameter, Object value, UriComponentsBuilder builder, Map<String, Object> uriVariables, ConversionService conversionService) { Class<?> paramType = parameter.getNestedParameterType(); if (Map.class.isAssignableFrom(paramType) || MultipartFile.class == paramType || "javax.servlet.http.Part".equals(paramType.getName())) { return; } RequestParam requestParam = parameter.getParameterAnnotation(RequestParam.class); String name = (requestParam == null || StringUtils.isEmpty(requestParam.name()) ? parameter.getParameterName() : requestParam.name()); if (value == null) { if (requestParam != null) { if (!requestParam.required() || !requestParam.defaultValue().equals(ValueConstants.DEFAULT_NONE)) { return; } } builder.queryParam(name); } else if (value instanceof Collection) { for (Object element : (Collection<?>) value) { element = formatUriValue(conversionService, TypeDescriptor.nested(parameter, 1), element); builder.queryParam(name, element); } } else { builder.queryParam(name, formatUriValue(conversionService, new TypeDescriptor(parameter), value)); } }
Example 5
Source File: MultipartResolutionDelegate.java From lams with GNU General Public License v2.0 | 5 votes |
private static Class<?> getCollectionParameterType(MethodParameter methodParam) { Class<?> paramType = methodParam.getNestedParameterType(); if (Collection.class == paramType || List.class.isAssignableFrom(paramType)){ Class<?> valueType = ResolvableType.forMethodParameter(methodParam).asCollection().resolveGeneric(); if (valueType != null) { return valueType; } } return null; }
Example 6
Source File: MultipartResolutionDelegate.java From lams with GNU General Public License v2.0 | 5 votes |
public static boolean isMultipartArgument(MethodParameter parameter) { Class<?> paramType = parameter.getNestedParameterType(); return (MultipartFile.class == paramType || isMultipartFileCollection(parameter) || isMultipartFileArray(parameter) || (servletPartClass != null && (servletPartClass == paramType || isPartCollection(parameter) || isPartArray(parameter)))); }
Example 7
Source File: RequestParamMethodArgumentResolver.java From java-technology-stack with MIT License | 5 votes |
@Override public void contributeMethodArgument(MethodParameter parameter, @Nullable Object value, UriComponentsBuilder builder, Map<String, Object> uriVariables, ConversionService conversionService) { Class<?> paramType = parameter.getNestedParameterType(); if (Map.class.isAssignableFrom(paramType) || MultipartFile.class == paramType || Part.class == paramType) { return; } RequestParam requestParam = parameter.getParameterAnnotation(RequestParam.class); String name = (requestParam == null || StringUtils.isEmpty(requestParam.name()) ? parameter.getParameterName() : requestParam.name()); Assert.state(name != null, "Unresolvable parameter name"); if (value == null) { if (requestParam != null && (!requestParam.required() || !requestParam.defaultValue().equals(ValueConstants.DEFAULT_NONE))) { return; } builder.queryParam(name); } else if (value instanceof Collection) { for (Object element : (Collection<?>) value) { element = formatUriValue(conversionService, TypeDescriptor.nested(parameter, 1), element); builder.queryParam(name, element); } } else { builder.queryParam(name, formatUriValue(conversionService, new TypeDescriptor(parameter), value)); } }
Example 8
Source File: ModelAttributeMethodProcessor.java From java-technology-stack with MIT License | 5 votes |
/** * Extension point to create the model attribute if not found in the model, * with subsequent parameter binding through bean properties (unless suppressed). * <p>The default implementation typically uses the unique public no-arg constructor * if available but also handles a "primary constructor" approach for data classes: * It understands the JavaBeans {@link ConstructorProperties} annotation as well as * runtime-retained parameter names in the bytecode, associating request parameters * with constructor arguments by name. If no such constructor is found, the default * constructor will be used (even if not public), assuming subsequent bean property * bindings through setter methods. * @param attributeName the name of the attribute (never {@code null}) * @param parameter the method parameter declaration * @param binderFactory for creating WebDataBinder instance * @param webRequest the current request * @return the created model attribute (never {@code null}) * @throws BindException in case of constructor argument binding failure * @throws Exception in case of constructor invocation failure * @see #constructAttribute(Constructor, String, MethodParameter, WebDataBinderFactory, NativeWebRequest) * @see BeanUtils#findPrimaryConstructor(Class) */ protected Object createAttribute(String attributeName, MethodParameter parameter, WebDataBinderFactory binderFactory, NativeWebRequest webRequest) throws Exception { MethodParameter nestedParameter = parameter.nestedIfOptional(); Class<?> clazz = nestedParameter.getNestedParameterType(); Constructor<?> ctor = BeanUtils.findPrimaryConstructor(clazz); if (ctor == null) { Constructor<?>[] ctors = clazz.getConstructors(); if (ctors.length == 1) { ctor = ctors[0]; } else { try { ctor = clazz.getDeclaredConstructor(); } catch (NoSuchMethodException ex) { throw new IllegalStateException("No primary or default constructor found for " + clazz, ex); } } } Object attribute = constructAttribute(ctor, attributeName, parameter, binderFactory, webRequest); if (parameter != nestedParameter) { attribute = Optional.of(attribute); } return attribute; }
Example 9
Source File: MultipartResolutionDelegate.java From java-technology-stack with MIT License | 5 votes |
@Nullable private static Class<?> getCollectionParameterType(MethodParameter methodParam) { Class<?> paramType = methodParam.getNestedParameterType(); if (Collection.class == paramType || List.class.isAssignableFrom(paramType)){ Class<?> valueType = ResolvableType.forMethodParameter(methodParam).asCollection().resolveGeneric(); if (valueType != null) { return valueType; } } return null; }
Example 10
Source File: HandlerMethodArgumentResolverSupport.java From java-technology-stack with MIT License | 5 votes |
/** * Evaluate the {@code Predicate} on the method parameter type if it has the * given annotation, nesting within {@link java.util.Optional} if necessary, * but raise an {@code IllegalStateException} if the same matches the generic * type within a reactive type wrapper. */ protected <A extends Annotation> boolean checkAnnotatedParamNoReactiveWrapper( MethodParameter parameter, Class<A> annotationType, BiPredicate<A, Class<?>> typePredicate) { A annotation = parameter.getParameterAnnotation(annotationType); if (annotation == null) { return false; } parameter = parameter.nestedIfOptional(); Class<?> type = parameter.getNestedParameterType(); ReactiveAdapter adapter = getAdapterRegistry().getAdapter(type); if (adapter != null) { assertHasValues(adapter, parameter); parameter = parameter.nested(); type = parameter.getNestedParameterType(); } if (typePredicate.test(annotation, type)) { if (adapter == null) { return true; } throw buildReactiveWrapperException(parameter); } return false; }
Example 11
Source File: CookieValueMethodArgumentResolver.java From java-technology-stack with MIT License | 5 votes |
@Override protected Object resolveNamedValue(String name, MethodParameter parameter, ServerWebExchange exchange) { HttpCookie cookie = exchange.getRequest().getCookies().getFirst(name); Class<?> paramType = parameter.getNestedParameterType(); if (HttpCookie.class.isAssignableFrom(paramType)) { return cookie; } return (cookie != null ? cookie.getValue() : null); }
Example 12
Source File: RequestParamMethodArgumentResolver.java From spring-analysis-note with MIT License | 5 votes |
@Override public void contributeMethodArgument(MethodParameter parameter, @Nullable Object value, UriComponentsBuilder builder, Map<String, Object> uriVariables, ConversionService conversionService) { Class<?> paramType = parameter.getNestedParameterType(); if (Map.class.isAssignableFrom(paramType) || MultipartFile.class == paramType || Part.class == paramType) { return; } RequestParam requestParam = parameter.getParameterAnnotation(RequestParam.class); String name = (requestParam != null && StringUtils.hasLength(requestParam.name()) ? requestParam.name() : parameter.getParameterName()); Assert.state(name != null, "Unresolvable parameter name"); parameter = parameter.nestedIfOptional(); if (value instanceof Optional) { value = ((Optional<?>) value).orElse(null); } if (value == null) { if (requestParam != null && (!requestParam.required() || !requestParam.defaultValue().equals(ValueConstants.DEFAULT_NONE))) { return; } builder.queryParam(name); } else if (value instanceof Collection) { for (Object element : (Collection<?>) value) { element = formatUriValue(conversionService, TypeDescriptor.nested(parameter, 1), element); builder.queryParam(name, element); } } else { builder.queryParam(name, formatUriValue(conversionService, new TypeDescriptor(parameter), value)); } }
Example 13
Source File: MultipartResolutionDelegate.java From spring-analysis-note with MIT License | 5 votes |
@Nullable private static Class<?> getCollectionParameterType(MethodParameter methodParam) { Class<?> paramType = methodParam.getNestedParameterType(); if (Collection.class == paramType || List.class.isAssignableFrom(paramType)){ Class<?> valueType = ResolvableType.forMethodParameter(methodParam).asCollection().resolveGeneric(); if (valueType != null) { return valueType; } } return null; }
Example 14
Source File: CookieValueMethodArgumentResolver.java From spring-analysis-note with MIT License | 5 votes |
@Override protected Object resolveNamedValue(String name, MethodParameter parameter, ServerWebExchange exchange) { HttpCookie cookie = exchange.getRequest().getCookies().getFirst(name); Class<?> paramType = parameter.getNestedParameterType(); if (HttpCookie.class.isAssignableFrom(paramType)) { return cookie; } return (cookie != null ? cookie.getValue() : null); }
Example 15
Source File: MultipartResolutionDelegate.java From java-technology-stack with MIT License | 4 votes |
public static boolean isMultipartArgument(MethodParameter parameter) { Class<?> paramType = parameter.getNestedParameterType(); return (MultipartFile.class == paramType || isMultipartFileCollection(parameter) || isMultipartFileArray(parameter) || (Part.class == paramType || isPartCollection(parameter) || isPartArray(parameter))); }
Example 16
Source File: MultipartResolutionDelegate.java From java-technology-stack with MIT License | 4 votes |
@Nullable public static Object resolveMultipartArgument(String name, MethodParameter parameter, HttpServletRequest request) throws Exception { MultipartHttpServletRequest multipartRequest = WebUtils.getNativeRequest(request, MultipartHttpServletRequest.class); boolean isMultipart = (multipartRequest != null || isMultipartContent(request)); if (MultipartFile.class == parameter.getNestedParameterType()) { if (multipartRequest == null && isMultipart) { multipartRequest = new StandardMultipartHttpServletRequest(request); } return (multipartRequest != null ? multipartRequest.getFile(name) : null); } else if (isMultipartFileCollection(parameter)) { if (multipartRequest == null && isMultipart) { multipartRequest = new StandardMultipartHttpServletRequest(request); } return (multipartRequest != null ? multipartRequest.getFiles(name) : null); } else if (isMultipartFileArray(parameter)) { if (multipartRequest == null && isMultipart) { multipartRequest = new StandardMultipartHttpServletRequest(request); } if (multipartRequest != null) { List<MultipartFile> multipartFiles = multipartRequest.getFiles(name); return multipartFiles.toArray(new MultipartFile[0]); } else { return null; } } else if (Part.class == parameter.getNestedParameterType()) { return (isMultipart ? request.getPart(name): null); } else if (isPartCollection(parameter)) { return (isMultipart ? resolvePartList(request, name) : null); } else if (isPartArray(parameter)) { return (isMultipart ? resolvePartList(request, name).toArray(new Part[0]) : null); } else { return UNRESOLVABLE; } }
Example 17
Source File: RequestPartMethodArgumentResolver.java From spring4-understanding with Apache License 2.0 | 4 votes |
@Override @UsesJava8 public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest request, WebDataBinderFactory binderFactory) throws Exception { HttpServletRequest servletRequest = request.getNativeRequest(HttpServletRequest.class); assertIsMultipartRequest(servletRequest); MultipartHttpServletRequest multipartRequest = WebUtils.getNativeRequest(servletRequest, MultipartHttpServletRequest.class); Class<?> paramType = parameter.getParameterType(); boolean optional = paramType.getName().equals("java.util.Optional"); if (optional) { parameter.increaseNestingLevel(); paramType = parameter.getNestedParameterType(); } String partName = getPartName(parameter); Object arg; if (MultipartFile.class == paramType) { Assert.notNull(multipartRequest, "Expected MultipartHttpServletRequest: is a MultipartResolver configured?"); arg = multipartRequest.getFile(partName); } else if (isMultipartFileCollection(parameter)) { Assert.notNull(multipartRequest, "Expected MultipartHttpServletRequest: is a MultipartResolver configured?"); arg = multipartRequest.getFiles(partName); } else if (isMultipartFileArray(parameter)) { Assert.notNull(multipartRequest, "Expected MultipartHttpServletRequest: is a MultipartResolver configured?"); List<MultipartFile> files = multipartRequest.getFiles(partName); arg = files.toArray(new MultipartFile[files.size()]); } else if ("javax.servlet.http.Part".equals(paramType.getName())) { assertIsMultipartRequest(servletRequest); arg = servletRequest.getPart(partName); } else if (isPartCollection(parameter)) { assertIsMultipartRequest(servletRequest); arg = new ArrayList<Object>(servletRequest.getParts()); } else if (isPartArray(parameter)) { assertIsMultipartRequest(servletRequest); arg = RequestPartResolver.resolvePart(servletRequest); } else { try { HttpInputMessage inputMessage = new RequestPartServletServerHttpRequest(servletRequest, partName); arg = readWithMessageConverters(inputMessage, parameter, parameter.getNestedGenericParameterType()); WebDataBinder binder = binderFactory.createBinder(request, arg, partName); if (arg != null) { validateIfApplicable(binder, parameter); if (binder.getBindingResult().hasErrors() && isBindExceptionRequired(binder, parameter)) { throw new MethodArgumentNotValidException(parameter, binder.getBindingResult()); } } mavContainer.addAttribute(BindingResult.MODEL_KEY_PREFIX + partName, binder.getBindingResult()); } catch (MissingServletRequestPartException ex) { // handled below arg = null; } } RequestPart requestPart = parameter.getParameterAnnotation(RequestPart.class); boolean isRequired = ((requestPart == null || requestPart.required()) && !optional); if (arg == null && isRequired) { throw new MissingServletRequestPartException(partName); } if (optional) { arg = Optional.ofNullable(arg); } return arg; }
Example 18
Source File: MultipartResolutionDelegate.java From lams with GNU General Public License v2.0 | 4 votes |
public static Object resolveMultipartArgument(String name, MethodParameter parameter, HttpServletRequest request) throws Exception { MultipartHttpServletRequest multipartRequest = WebUtils.getNativeRequest(request, MultipartHttpServletRequest.class); boolean isMultipart = (multipartRequest != null || isMultipartContent(request)); if (MultipartFile.class == parameter.getNestedParameterType()) { if (multipartRequest == null && isMultipart) { multipartRequest = adaptToMultipartHttpServletRequest(request); } return (multipartRequest != null ? multipartRequest.getFile(name) : null); } else if (isMultipartFileCollection(parameter)) { if (multipartRequest == null && isMultipart) { multipartRequest = adaptToMultipartHttpServletRequest(request); } return (multipartRequest != null ? multipartRequest.getFiles(name) : null); } else if (isMultipartFileArray(parameter)) { if (multipartRequest == null && isMultipart) { multipartRequest = adaptToMultipartHttpServletRequest(request); } if (multipartRequest != null) { List<MultipartFile> multipartFiles = multipartRequest.getFiles(name); return multipartFiles.toArray(new MultipartFile[multipartFiles.size()]); } else { return null; } } else if (servletPartClass != null) { if (servletPartClass == parameter.getNestedParameterType()) { return (isMultipart ? RequestPartResolver.resolvePart(request, name) : null); } else if (isPartCollection(parameter)) { return (isMultipart ? RequestPartResolver.resolvePartList(request, name) : null); } else if (isPartArray(parameter)) { return (isMultipart ? RequestPartResolver.resolvePartArray(request, name) : null); } } return UNRESOLVABLE; }
Example 19
Source File: MultipartResolutionDelegate.java From spring-analysis-note with MIT License | 4 votes |
@Nullable public static Object resolveMultipartArgument(String name, MethodParameter parameter, HttpServletRequest request) throws Exception { MultipartHttpServletRequest multipartRequest = WebUtils.getNativeRequest(request, MultipartHttpServletRequest.class); boolean isMultipart = (multipartRequest != null || isMultipartContent(request)); if (MultipartFile.class == parameter.getNestedParameterType()) { if (multipartRequest == null && isMultipart) { multipartRequest = new StandardMultipartHttpServletRequest(request); } return (multipartRequest != null ? multipartRequest.getFile(name) : null); } else if (isMultipartFileCollection(parameter)) { if (multipartRequest == null && isMultipart) { multipartRequest = new StandardMultipartHttpServletRequest(request); } return (multipartRequest != null ? multipartRequest.getFiles(name) : null); } else if (isMultipartFileArray(parameter)) { if (multipartRequest == null && isMultipart) { multipartRequest = new StandardMultipartHttpServletRequest(request); } if (multipartRequest != null) { List<MultipartFile> multipartFiles = multipartRequest.getFiles(name); return multipartFiles.toArray(new MultipartFile[0]); } else { return null; } } else if (Part.class == parameter.getNestedParameterType()) { return (isMultipart ? request.getPart(name): null); } else if (isPartCollection(parameter)) { return (isMultipart ? resolvePartList(request, name) : null); } else if (isPartArray(parameter)) { return (isMultipart ? resolvePartList(request, name).toArray(new Part[0]) : null); } else { return UNRESOLVABLE; } }
Example 20
Source File: MultipartResolutionDelegate.java From spring-analysis-note with MIT License | 4 votes |
public static boolean isMultipartArgument(MethodParameter parameter) { Class<?> paramType = parameter.getNestedParameterType(); return (MultipartFile.class == paramType || isMultipartFileCollection(parameter) || isMultipartFileArray(parameter) || (Part.class == paramType || isPartCollection(parameter) || isPartArray(parameter))); }