Search in sources :

Example 56 with BeanWrapperImpl

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();
}
Also used : JSONWriter(org.grails.web.json.JSONWriter) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) GrailsDomainClass(grails.core.GrailsDomainClass) GrailsDomainClassProperty(grails.core.GrailsDomainClassProperty) IncludeExcludeSupport(org.grails.core.util.IncludeExcludeSupport) BeanWrapper(org.springframework.beans.BeanWrapper) GroovyObject(groovy.lang.GroovyObject)

Example 57 with BeanWrapperImpl

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());
}
Also used : Errors(org.springframework.validation.Errors) BeanWrapper(org.springframework.beans.BeanWrapper) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) BindException(org.springframework.validation.BindException) TestClass(grails.validation.TestClass)

Example 58 with BeanWrapperImpl

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);
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) ArrayList(java.util.ArrayList) PropertyValue(org.springframework.beans.PropertyValue) HashSet(java.util.HashSet)

Example 59 with BeanWrapperImpl

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);
    }
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) JedisPoolConfig(redis.clients.jedis.JedisPoolConfig)

Example 60 with BeanWrapperImpl

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);
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) PoolConfig(org.springframework.cloud.service.PooledServiceConnectorConfig.PoolConfig)

Aggregations

BeanWrapperImpl (org.springframework.beans.BeanWrapperImpl)113 BeanWrapper (org.springframework.beans.BeanWrapper)75 Test (org.junit.jupiter.api.Test)32 NumberTestBean (org.springframework.beans.testfixture.beans.NumberTestBean)21 BooleanTestBean (org.springframework.beans.testfixture.beans.BooleanTestBean)20 Test (org.junit.Test)19 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)17 IndexedTestBean (org.springframework.beans.testfixture.beans.IndexedTestBean)17 TestBean (org.springframework.beans.testfixture.beans.TestBean)17 PropertyDescriptor (java.beans.PropertyDescriptor)14 PropertyEditorSupport (java.beans.PropertyEditorSupport)14 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)14 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)12 HashSet (java.util.HashSet)11 BeansException (org.springframework.beans.BeansException)9 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 BeanCreationException (org.springframework.beans.factory.BeanCreationException)5 ConstructorArgumentValues (org.springframework.beans.factory.config.ConstructorArgumentValues)5 IOException (java.io.IOException)4