List of usage examples for org.springframework.beans BeanWrapper isReadableProperty
boolean isReadableProperty(String propertyName);
From source file:net.sf.juffrou.reflect.spring.BeanWrapperTests.java
@Test public void testReadableAndWritableForIndexedProperties() { BeanWrapper bw = new JuffrouSpringBeanWrapper(IndexedTestBean.class); assertTrue(bw.isReadableProperty("array")); assertTrue(bw.isReadableProperty("list")); assertTrue(bw.isReadableProperty("set")); assertTrue(bw.isReadableProperty("map")); assertFalse(bw.isReadableProperty("xxx")); assertTrue(bw.isWritableProperty("array")); assertTrue(bw.isWritableProperty("list")); assertTrue(bw.isWritableProperty("set")); assertTrue(bw.isWritableProperty("map")); assertFalse(bw.isWritableProperty("xxx")); assertTrue(bw.isReadableProperty("array[0]")); assertTrue(bw.isReadableProperty("array[0].name")); assertTrue(bw.isReadableProperty("list[0]")); assertTrue(bw.isReadableProperty("list[0].name")); assertTrue(bw.isReadableProperty("set[0]")); assertTrue(bw.isReadableProperty("set[0].name")); assertTrue(bw.isReadableProperty("map[key1]")); assertTrue(bw.isReadableProperty("map[key1].name")); assertTrue(bw.isReadableProperty("map[key4][0]")); assertTrue(bw.isReadableProperty("map[key4][0].name")); assertTrue(bw.isReadableProperty("map[key4][1]")); assertTrue(bw.isReadableProperty("map[key4][1].name")); assertFalse(bw.isReadableProperty("array[key1]")); assertTrue(bw.isWritableProperty("array[0]")); assertTrue(bw.isWritableProperty("array[0].name")); assertTrue(bw.isWritableProperty("list[0]")); assertTrue(bw.isWritableProperty("list[0].name")); assertTrue(bw.isWritableProperty("set[0]")); assertTrue(bw.isWritableProperty("set[0].name")); assertTrue(bw.isWritableProperty("map[key1]")); assertTrue(bw.isWritableProperty("map[key1].name")); assertTrue(bw.isWritableProperty("map[key4][0]")); assertTrue(bw.isWritableProperty("map[key4][0].name")); assertTrue(bw.isWritableProperty("map[key4][1]")); assertTrue(bw.isWritableProperty("map[key4][1].name")); assertFalse(bw.isWritableProperty("array[key1]")); }
From source file:grails.util.GrailsClassUtils.java
public static Object getPropertyOrStaticPropertyOrFieldValue(BeanWrapper ref, Object obj, String name) { if (ref.isReadableProperty(name)) { return ref.getPropertyValue(name); }/*from w ww . j a v a2 s . c o m*/ // Look for public fields if (isPublicField(obj, name)) { return getFieldValue(obj, name); } // Look for statics Class<?> clazz = obj.getClass(); if (isStaticProperty(clazz, name)) { return getStaticPropertyValue(clazz, name); } return null; }
From source file:org.codehaus.groovy.grails.commons.GrailsClassUtils.java
/** * <p>Looks for a property of the reference instance with a given name.</p> * <p>If found its value is returned. We follow the Java bean conventions with augmentation for groovy support * and static fields/properties. We will therefore match, in this order: * </p>/*from w w w.jav a 2s. c om*/ * <ol> * <li>Standard public bean property (with getter or just public field, using normal introspection) * <li>Public static property with getter method * <li>Public static field * </ol> * * @return property value or null if no property found */ public static Object getPropertyOrStaticPropertyOrFieldValue(Object obj, String name) throws BeansException { BeanWrapper ref = new BeanWrapperImpl(obj); if (ref.isReadableProperty(name)) { return ref.getPropertyValue(name); } // Look for public fields if (isPublicField(obj, name)) { return getFieldValue(obj, name); } // Look for statics Class<?> clazz = obj.getClass(); if (isStaticProperty(clazz, name)) { return getStaticPropertyValue(clazz, name); } return null; }
From source file:org.codehaus.groovy.grails.web.binding.GrailsDataBinder.java
@SuppressWarnings("unchecked") private Object autoCreatePropertyIfPossible(BeanWrapper wrapper, String propertyName, Object propertyValue) { propertyName = PropertyAccessorUtils.canonicalPropertyName(propertyName); int currentKeyStart = propertyName.indexOf(PropertyAccessor.PROPERTY_KEY_PREFIX_CHAR); int currentKeyEnd = propertyName.indexOf(PropertyAccessor.PROPERTY_KEY_SUFFIX_CHAR); String propertyNameWithIndex = propertyName; if (currentKeyStart > -1) { propertyName = propertyName.substring(0, currentKeyStart); }/*from ww w .j a va 2s . co m*/ Class<?> type = wrapper.getPropertyType(propertyName); Object val = wrapper.isReadableProperty(propertyName) ? wrapper.getPropertyValue(propertyName) : null; LOG.debug( "Checking if auto-create is possible for property [" + propertyName + "] and type [" + type + "]"); if (type != null && val == null && (isDomainClass(type) || isEmbedded(wrapper, propertyName))) { if (!shouldPropertyValueSkipAutoCreate(propertyValue) && isNullAndWritableProperty(wrapper, propertyName)) { if (isDomainClass(type)) { Object created = autoInstantiateDomainInstance(type); if (created != null) { val = created; wrapper.setPropertyValue(propertyName, created); } } else if (isEmbedded(wrapper, propertyName)) { Object created = autoInstantiateEmbeddedInstance(type); if (created != null) { val = created; wrapper.setPropertyValue(propertyName, created); } } } } else { final Object beanInstance = wrapper.getWrappedInstance(); if (type != null && Collection.class.isAssignableFrom(type)) { Collection<?> c = null; final Class<?> referencedType = getReferencedTypeForCollection(propertyName, beanInstance); if (isNullAndWritableProperty(wrapper, propertyName)) { c = decorateCollectionForDomainAssociation(GrailsClassUtils.createConcreteCollection(type), referencedType); } else { if (wrapper.isReadableProperty(propertyName)) { c = decorateCollectionForDomainAssociation( (Collection<?>) wrapper.getPropertyValue(propertyName), referencedType); } } if (wrapper.isWritableProperty(propertyName) && c != null) { wrapper.setPropertyValue(propertyName, c); } val = c; if (c != null && currentKeyStart > -1 && currentKeyEnd > -1) { String indexString = propertyNameWithIndex.substring(currentKeyStart + 1, currentKeyEnd); int index = Integer.parseInt(indexString); // See if we have an instance in the collection. If so, that specific instance // is the value to return for this indexed property. Object instance = findIndexedValue(c, index); if (instance != null) { val = instance; } // If no value in the collection, this might be a domain class else if (isDomainClass(referencedType)) { instance = autoInstantiateDomainInstance(referencedType); if (instance != null) { val = instance; if (index == c.size()) { addAssociationToTarget(propertyName, beanInstance, instance); } else if (index > c.size()) { while (index > c.size()) { addAssociationToTarget(propertyName, beanInstance, autoInstantiateDomainInstance(referencedType)); } addAssociationToTarget(propertyName, beanInstance, instance); } } } } } else if (type != null && Map.class.isAssignableFrom(type)) { Map<String, Object> map; if (isNullAndWritableProperty(wrapper, propertyName)) { map = new HashMap<String, Object>(); wrapper.setPropertyValue(propertyName, map); } else { map = (Map) wrapper.getPropertyValue(propertyName); } val = map; wrapper.setPropertyValue(propertyName, val); if (currentKeyStart > -1 && currentKeyEnd > -1) { String indexString = propertyNameWithIndex.substring(currentKeyStart + 1, currentKeyEnd); Class<?> referencedType = getReferencedTypeForCollection(propertyName, beanInstance); if (isDomainClass(referencedType)) { final Object domainInstance = autoInstantiateDomainInstance(referencedType); val = domainInstance; map.put(indexString, domainInstance); } } } } return val; }
From source file:org.springframework.beans.BeanWrapperTests.java
@Test public void testReadableAndWritableForIndexedProperties() { BeanWrapper bw = new BeanWrapperImpl(IndexedTestBean.class); assertTrue(bw.isReadableProperty("array")); assertTrue(bw.isReadableProperty("list")); assertTrue(bw.isReadableProperty("set")); assertTrue(bw.isReadableProperty("map")); assertFalse(bw.isReadableProperty("xxx")); assertTrue(bw.isWritableProperty("array")); assertTrue(bw.isWritableProperty("list")); assertTrue(bw.isWritableProperty("set")); assertTrue(bw.isWritableProperty("map")); assertFalse(bw.isWritableProperty("xxx")); assertTrue(bw.isReadableProperty("array[0]")); assertTrue(bw.isReadableProperty("array[0].name")); assertTrue(bw.isReadableProperty("list[0]")); assertTrue(bw.isReadableProperty("list[0].name")); assertTrue(bw.isReadableProperty("set[0]")); assertTrue(bw.isReadableProperty("set[0].name")); assertTrue(bw.isReadableProperty("map[key1]")); assertTrue(bw.isReadableProperty("map[key1].name")); assertTrue(bw.isReadableProperty("map[key4][0]")); assertTrue(bw.isReadableProperty("map[key4][0].name")); assertTrue(bw.isReadableProperty("map[key4][1]")); assertTrue(bw.isReadableProperty("map[key4][1].name")); assertFalse(bw.isReadableProperty("array[key1]")); assertTrue(bw.isWritableProperty("array[0]")); assertTrue(bw.isWritableProperty("array[0].name")); assertTrue(bw.isWritableProperty("list[0]")); assertTrue(bw.isWritableProperty("list[0].name")); assertTrue(bw.isWritableProperty("set[0]")); assertTrue(bw.isWritableProperty("set[0].name")); assertTrue(bw.isWritableProperty("map[key1]")); assertTrue(bw.isWritableProperty("map[key1].name")); assertTrue(bw.isWritableProperty("map[key4][0]")); assertTrue(bw.isWritableProperty("map[key4][0].name")); assertTrue(bw.isWritableProperty("map[key4][1]")); assertTrue(bw.isWritableProperty("map[key4][1].name")); assertFalse(bw.isWritableProperty("array[key1]")); }
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;/*ww w. j a va 2 s . c om*/ 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() + "\""); }// w ww .j a va 2 s .com 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; }