List of usage examples for java.beans PropertyEditor getValue
Object getValue();
From source file:com.springframework.beans.TypeConverterDelegate.java
/** * Convert the value to the required type (if necessary from a String), * using the given property editor.// w w w . j a v a 2s. c o m * @param oldValue the previous value, if available (may be {@code null}) * @param newValue the proposed new value * @param requiredType the type we must convert to * (or {@code null} if not known, for example in case of a collection element) * @param editor the PropertyEditor to use * @return the new value, possibly the result of type conversion * @throws IllegalArgumentException if type conversion failed */ private Object doConvertValue(Object oldValue, Object newValue, Class<?> requiredType, PropertyEditor editor) { Object convertedValue = newValue; if (editor != null && !(convertedValue instanceof String)) { // Not a String -> use PropertyEditor's setValue. // With standard PropertyEditors, this will return the very same object; // we just want to allow special PropertyEditors to override setValue // for type conversion from non-String values to the required type. try { editor.setValue(convertedValue); Object newConvertedValue = editor.getValue(); if (newConvertedValue != convertedValue) { convertedValue = newConvertedValue; // Reset PropertyEditor: It already did a proper conversion. // Don't use it again for a setAsText call. editor = null; } } catch (Exception ex) { if (logger.isDebugEnabled()) { logger.debug( "PropertyEditor [" + editor.getClass().getName() + "] does not support setValue call", ex); } // Swallow and proceed. } } Object returnValue = convertedValue; if (requiredType != null && !requiredType.isArray() && convertedValue instanceof String[]) { // Convert String array to a comma-separated String. // Only applies if no PropertyEditor converted the String array before. // The CSV String will be passed into a PropertyEditor's setAsText method, if any. if (logger.isTraceEnabled()) { logger.trace("Converting String array to comma-delimited String [" + convertedValue + "]"); } convertedValue = StringUtils.arrayToCommaDelimitedString((String[]) convertedValue); } if (convertedValue instanceof String) { if (editor != null) { // Use PropertyEditor's setAsText in case of a String value. if (logger.isTraceEnabled()) { logger.trace( "Converting String to [" + requiredType + "] using property editor [" + editor + "]"); } String newTextValue = (String) convertedValue; return doConvertTextValue(oldValue, newTextValue, editor); } else if (String.class.equals(requiredType)) { returnValue = convertedValue; } } return returnValue; }
From source file:org.apache.jmeter.testbeans.gui.GenericTestBeanCustomizer.java
/** * Save values from the GUI fields into the property map *///from ww w .j av a2 s .c o m void saveGuiFields() { for (int i = 0; i < editors.length; i++) { PropertyEditor propertyEditor = editors[i]; // might be null (e.g. in testing) if (propertyEditor != null) { Object value = propertyEditor.getValue(); String name = descriptors[i].getName(); if (value == null) { propertyMap.remove(name); if (log.isDebugEnabled()) { log.debug("Unset " + name); } } else { propertyMap.put(name, value); if (log.isDebugEnabled()) { log.debug("Set " + name + "= " + value); } } } } }
From source file:org.hdiv.web.servlet.tags.form.SelectedValueComparatorHDIV.java
private static boolean exhaustiveCompare(Object boundValue, Object candidate, PropertyEditor editor, Map<PropertyEditor, Object> convertedValueCache) { String candidateDisplayString = ValueFormatterHDIV.getDisplayString(candidate, editor, false); if (boundValue instanceof LabeledEnum) { LabeledEnum labeledEnum = (LabeledEnum) boundValue; String enumCodeAsString = ObjectUtils.getDisplayString(labeledEnum.getCode()); if (enumCodeAsString.equals(candidateDisplayString)) { return true; }//ww w .ja v a 2 s . c om String enumLabelAsString = ObjectUtils.getDisplayString(labeledEnum.getLabel()); if (enumLabelAsString.equals(candidateDisplayString)) { return true; } } else if (boundValue.getClass().isEnum()) { Enum boundEnum = (Enum) boundValue; String enumCodeAsString = ObjectUtils.getDisplayString(boundEnum.name()); if (enumCodeAsString.equals(candidateDisplayString)) { return true; } String enumLabelAsString = ObjectUtils.getDisplayString(boundEnum.toString()); if (enumLabelAsString.equals(candidateDisplayString)) { return true; } } else if (ObjectUtils.getDisplayString(boundValue).equals(candidateDisplayString)) { return true; } else if (editor != null && candidate instanceof String) { // Try PE-based comparison (PE should *not* be allowed to escape creating thread) String candidateAsString = (String) candidate; Object candidateAsValue; if (convertedValueCache != null && convertedValueCache.containsKey(editor)) { candidateAsValue = convertedValueCache.get(editor); } else { editor.setAsText(candidateAsString); candidateAsValue = editor.getValue(); if (convertedValueCache != null) { convertedValueCache.put(editor, candidateAsValue); } } if (ObjectUtils.nullSafeEquals(boundValue, candidateAsValue)) { return true; } } return false; }
From source file:org.springframework.beans.TypeConverterDelegate.java
/** * Convert the given text value using the given property editor. * @param oldValue the previous value, if available (may be {@code null}) * @param newTextValue the proposed text value * @param editor the PropertyEditor to use * @return the converted value// w w w. ja v a 2 s . c om */ private Object doConvertTextValue(@Nullable Object oldValue, String newTextValue, PropertyEditor editor) { try { editor.setValue(oldValue); } catch (Exception ex) { if (logger.isDebugEnabled()) { logger.debug("PropertyEditor [" + editor.getClass().getName() + "] does not support setValue call", ex); } // Swallow and proceed. } editor.setAsText(newTextValue); return editor.getValue(); }
From source file:net.sf.juffrou.reflect.JuffrouTypeConverterDelegate.java
/** * Convert the given text value using the given property editor. * /*from w w w . j a va 2s. c o m*/ * @param oldValue * the previous value, if available (may be <code>null</code>) * @param newTextValue * the proposed text value * @param editor * the PropertyEditor to use * @return the converted value */ private Object doConvertTextValue(Object oldValue, String newTextValue, PropertyEditor editor) { try { editor.setValue(oldValue); } catch (Exception ex) { if (logger.isDebugEnabled()) { logger.debug("PropertyEditor [" + editor.getClass().getName() + "] does not support setValue call", ex); } // Swallow and proceed. } editor.setAsText(newTextValue); return editor.getValue(); }
From source file:org.springframework.beans.TypeConverterDelegate.java
/** * Convert the value to the required type (if necessary from a String), * using the given property editor./* w w w .jav a 2s . co m*/ * @param oldValue the previous value, if available (may be {@code null}) * @param newValue the proposed new value * @param requiredType the type we must convert to * (or {@code null} if not known, for example in case of a collection element) * @param editor the PropertyEditor to use * @return the new value, possibly the result of type conversion * @throws IllegalArgumentException if type conversion failed */ @Nullable private Object doConvertValue(@Nullable Object oldValue, @Nullable Object newValue, @Nullable Class<?> requiredType, @Nullable PropertyEditor editor) { Object convertedValue = newValue; if (editor != null && !(convertedValue instanceof String)) { // Not a String -> use PropertyEditor's setValue. // With standard PropertyEditors, this will return the very same object; // we just want to allow special PropertyEditors to override setValue // for type conversion from non-String values to the required type. try { editor.setValue(convertedValue); Object newConvertedValue = editor.getValue(); if (newConvertedValue != convertedValue) { convertedValue = newConvertedValue; // Reset PropertyEditor: It already did a proper conversion. // Don't use it again for a setAsText call. editor = null; } } catch (Exception ex) { if (logger.isDebugEnabled()) { logger.debug( "PropertyEditor [" + editor.getClass().getName() + "] does not support setValue call", ex); } // Swallow and proceed. } } Object returnValue = convertedValue; if (requiredType != null && !requiredType.isArray() && convertedValue instanceof String[]) { // Convert String array to a comma-separated String. // Only applies if no PropertyEditor converted the String array before. // The CSV String will be passed into a PropertyEditor's setAsText method, if any. if (logger.isTraceEnabled()) { logger.trace("Converting String array to comma-delimited String [" + convertedValue + "]"); } convertedValue = StringUtils.arrayToCommaDelimitedString((String[]) convertedValue); } if (convertedValue instanceof String) { if (editor != null) { // Use PropertyEditor's setAsText in case of a String value. if (logger.isTraceEnabled()) { logger.trace( "Converting String to [" + requiredType + "] using property editor [" + editor + "]"); } String newTextValue = (String) convertedValue; return doConvertTextValue(oldValue, newTextValue, editor); } else if (String.class == requiredType) { returnValue = convertedValue; } } return returnValue; }
From source file:org.tinygroup.beanwrapper.TypeConverterDelegate.java
/** * Convert the value to the required type (if necessary from a String), * using the given property editor.//from w w w. ja v a 2 s. c om * @param oldValue the previous value, if available (may be <code>null</code>) * @param newValue the proposed new value * @param requiredType the type we must convert to * (or <code>null</code> if not known, for example in case of a collection element) * @param editor the PropertyEditor to use * @return the new value, possibly the result of type conversion * @throws IllegalArgumentException if type conversion failed */ protected Object doConvertValue(Object oldValue, Object newValue, Class requiredType, PropertyEditor editor) { Object convertedValue = newValue; boolean sharedEditor = false; if (editor != null) { sharedEditor = this.propertyEditorRegistry.isSharedEditor(editor); } if (editor != null && !(convertedValue instanceof String)) { // Not a String -> use PropertyEditor's setValue. // With standard PropertyEditors, this will return the very same object; // we just want to allow special PropertyEditors to override setValue // for type conversion from non-String values to the required type. try { Object newConvertedValue = null; if (sharedEditor) { // Synchronized access to shared editor instance. synchronized (editor) { editor.setValue(convertedValue); newConvertedValue = editor.getValue(); } } else { // Unsynchronized access to non-shared editor instance. editor.setValue(convertedValue); newConvertedValue = editor.getValue(); } if (newConvertedValue != convertedValue) { convertedValue = newConvertedValue; // Reset PropertyEditor: It already did a proper conversion. // Don't use it again for a setAsText call. editor = null; } } catch (Exception ex) { if (logger.isDebugEnabled()) { logger.debug( "PropertyEditor [" + editor.getClass().getName() + "] does not support setValue call", ex); } // Swallow and proceed. } } if (requiredType != null && !requiredType.isArray() && convertedValue instanceof String[]) { // Convert String array to a comma-separated String. // Only applies if no PropertyEditor converted the String array before. // The CSV String will be passed into a PropertyEditor's setAsText method, if any. if (logger.isTraceEnabled()) { logger.trace("Converting String array to comma-delimited String [" + convertedValue + "]"); } convertedValue = StringUtils.arrayToCommaDelimitedString((String[]) convertedValue); } if (editor != null && convertedValue instanceof String) { // Use PropertyEditor's setAsText in case of a String value. if (logger.isTraceEnabled()) { logger.trace("Converting String to [" + requiredType + "] using property editor [" + editor + "]"); } String newTextValue = (String) convertedValue; if (sharedEditor) { // Synchronized access to shared editor instance. synchronized (editor) { return doConvertTextValue(oldValue, newTextValue, editor); } } else { // Unsynchronized access to non-shared editor instance. return doConvertTextValue(oldValue, newTextValue, editor); } } return convertedValue; }
From source file:net.sf.juffrou.reflect.JuffrouTypeConverterDelegate.java
/** * Convert the value to the required type (if necessary from a String), using the given property editor. * // w w w. ja v a 2 s . c o m * @param oldValue * the previous value, if available (may be <code>null</code>) * @param newValue * the proposed new value * @param requiredType * the type we must convert to (or <code>null</code> if not known, for example in case of a collection * element) * @param editor * the PropertyEditor to use * @return the new value, possibly the result of type conversion * @throws IllegalArgumentException * if type conversion failed */ private Object doConvertValue(Object oldValue, Object newValue, Class<?> requiredType, PropertyEditor editor) { Object convertedValue = newValue; boolean sharedEditor = false; if (editor != null) { sharedEditor = this.propertyEditorRegistry.isSharedEditor(editor); } if (editor != null && !(convertedValue instanceof String)) { // Not a String -> use PropertyEditor's setValue. // With standard PropertyEditors, this will return the very same object; // we just want to allow special PropertyEditors to override setValue // for type conversion from non-String values to the required type. try { Object newConvertedValue; if (sharedEditor) { // Synchronized access to shared editor instance. synchronized (editor) { editor.setValue(convertedValue); newConvertedValue = editor.getValue(); } } else { // Unsynchronized access to non-shared editor instance. editor.setValue(convertedValue); newConvertedValue = editor.getValue(); } if (newConvertedValue != convertedValue) { convertedValue = newConvertedValue; // Reset PropertyEditor: It already did a proper conversion. // Don't use it again for a setAsText call. editor = null; } } catch (Exception ex) { if (logger.isDebugEnabled()) { logger.debug( "PropertyEditor [" + editor.getClass().getName() + "] does not support setValue call", ex); } // Swallow and proceed. } } Object returnValue = convertedValue; if (requiredType != null && !requiredType.isArray() && convertedValue instanceof String[]) { // Convert String array to a comma-separated String. // Only applies if no PropertyEditor converted the String array before. // The CSV String will be passed into a PropertyEditor's setAsText method, if any. if (logger.isTraceEnabled()) { logger.trace("Converting String array to comma-delimited String [" + convertedValue + "]"); } convertedValue = StringUtils.arrayToCommaDelimitedString((String[]) convertedValue); } if (convertedValue instanceof String) { if (editor != null) { // Use PropertyEditor's setAsText in case of a String value. if (logger.isTraceEnabled()) { logger.trace( "Converting String to [" + requiredType + "] using property editor [" + editor + "]"); } String newTextValue = (String) convertedValue; if (sharedEditor) { // Synchronized access to shared editor instance. synchronized (editor) { return doConvertTextValue(oldValue, newTextValue, editor); } } else { // Unsynchronized access to non-shared editor instance. return doConvertTextValue(oldValue, newTextValue, editor); } } else if (String.class.equals(requiredType)) { returnValue = convertedValue; } } return returnValue; }
From source file:com.interface21.beans.BeanWrapperImpl.java
/** * Convert the value to the required type (if necessary from a String). * Conversions from String to any type use the setAsText() method of * the PropertyEditor class. Note that a PropertyEditor must be registered * for this class for this to work. This is a standard Java Beans API. * A number of property editors are automatically registered by this class. * @param target target bean/*from ww w. j a va 2 s . c om*/ * @param propertyName name of the property * @param oldValue previous value, if available. May be null. * @param newValue proposed change value. * @param requiredType type we must convert to * @throws BeansException if there is an internal error * @return new value, possibly the result of type convertion. */ public Object doTypeConversionIfNecessary(Object target, String propertyName, Object oldValue, Object newValue, Class requiredType) throws BeansException { // Only need to cast if value isn't null if (newValue != null) { // We may need to change the value of newValue // custom editor for this type? PropertyEditor pe = findCustomEditor(requiredType, propertyName); if ((pe != null || !requiredType.isAssignableFrom(newValue.getClass())) && (newValue instanceof String)) { if (logger.isDebugEnabled()) logger.debug("Convert: String to " + requiredType); if (pe == null) { // no custom editor -> check BeanWrapper's default editors pe = (PropertyEditor) defaultEditors.get(requiredType); if (pe == null) { // no BeanWrapper default editor -> check standard editors pe = PropertyEditorManager.findEditor(requiredType); } } if (logger.isDebugEnabled()) logger.debug("Using property editor [" + pe + "]"); if (pe != null) { try { pe.setAsText((String) newValue); newValue = pe.getValue(); } catch (IllegalArgumentException ex) { throw new TypeMismatchException( new PropertyChangeEvent(target, propertyName, oldValue, newValue), requiredType, ex); } } } } return newValue; }
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 ww w. j av a 2s .c o m*/ * @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); } } } }