List of usage examples for org.springframework.beans PropertyAccessor PROPERTY_KEY_PREFIX_CHAR
char PROPERTY_KEY_PREFIX_CHAR
To view the source code for org.springframework.beans PropertyAccessor PROPERTY_KEY_PREFIX_CHAR.
Click Source Link
From source file:com.siberhus.tdfl.mapping.BeanWrapLineMapper.java
private String findPropertyName(Object bean, String key) { if (bean == null) { return null; }/*from ww w .j a v a2 s . c o m*/ Class<?> cls = bean.getClass(); int index = PropertyAccessorUtils.getFirstNestedPropertySeparatorIndex(key); String prefix; String suffix; // If the property name is nested recurse down through the properties // looking for a match. if (index > 0) { prefix = key.substring(0, index); suffix = key.substring(index + 1, key.length()); String nestedName = findPropertyName(bean, prefix); if (nestedName == null) { return null; } Object nestedValue = getPropertyValue(bean, nestedName); String nestedPropertyName = findPropertyName(nestedValue, suffix); return nestedPropertyName == null ? null : nestedName + "." + nestedPropertyName; } String name = null; int distance = 0; index = key.indexOf(PropertyAccessor.PROPERTY_KEY_PREFIX_CHAR); if (index > 0) { prefix = key.substring(0, index); suffix = key.substring(index); } else { prefix = key; suffix = ""; } while (name == null && distance <= distanceLimit) { String[] candidates = PropertyMatches.forProperty(prefix, cls, distance).getPossibleMatches(); // If we find precisely one match, then use that one... if (candidates.length == 1) { String candidate = candidates[0]; if (candidate.equals(prefix)) { // if it's the same don't // replace it... name = key; } else { name = candidate + suffix; } } distance++; } return name; }
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 w w w.j a va 2s . com 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.kuali.rice.krad.data.util.ReferenceLinker.java
/** * Gets index of property name.// w ww.j a va 2 s. c o m * * <p> * Returns the index number of the location of the given property name. * </p> * * @param propertyName name of property to find index of. * @return index number representing location of property name. */ private Integer extractIndex(String propertyName) { int firstIndex = propertyName.indexOf(PropertyAccessor.PROPERTY_KEY_PREFIX_CHAR); int lastIndex = propertyName.lastIndexOf(PropertyAccessor.PROPERTY_KEY_SUFFIX_CHAR); if (firstIndex != -1 && lastIndex != -1) { String indexValue = propertyName.substring(firstIndex + 1, lastIndex); try { int index = Integer.parseInt(indexValue); return Integer.valueOf(index); } catch (NumberFormatException e) { // if we encounter this then it wasn't really an index, ignore } } return null; }