List of usage examples for org.springframework.validation DataBinder initDirectFieldAccess
public void initDirectFieldAccess()
From source file:org.synyx.hades.extensions.web.PageableArgumentResolver.java
public Object resolveArgument(MethodParameter methodParameter, NativeWebRequest webRequest) { if (methodParameter.getParameterType().equals(Pageable.class)) { assertPageableUniqueness(methodParameter); Pageable request = getDefaultFromAnnotationOrFallback(methodParameter); ServletRequest servletRequest = (ServletRequest) webRequest.getNativeRequest(); PropertyValues propertyValues = new ServletRequestParameterPropertyValues(servletRequest, getPrefix(methodParameter), separator); DataBinder binder = new ServletRequestDataBinder(request); binder.initDirectFieldAccess(); binder.registerCustomEditor(Sort.class, new SortPropertyEditor("sort.dir", propertyValues)); binder.bind(propertyValues);/*from www .j a va2s. co m*/ if (request.getPageNumber() > 0) { request = new PageRequest(request.getPageNumber() - 1, request.getPageSize(), request.getSort()); } return request; } return UNRESOLVED; }
From source file:org.uimafit.component.initialize.ConfigurationParameterInitializer.java
/** * Initialize a component from an {@link UimaContext} This code can be a little confusing * because the configuration parameter annotations are used in two contexts: in describing the * component and to initialize member variables from a {@link UimaContext}. Here we are * performing the latter task. It is important to remember that the {@link UimaContext} passed * in to this method may or may not have been derived using reflection of the annotations (i.e. * using {@link ConfigurationParameterFactory} via e.g. a call to a AnalysisEngineFactory.create * method). It is just as possible for the description of the component to come directly from an * XML descriptor file. So, for example, just because a configuration parameter specifies a * default value, this does not mean that the passed in context will have a value for that * configuration parameter. It should be possible for a descriptor file to specify its own value * or to not provide one at all. If the context does not have a configuration parameter, then * the default value provided by the developer as specified by the defaultValue element of the * {@link ConfigurationParameter} will be used. See comments in the code for additional details. * * @param component the component to initialize. * @param context a UIMA context with configuration parameters. *///from w w w .ja va 2s . c om public static void initialize(final Object component, final UimaContext context) throws ResourceInitializationException { MutablePropertyValues values = new MutablePropertyValues(); List<String> mandatoryValues = new ArrayList<String>(); for (Field field : ReflectionUtil.getFields(component)) { // component.getClass().getDeclaredFields()) if (ConfigurationParameterFactory.isConfigurationParameterField(field)) { org.uimafit.descriptor.ConfigurationParameter annotation = field .getAnnotation(org.uimafit.descriptor.ConfigurationParameter.class); Object parameterValue; String parameterName = ConfigurationParameterFactory.getConfigurationParameterName(field); // Obtain either from the context - or - if the context does not provide the // parameter, check if there is a default value. Note there are three possibilities: // 1) Parameter present and set // 2) Parameter present and set to null (null value) // 3) Parameter not present (also provided as null value by UIMA) // Unfortunately we cannot make a difference between case 2 and 3 since UIMA does // not allow us to actually get a list of the parameters set in the context. We can // only get a list of the declared parameters. Thus we have to rely on the null // value. parameterValue = context.getConfigParameterValue(parameterName); if (parameterValue == null) { parameterValue = ConfigurationParameterFactory.getDefaultValue(field); } if (parameterValue != null) { values.addPropertyValue(field.getName(), parameterValue); } // TODO does this check really belong here? It seems that // this check is already performed by UIMA if (annotation.mandatory()) { mandatoryValues.add(field.getName()); // if (parameterValue == null) { // final String key = ResourceInitializationException.CONFIG_SETTING_ABSENT; // throw new ResourceInitializationException(key, // new Object[] { configurationParameterName }); // } } // else { // if (parameterValue == null) { // continue; // } // } // final Object fieldValue = convertValue(field, parameterValue); // try { // setParameterValue(component, field, fieldValue); // } // catch (Exception e) { // throw new ResourceInitializationException(e); // } } } DataBinder binder = new DataBinder(component) { @Override protected void checkRequiredFields(MutablePropertyValues mpvs) { String[] requiredFields = getRequiredFields(); if (!ObjectUtils.isEmpty(requiredFields)) { Map<String, PropertyValue> propertyValues = new HashMap<String, PropertyValue>(); PropertyValue[] pvs = mpvs.getPropertyValues(); for (PropertyValue pv : pvs) { String canonicalName = PropertyAccessorUtils.canonicalPropertyName(pv.getName()); propertyValues.put(canonicalName, pv); } for (String field : requiredFields) { PropertyValue pv = propertyValues.get(field); boolean empty = (pv == null || pv.getValue() == null); // For our purposes, empty Strings or empty String arrays do not count as // empty. Empty is only "null". // if (!empty) { // if (pv.getValue() instanceof String) { // empty = !StringUtils.hasText((String) pv.getValue()); // } // else if (pv.getValue() instanceof String[]) { // String[] values = (String[]) pv.getValue(); // empty = (values.length == 0 || !StringUtils.hasText(values[0])); // } // } if (empty) { // Use bind error processor to create FieldError. getBindingErrorProcessor().processMissingFieldError(field, getInternalBindingResult()); // Remove property from property values to bind: // It has already caused a field error with a rejected value. if (pv != null) { mpvs.removePropertyValue(pv); propertyValues.remove(field); } } } } } }; binder.initDirectFieldAccess(); PropertyEditorUtil.registerUimaFITEditors(binder); binder.setRequiredFields(mandatoryValues.toArray(new String[mandatoryValues.size()])); binder.bind(values); if (binder.getBindingResult().hasErrors()) { StringBuilder sb = new StringBuilder(); sb.append("Errors initializing [" + component.getClass() + "]"); for (ObjectError error : binder.getBindingResult().getAllErrors()) { if (sb.length() > 0) { sb.append("\n"); } sb.append(error.getDefaultMessage()); } throw new IllegalArgumentException(sb.toString()); } }