@Test @SuppressWarnings("rawtypes") public void resolveOptional() throws Exception { ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer(); initializer.setConversionService(new DefaultConversionService()); WebDataBinderFactory binderFactory = new DefaultDataBinderFactory(initializer); Object result = resolver.resolveArgument(paramOptional, null, webRequest, binderFactory); assertEquals(Optional.class, result.getClass()); assertEquals(Optional.empty(), result); this.request.addParameter("name", "123"); result = resolver.resolveArgument(paramOptional, null, webRequest, binderFactory); assertEquals(Optional.class, result.getClass()); assertEquals(123, ((Optional) result).get()); }
@Override @SuppressWarnings("unchecked") public <T> T provide(Method method, int parameterIndex) { MethodParameter methodParameter = new MethodParameter(method, parameterIndex); ModelAndViewContainer modelAndViewContainer = new ModelAndViewContainer(); NativeWebRequest webRequest = new ServletWebRequest(request); DefaultDataBinderFactory binderFactory = new DefaultDataBinderFactory(new ConfigurableWebBindingInitializer()); try { return (T) argumentResolvers.resolveArgument(methodParameter, modelAndViewContainer, webRequest, binderFactory); } catch (Exception e) { throw new IllegalStateException(e); } }
private InvocableHandlerMethod createInitBinderMethod(Object bean, Method method) { InvocableHandlerMethod binderMethod = new InvocableHandlerMethod(bean, method); binderMethod.setHandlerMethodArgumentResolvers(this.initBinderArgumentResolvers); binderMethod.setDataBinderFactory(new DefaultDataBinderFactory(this.webBindingInitializer)); binderMethod.setParameterNameDiscoverer(this.parameterNameDiscoverer); return binderMethod; }
private void runTest(Object controller) throws Exception { HandlerMethodArgumentResolverComposite resolvers = new HandlerMethodArgumentResolverComposite(); resolvers.addResolver(new ModelAttributeMethodProcessor(false)); resolvers.addResolver(new ModelMethodProcessor()); WebDataBinderFactory dataBinderFactory = new DefaultDataBinderFactory(null); Class<?> type = controller.getClass(); Set<Method> methods = MethodIntrospector.selectMethods(type, METHOD_FILTER); List<InvocableHandlerMethod> modelMethods = new ArrayList<InvocableHandlerMethod>(); for (Method method : methods) { InvocableHandlerMethod modelMethod = new InvocableHandlerMethod(controller, method); modelMethod.setHandlerMethodArgumentResolvers(resolvers); modelMethod.setDataBinderFactory(dataBinderFactory); modelMethods.add(modelMethod); } Collections.shuffle(modelMethods); SessionAttributesHandler sessionHandler = new SessionAttributesHandler(type, this.sessionAttributeStore); ModelFactory factory = new ModelFactory(modelMethods, dataBinderFactory, sessionHandler); factory.initModel(this.webRequest, this.mavContainer, new HandlerMethod(controller, "handle")); if (logger.isDebugEnabled()) { StringBuilder sb = new StringBuilder(); for (String name : getInvokedMethods()) { sb.append(" >> ").append(name); } logger.debug(sb); } }
private WebDataBinderFactory createBinderFactory(String methodName, Class<?>... parameterTypes) throws Exception { Object handler = new InitBinderHandler(); Method method = handler.getClass().getMethod(methodName, parameterTypes); InvocableHandlerMethod handlerMethod = new InvocableHandlerMethod(handler, method); handlerMethod.setHandlerMethodArgumentResolvers(argumentResolvers); handlerMethod.setDataBinderFactory(new DefaultDataBinderFactory(null)); handlerMethod.setParameterNameDiscoverer(new LocalVariableTableParameterNameDiscoverer()); return new InitBinderDataBinderFactory(Arrays.asList(handlerMethod), bindingInitializer); }
@Override @SuppressWarnings("unchecked") public <T> T provide(Method method, int parameterIndex) { MethodParameter methodParameter = new MethodParameter(method, parameterIndex); ModelAndViewContainer modelAndViewContainer = new ModelAndViewContainer(); NativeWebRequest webRequest = new ServletWebRequest(request); DefaultDataBinderFactory binderFactory = new DefaultDataBinderFactory(new ConfigurableWebBindingInitializer()); try { return (T) argumentResolvers.resolveArgument(methodParameter, modelAndViewContainer, webRequest, binderFactory); } catch (Exception e) { throw new RuntimeException(e); } }
@Before public void setUp() throws Exception { MockServletContext sc = new MockServletContext(); request = new MockHttpServletRequest(sc); webRequest = new ServletWebRequest(request); RequestContextHolder.setRequestAttributes(webRequest); request.setAttribute(DispatcherServlet.OUTPUT_FLASH_MAP_ATTRIBUTE, new FlashMap()); factory = new DefaultDataBinderFactory(new ConfigurableWebBindingInitializer()); }