use of org.springframework.beans.BeanWrapperImpl in project grails-core by grails.
the class DomainClassMarshaller method marshalObject.
@SuppressWarnings({ "unchecked", "rawtypes" })
public void marshalObject(Object value, JSON json) throws ConverterException {
JSONWriter writer = json.getWriter();
value = proxyHandler.unwrapIfProxy(value);
Class<?> clazz = value.getClass();
List<String> excludes = json.getExcludes(clazz);
List<String> includes = json.getIncludes(clazz);
IncludeExcludeSupport<String> includeExcludeSupport = new IncludeExcludeSupport<String>();
GrailsDomainClass domainClass = (GrailsDomainClass) application.getArtefact(DomainClassArtefactHandler.TYPE, ConverterUtil.trimProxySuffix(clazz.getName()));
BeanWrapper beanWrapper = new BeanWrapperImpl(value);
writer.object();
if (includeClass && shouldInclude(includeExcludeSupport, includes, excludes, value, "class")) {
writer.key("class").value(domainClass.getClazz().getName());
}
GrailsDomainClassProperty id = domainClass.getIdentifier();
if (shouldInclude(includeExcludeSupport, includes, excludes, value, id.getName())) {
Object idValue = extractValue(value, id);
if (idValue != null) {
json.property(GrailsDomainClassProperty.IDENTITY, idValue);
}
}
if (shouldInclude(includeExcludeSupport, includes, excludes, value, GrailsDomainClassProperty.VERSION) && isIncludeVersion()) {
GrailsDomainClassProperty versionProperty = domainClass.getVersion();
Object version = extractValue(value, versionProperty);
if (version != null) {
json.property(GrailsDomainClassProperty.VERSION, version);
}
}
GrailsDomainClassProperty[] properties = domainClass.getPersistentProperties();
for (GrailsDomainClassProperty property : properties) {
if (!shouldInclude(includeExcludeSupport, includes, excludes, value, property.getName()))
continue;
writer.key(property.getName());
if (!property.isAssociation()) {
// Write non-relation property
Object val = beanWrapper.getPropertyValue(property.getName());
json.convertAnother(val);
} else {
Object referenceObject = beanWrapper.getPropertyValue(property.getName());
if (isRenderDomainClassRelations()) {
if (referenceObject == null) {
writer.valueNull();
} else {
referenceObject = proxyHandler.unwrapIfProxy(referenceObject);
if (referenceObject instanceof SortedMap) {
referenceObject = new TreeMap((SortedMap) referenceObject);
} else if (referenceObject instanceof SortedSet) {
referenceObject = new TreeSet((SortedSet) referenceObject);
} else if (referenceObject instanceof Set) {
referenceObject = new LinkedHashSet((Set) referenceObject);
} else if (referenceObject instanceof Map) {
referenceObject = new LinkedHashMap((Map) referenceObject);
} else if (referenceObject instanceof Collection) {
referenceObject = new ArrayList((Collection) referenceObject);
}
json.convertAnother(referenceObject);
}
} else {
if (referenceObject == null) {
json.value(null);
} else {
GrailsDomainClass referencedDomainClass = property.getReferencedDomainClass();
// Embedded are now always fully rendered
if (referencedDomainClass == null || property.isEmbedded() || property.getType().isEnum()) {
json.convertAnother(referenceObject);
} else if (property.isOneToOne() || property.isManyToOne() || property.isEmbedded()) {
asShortObject(referenceObject, json, referencedDomainClass.getIdentifier(), referencedDomainClass);
} else {
GrailsDomainClassProperty referencedIdProperty = referencedDomainClass.getIdentifier();
@SuppressWarnings("unused") String refPropertyName = referencedDomainClass.getPropertyName();
if (referenceObject instanceof Collection) {
Collection o = (Collection) referenceObject;
writer.array();
for (Object el : o) {
asShortObject(el, json, referencedIdProperty, referencedDomainClass);
}
writer.endArray();
} else if (referenceObject instanceof Map) {
Map<Object, Object> map = (Map<Object, Object>) referenceObject;
for (Map.Entry<Object, Object> entry : map.entrySet()) {
String key = String.valueOf(entry.getKey());
Object o = entry.getValue();
writer.object();
writer.key(key);
asShortObject(o, json, referencedIdProperty, referencedDomainClass);
writer.endObject();
}
}
}
}
}
}
}
writer.endObject();
}
use of org.springframework.beans.BeanWrapperImpl in project grails-core by grails.
the class ScaleConstraintTests method proceedValidation.
private Object proceedValidation(Constraint constraint, Object value) {
BeanWrapper constrainedBean = new BeanWrapperImpl(new TestClass());
constrainedBean.setPropertyValue(constraint.getPropertyName(), value);
Errors errors = new BindException(constrainedBean.getWrappedInstance(), constrainedBean.getWrappedClass().getName());
assertFalse(errors.hasErrors());
constraint.validate(constrainedBean.getWrappedInstance(), value, errors);
return constrainedBean.getPropertyValue(constraint.getPropertyName());
}
use of org.springframework.beans.BeanWrapperImpl in project spring-boot by spring-projects.
the class RelaxedDataBinder method modifyProperties.
/**
* Modify the property values so that period separated property paths are valid for
* map keys. Also creates new maps for properties of map type that are null (assuming
* all maps are potentially nested). The standard bracket {@code[...]} dereferencing
* is also accepted.
* @param propertyValues the property values
* @param target the target object
* @return modified property values
*/
private MutablePropertyValues modifyProperties(MutablePropertyValues propertyValues, Object target) {
propertyValues = getPropertyValuesForNamePrefix(propertyValues);
if (target instanceof MapHolder) {
propertyValues = addMapPrefix(propertyValues);
}
BeanWrapper wrapper = new BeanWrapperImpl(target);
wrapper.setConversionService(new RelaxedConversionService(getConversionService()));
wrapper.setAutoGrowNestedPaths(true);
List<PropertyValue> sortedValues = new ArrayList<>();
Set<String> modifiedNames = new HashSet<>();
List<String> sortedNames = getSortedPropertyNames(propertyValues);
for (String name : sortedNames) {
PropertyValue propertyValue = propertyValues.getPropertyValue(name);
PropertyValue modifiedProperty = modifyProperty(wrapper, propertyValue);
if (modifiedNames.add(modifiedProperty.getName())) {
sortedValues.add(modifiedProperty);
}
}
return new MutablePropertyValues(sortedValues);
}
use of org.springframework.beans.BeanWrapperImpl in project spring-cloud-connectors by spring-cloud.
the class RedisConnectionFactoryConfigurer method configurePool.
private void configurePool(JedisConnectionFactory connectionFactory, PooledServiceConnectorConfig config) {
if (config.getPoolConfig() != null) {
JedisPoolConfig poolConfig = new JedisPoolConfig();
BeanWrapper target = new BeanWrapperImpl(poolConfig);
BeanWrapper source = new BeanWrapperImpl(config.getPoolConfig());
Util.setCorrespondingProperties(target, source);
connectionFactory.setPoolConfig(poolConfig);
}
}
use of org.springframework.beans.BeanWrapperImpl in project spring-cloud-connectors by spring-cloud.
the class DbcpLikePooledDataSourceCreator method setBasicDataSourceProperties.
protected void setBasicDataSourceProperties(DataSource basicDataSource, RelationalServiceInfo serviceInfo, ServiceConnectorConfig serviceConnectorConfig, String driverClassName, String validationQuery) {
BeanWrapper target = new BeanWrapperImpl(basicDataSource);
target.setPropertyValue("driverClassName", driverClassName);
target.setPropertyValue("url", serviceInfo.getJdbcUrl());
if (validationQuery != null) {
target.setPropertyValue("validationQuery", validationQuery);
target.setPropertyValue("testOnBorrow", true);
}
if (serviceConnectorConfig == null) {
// choose sensible values so that we set max connection pool size to what
// free tier services on Cloud Foundry and Heroku allow
serviceConnectorConfig = new DataSourceConfig(new PoolConfig(4, 30000), null);
}
configurer.configure(basicDataSource, (DataSourceConfig) serviceConnectorConfig);
}
Aggregations