List of usage examples for org.springframework.beans BeanWrapper getPropertyValue
@Nullable
Object getPropertyValue(String propertyName) throws BeansException;
From source file:org.springframework.beans.factory.config.PropertyPathFactoryBean.java
@Override @Nullable/*from w w w. j a v a 2 s . co m*/ public Object getObject() throws BeansException { BeanWrapper target = this.targetBeanWrapper; if (target != null) { if (logger.isWarnEnabled() && this.targetBeanName != null && this.beanFactory instanceof ConfigurableBeanFactory && ((ConfigurableBeanFactory) this.beanFactory).isCurrentlyInCreation(this.targetBeanName)) { logger.warn("Target bean '" + this.targetBeanName + "' is still in creation due to a circular " + "reference - obtained value for property '" + this.propertyPath + "' may be outdated!"); } } else { // Fetch prototype target bean... Assert.state(this.beanFactory != null, "No BeanFactory available"); Assert.state(this.targetBeanName != null, "No target bean name specified"); Object bean = this.beanFactory.getBean(this.targetBeanName); target = PropertyAccessorFactory.forBeanPropertyAccess(bean); } Assert.state(this.propertyPath != null, "No property path specified"); return target.getPropertyValue(this.propertyPath); }
From source file:org.springframework.cloud.stream.app.field.value.counter.sink.FieldValueCounterSinkConfiguration.java
private void processPojo(String counterName, Object payload) { String fieldName = this.fvcSinkProperties.getFieldName(); Object value = null;// w ww. j ava 2 s. c o m if (payload instanceof Map) { Map map = (Map) payload; if (map.containsKey(fieldName)) { value = map.get(fieldName); } else { log.error("The property '" + fieldName + "' is not available in the payload: " + payload); } } else { BeanWrapper beanWrapper = new BeanWrapperImpl(payload); if (beanWrapper.isReadableProperty(fieldName)) { value = beanWrapper.getPropertyValue(fieldName); } else { log.error("The property '" + fieldName + "' is not available in the payload: " + payload); } } if (value != null) { processValue(counterName, value); } else { log.info("The value for the property '" + fieldName + "' in the payload '" + payload + "' is null. Ignored"); } }
From source file:org.springframework.data.jdbc.support.oracle.BeanPropertyStructMapper.java
public STRUCT toStruct(T source, Connection conn, String typeName) throws SQLException { StructDescriptor descriptor = new StructDescriptor(typeName, conn); ResultSetMetaData rsmd = descriptor.getMetaData(); int columns = rsmd.getColumnCount(); Object[] values = new Object[columns]; for (int i = 1; i <= columns; i++) { String column = JdbcUtils.lookupColumnName(rsmd, i).toLowerCase(); PropertyDescriptor fieldMeta = (PropertyDescriptor) this.mappedFields.get(column); if (fieldMeta != null) { BeanWrapper bw = new BeanWrapperImpl(source); if (bw.isReadableProperty(fieldMeta.getName())) { try { if (logger.isDebugEnabled()) { logger.debug("Mapping column named \"" + column + "\"" + " to property \"" + fieldMeta.getName() + "\""); }//from w ww.j av a2 s . c om values[i - 1] = bw.getPropertyValue(fieldMeta.getName()); } catch (NotReadablePropertyException ex) { throw new DataRetrievalFailureException( "Unable to map column " + column + " to property " + fieldMeta.getName(), ex); } } else { logger.warn("Unable to access the getter for " + fieldMeta.getName() + ". Check that " + "get" + StringUtils.capitalize(fieldMeta.getName()) + " is declared and has public access."); } } } STRUCT struct = new STRUCT(descriptor, conn, values); return struct; }
From source file:org.springframework.faces.mvc.bind.ReverseDataBinder.java
/** * Perform the reverse bind on the <tt>dataBinder</tt> provided in the constructor. Note: Calling with method will * also trigger a <tt>bind</tt> operation on the <tt>dataBinder</tt>. This method returns {@link PropertyValues} * containing a name/value pairs for each property that can be bound. Property values are encoded as Strings using * the property editors bound to the original dataBinder. * @return property values that could be re-bound using the data binder * @throws IllegalStateException if the target object values cannot be bound */// ww w . j a v a 2 s . c o m public PropertyValues reverseBind() { Assert.notNull(dataBinder.getTarget(), "ReverseDataBinder.reverseBind can only be used with a DataBinder that has a target object"); MutablePropertyValues rtn = new MutablePropertyValues(); BeanWrapper target = PropertyAccessorFactory.forBeanPropertyAccess(dataBinder.getTarget()); PropertyDescriptor[] propertyDescriptors = target.getPropertyDescriptors(); BeanWrapper defaultValues = null; if (skipDefaultValues) { defaultValues = newDefaultTargetValues(dataBinder.getTarget()); } for (int i = 0; i < propertyDescriptors.length; i++) { PropertyDescriptor property = propertyDescriptors[i]; String propertyName = getPropertyName(property); Object propertyValue = target.getPropertyValue(propertyName); if (isSkippedProperty(property)) { continue; } if (!isMutableProperty(property)) { if (logger.isDebugEnabled()) { logger.debug("Ignoring '" + propertyName + "' due to missing read/write methods"); } continue; } if (defaultValues != null && ObjectUtils.nullSafeEquals(defaultValues.getPropertyValue(propertyName), propertyValue)) { if (logger.isDebugEnabled()) { logger.debug("Skipping '" + propertyName + "' as property contains default value"); } continue; } // Find a property editor PropertyEditorRegistrySupport propertyEditorRegistrySupport = null; if (target instanceof PropertyEditorRegistrySupport) { propertyEditorRegistrySupport = (PropertyEditorRegistrySupport) target; } PropertyEditor propertyEditor = findEditor(propertyEditorRegistrySupport, target.getWrappedInstance(), target.getPropertyType(propertyName), property); // Convert and store the value String convertedPropertyValue = convertToStringUsingPropertyEditor(propertyValue, propertyEditor); if (convertedPropertyValue != null) { rtn.addPropertyValue(propertyName, convertedPropertyValue); } } dataBinder.bind(rtn); BindingResult bindingResult = dataBinder.getBindingResult(); if (bindingResult.hasErrors()) { throw new IllegalStateException("Unable to reverse bind from target '" + dataBinder.getObjectName() + "', the properties '" + rtn + "' will result in binding errors when re-bound " + bindingResult.getAllErrors()); } return rtn; }
From source file:org.springframework.springfaces.mvc.bind.ReverseDataBinder.java
/** * Perform the reverse bind on the <tt>dataBinder</tt> provided in the constructor. Note: Calling with method will * also trigger a <tt>bind</tt> operation on the <tt>dataBinder</tt>. This method returns {@link PropertyValues} * containing a name/value pairs for each property that can be bound. Property values are encoded as Strings using * the property editors bound to the original dataBinder. * @return property values that could be re-bound using the data binder * @throws IllegalStateException if the target object values cannot be bound *//*from ww w .java 2s . c om*/ public PropertyValues reverseBind() { Assert.notNull(this.dataBinder.getTarget(), "ReverseDataBinder.reverseBind can only be used with a DataBinder that has a target object"); MutablePropertyValues rtn = new MutablePropertyValues(); BeanWrapper target = PropertyAccessorFactory.forBeanPropertyAccess(this.dataBinder.getTarget()); ConversionService conversionService = this.dataBinder.getConversionService(); if (conversionService != null) { target.setConversionService(conversionService); } PropertyDescriptor[] propertyDescriptors = target.getPropertyDescriptors(); BeanWrapper defaultValues = null; if (this.skipDefaultValues) { defaultValues = newDefaultTargetValues(this.dataBinder.getTarget()); } for (int i = 0; i < propertyDescriptors.length; i++) { PropertyDescriptor property = propertyDescriptors[i]; String propertyName = PropertyAccessorUtils.canonicalPropertyName(property.getName()); Object propertyValue = target.getPropertyValue(propertyName); if (isSkippedProperty(property)) { continue; } if (!isMutableProperty(property)) { if (this.logger.isDebugEnabled()) { this.logger.debug("Ignoring '" + propertyName + "' due to missing read/write methods"); } continue; } if (defaultValues != null && ObjectUtils.nullSafeEquals(defaultValues.getPropertyValue(propertyName), propertyValue)) { if (this.logger.isDebugEnabled()) { this.logger.debug("Skipping '" + propertyName + "' as property contains default value"); } continue; } // Find a property editor PropertyEditorRegistrySupport propertyEditorRegistrySupport = null; if (target instanceof PropertyEditorRegistrySupport) { propertyEditorRegistrySupport = (PropertyEditorRegistrySupport) target; } PropertyEditor propertyEditor = findEditor(propertyName, propertyEditorRegistrySupport, target.getWrappedInstance(), target.getPropertyType(propertyName), target.getPropertyTypeDescriptor(propertyName)); // Convert and store the value String convertedPropertyValue = convertToStringUsingPropertyEditor(propertyValue, propertyEditor); if (convertedPropertyValue != null) { rtn.addPropertyValue(propertyName, convertedPropertyValue); } } this.dataBinder.bind(rtn); BindingResult bindingResult = this.dataBinder.getBindingResult(); if (bindingResult.hasErrors()) { throw new IllegalStateException("Unable to reverse bind from target '" + this.dataBinder.getObjectName() + "', the properties '" + rtn + "' will result in binding errors when re-bound " + bindingResult.getAllErrors()); } return rtn; }
From source file:org.springmodules.validation.bean.BeanValidator.java
/** * The heart of this validator. This is a recursive method that validates the given object (object) under the * context of the given object graph root (root). The validation rules to be applied are loaded using the * configured {@link org.springmodules.validation.bean.conf.loader.BeanValidationConfigurationLoader}. All errors are registered with the given {@link Errors} * object under the context of the object graph root. * * @param root The root of the object graph. * @param obj The object to be validated * @param errors The {@link Errors} instance where the validation errors will be registered. * @param validatedObjects keeps track of all the validated objects (to prevent revalidating the same objects when * a circular relationship exists). *//* w w w . j av a 2s. c o m*/ protected void validateObjectGraphConstraints(Object root, Object obj, Errors errors, Set validatedObjects) { // cannot load any validation rules for null values if (obj == null) { return; } // if this object was already validated, the skipping this valiation. if (validatedObjects.contains(obj)) { if (logger.isDebugEnabled()) { logger.debug("Skipping validation of object in path '" + errors.getObjectName() + "' for it was already validated"); } return; } if (logger.isDebugEnabled()) { logger.debug("Validating object in path '" + errors.getNestedPath() + "'"); } // loading the bean validation configuration based on the validated object class. Class clazz = obj.getClass(); BeanValidationConfiguration configuration = configurationLoader.loadConfiguration(clazz); if (configuration == null) { return; // no validation configuration for this object, then there's nothing to validate. } // applying all the validation rules for the object and registering the object as "validated" applyBeanValidation(configuration, obj, errors); validatedObjects.add(obj); // after all the validation rules where applied, checking what properties of the object require their own // validation and recursively calling this method on them. CascadeValidation[] cascadeValidations = configuration.getCascadeValidations(); BeanWrapper wrapper = wrapBean(obj); for (int i = 0; i < cascadeValidations.length; i++) { CascadeValidation cascadeValidation = cascadeValidations[i]; Condition applicabilityCondition = cascadeValidation.getApplicabilityCondition(); if (!applicabilityCondition.check(obj)) { continue; } String propertyName = cascadeValidation.getPropertyName(); Class propertyType = wrapper.getPropertyType(propertyName); Object propertyValue = wrapper.getPropertyValue(propertyName); // if the property value is not there nothing to validate. if (propertyValue == null) { continue; } // if the property is an array of a collection, then iterating on it and validating each element. Note that // the error codes that are registered for arrays/collection elements follow the pattern supported by // spring's PropertyAccessor. Also note that just before each recursive call, the context of the validation // is appropriately adjusted using errors.pushNestedPath(...), and after each call it is being adjusted back // using errors.popNestedPath(). if (propertyType.isArray()) { validateArrayProperty(root, propertyValue, propertyName, errors, validatedObjects); } else if (List.class.isAssignableFrom(propertyType) || Set.class.isAssignableFrom(propertyType)) { validateListOrSetProperty(root, (Collection) propertyValue, propertyName, errors, validatedObjects); } else if (Map.class.isAssignableFrom(propertyType)) { validateMapProperty(root, ((Map) propertyValue), propertyName, errors, validatedObjects); } else { // if the object is just a normal object (not an array or a collection), then applying its // validation rules. validatedSubBean(root, propertyValue, propertyName, errors, validatedObjects); } } }