Example usage for org.springframework.web.bind WebDataBinder findCustomEditor

List of usage examples for org.springframework.web.bind WebDataBinder findCustomEditor

Introduction

In this page you can find the example usage for org.springframework.web.bind WebDataBinder findCustomEditor.

Prototype

@Override
    @Nullable
    public PropertyEditor findCustomEditor(@Nullable Class<?> requiredType, @Nullable String propertyPath) 

Source Link

Usage

From source file:com.p5solutions.core.json.JsonDeserializer.java

/**
 * Sets the value. May also use the {@link ConversionService} to convert
 * types, such as {@link Date} using the {@link DateTimeFormat}. @see
 * //from   w  w w.  j  av a  2 s.  c om
 * @param method
 *          the method
 * @param fieldName
 *          the field name
 * @param target
 *          the target
 * @param value
 *          the value
 * @param binder
 *          the binder {@link ConversionService} and implementation of custom
 *          converters by implementing {@link GenericConverter}
 * @throws Exception
 */
protected void setValue(Method method, String fieldName, Object target, Object value, WebRequest webRequest,
        WebDataBinder binder) {

    // Expose the real method, if proxied, since annotations need to be found.
    Method realMethod = method;
    if (target instanceof Targetable) {
        Targetable proxy = (Targetable) target;
        Class<?> clazz = proxy.getTarget().getClass();

        realMethod = ReflectionUtility.findMethod(clazz, realMethod.getName());
    }
    // TODO expose TrackStateUtility as part of Core??
    // Method realMethod = TrackStateUtility.exposeRealMethod(method, target);

    if (realMethod == null && method == null) {
        // if there are any binding, or formatting issues, put an error
        // in the model state.

        Object tempState = webRequest.getAttribute(ModelState.MODEL_STATE, WebRequest.SCOPE_REQUEST);
        if (tempState == null) {
            tempState = new ModelState();
        }
        ModelState modelState = (ModelState) tempState;
        modelState.add(fieldName,
                "Cannot bind value " + value + " to target object "
                        + (target != null ? target.getClass() : "<null>"),
                new RuntimeException(
                        "Field " + fieldName + " does not exist for " + target.getClass().getName()));
        webRequest.setAttribute(ModelState.MODEL_STATE, modelState, WebRequest.SCOPE_REQUEST);
    }
    // get the nullable property annotation, if any
    JsonNotNullProperty jnullpt = ReflectionUtility.findAnnotation(realMethod, JsonNotNullProperty.class);

    // get the json property, if any
    JsonProperty jpt = ReflectionUtility.findAnnotation(realMethod, JsonProperty.class);

    Class<?> returnType = method.getReturnType();
    if (ReflectionUtility.isNumberClass(returnType)) {

        try {
            Object numeric = NumberUtils.valueOf(value.toString(), returnType);
            ReflectionUtility.setValue(fieldName, target, numeric);
        } catch (NumberFormatException nfe) {
            // if there are any binding, or formatting issues, put an error
            // in the model state.

            Object tempState = webRequest.getAttribute(ModelState.MODEL_STATE, WebRequest.SCOPE_REQUEST);
            if (tempState == null) {
                tempState = new ModelState();
            }
            ModelState modelState = (ModelState) tempState;
            modelState.add(fieldName, "Cannot bind value " + value + " to target object "
                    + (target != null ? target.getClass() : "<null>"), nfe);
            webRequest.setAttribute(ModelState.MODEL_STATE, modelState, WebRequest.SCOPE_REQUEST);
        }

    } else if (ReflectionUtility.isStringClass(returnType)) {

        // set empty values to null
        String sv = (String) value;
        sv = Comparison.isEmpty(sv) ? null : sv;

        // if the Nullable property is et with emptyNull to false
        // then actually set the value, even if its empty.
        if (jnullpt != null) {
            sv = (String) value;
        }

        // unescape the sting character.
        sv = unescape(sv);
        sv = unnull(sv);

        ReflectionUtility.setValue(fieldName, target, sv);
    } else if (!attemptListMapping(fieldName, target, value, returnType, jpt, webRequest, binder)) {

        // / attempt to map of Map<?,?>
        if (!attemptMapMapping(fieldName, target, value, returnType, jpt, webRequest, binder)) {

            // attempt to simple map the object
            if (!attemptSimpleMapping(fieldName, target, value, returnType, webRequest, binder)) {

                // Use the Spring Conversion service and try to map the
                // values
                TypeDescriptor sourceType = TypeDescriptor.forObject(value);

                // specify the method, -1 such that it uses the return value
                // type
                MethodParameter mp = new MethodParameter(realMethod, -1);

                // setup the type descriptor and pass it to the converter
                TypeDescriptor targetType = new TypeDescriptor(mp);

                // PropertyValue pv = new PropertyValue(fieldName, value);

                Object converted = null;
                PropertyEditor editor = null;
                if (binder != null) {
                    editor = binder.findCustomEditor(returnType, null);
                }

                if (editor != null) {
                    editor.setAsText(value.toString());
                    converted = editor.getValue();
                } else if (conversionService != null) {
                    // use the conversion service to translate the value
                    converted = this.conversionService.convert(value, sourceType, targetType);
                }

                // set the converted value, if any
                ReflectionUtility.setValue(fieldName, target, converted);
            }
        }
    }
}