Search in sources :

Example 11 with BeanWrapper

use of org.springframework.beans.BeanWrapper in project grails-core by grails.

the class ScaleConstraint method processValidate.

/**
     * {@inheritDoc}
     * @see org.grails.validation.AbstractConstraint#processValidate(
     *     java.lang.Object, java.lang.Object, org.springframework.validation.Errors)
     */
@Override
protected void processValidate(Object target, Object propertyValue, Errors errors) {
    BigDecimal bigDecimal;
    BeanWrapper bean = new BeanWrapperImpl(target);
    if (propertyValue instanceof Float) {
        bigDecimal = new BigDecimal(propertyValue.toString());
        bigDecimal = getScaledValue(bigDecimal);
        bean.setPropertyValue(getPropertyName(), bigDecimal.floatValue());
    } else if (propertyValue instanceof Double) {
        bigDecimal = new BigDecimal(propertyValue.toString());
        bigDecimal = getScaledValue(bigDecimal);
        bean.setPropertyValue(getPropertyName(), bigDecimal.doubleValue());
    } else if (propertyValue instanceof BigDecimal) {
        bigDecimal = (BigDecimal) propertyValue;
        bigDecimal = getScaledValue(bigDecimal);
        bean.setPropertyValue(getPropertyName(), bigDecimal);
    } else {
        throw new IllegalArgumentException("Unsupported type detected in constraint [" + getName() + "] of property [" + constraintPropertyName + "] of class [" + constraintOwningClass + "]");
    }
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) BigDecimal(java.math.BigDecimal)

Example 12 with BeanWrapper

use of org.springframework.beans.BeanWrapper in project grails-core by grails.

the class AbstractConstraintTests method validateConstraint.

protected Errors validateConstraint(Constraint constraint, Object value, Boolean shouldVeto) {
    BeanWrapper constrainedBean = new BeanWrapperImpl(new TestClass());
    constrainedBean.setPropertyValue(constraint.getPropertyName(), value);
    Errors errors = new BindException(constrainedBean.getWrappedInstance(), constrainedBean.getWrappedClass().getName());
    if (!(constraint instanceof VetoingConstraint) || shouldVeto == null) {
        constraint.validate(constrainedBean.getWrappedInstance(), value, errors);
    } else {
        boolean vetoed = ((VetoingConstraint) constraint).validateWithVetoing(constrainedBean.getWrappedInstance(), value, errors);
        if (shouldVeto.booleanValue() && !vetoed)
            fail("Constraint should veto");
        else if (!shouldVeto.booleanValue() && vetoed)
            fail("Constraint shouldn't veto");
    }
    return errors;
}
Also used : Errors(org.springframework.validation.Errors) BeanWrapper(org.springframework.beans.BeanWrapper) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) BindException(org.springframework.validation.BindException)

Example 13 with BeanWrapper

use of org.springframework.beans.BeanWrapper in project grails-core by grails.

the class ConverterUtil method createBeanWrapper.

public static BeanWrapper createBeanWrapper(Object o) {
    BeanWrapper beanWrapper;
    try {
        Class<?> c = Class.forName(PERSISTENCE_BEAN_WRAPPER_CLASS, true, Thread.currentThread().getContextClassLoader());
        Constructor<?> init = c.getConstructor(new Class[] { Object.class });
        beanWrapper = (BeanWrapper) init.newInstance(new Object[] { o });
    } catch (Exception e) {
        beanWrapper = new BeanWrapperImpl(o);
    }
    return beanWrapper;
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) ConverterException(org.grails.web.converters.exceptions.ConverterException)

Example 14 with BeanWrapper

use of org.springframework.beans.BeanWrapper in project grails-core by grails.

the class DomainClassMarshaller method marshalObject.

@SuppressWarnings({ "unchecked", "rawtypes" })
public void marshalObject(Object value, XML xml) throws ConverterException {
    Class clazz = value.getClass();
    List<String> excludes = xml.getExcludes(clazz);
    List<String> includes = xml.getIncludes(clazz);
    IncludeExcludeSupport<String> includeExcludeSupport = new IncludeExcludeSupport<String>();
    GrailsDomainClass domainClass = (GrailsDomainClass) application.getArtefact(DomainClassArtefactHandler.TYPE, ConverterUtil.trimProxySuffix(clazz.getName()));
    BeanWrapper beanWrapper = new BeanWrapperImpl(value);
    GrailsDomainClassProperty id = domainClass.getIdentifier();
    if (shouldInclude(includeExcludeSupport, includes, excludes, value, id.getName())) {
        Object idValue = beanWrapper.getPropertyValue(id.getName());
        if (idValue != null)
            xml.attribute("id", String.valueOf(idValue));
    }
    if (shouldInclude(includeExcludeSupport, includes, excludes, value, GrailsDomainClassProperty.VERSION) && includeVersion) {
        Object versionValue = beanWrapper.getPropertyValue(domainClass.getVersion().getName());
        xml.attribute("version", String.valueOf(versionValue));
    }
    GrailsDomainClassProperty[] properties = domainClass.getPersistentProperties();
    for (GrailsDomainClassProperty property : properties) {
        String propertyName = property.getName();
        if (!shouldInclude(includeExcludeSupport, includes, excludes, value, property.getName()))
            continue;
        xml.startNode(propertyName);
        if (!property.isAssociation()) {
            // Write non-relation property
            Object val = beanWrapper.getPropertyValue(propertyName);
            xml.convertAnother(val);
        } else {
            if (isRenderDomainClassRelations()) {
                Object referenceObject = beanWrapper.getPropertyValue(propertyName);
                if (referenceObject != null && shouldInitializeProxy(referenceObject)) {
                    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);
                    }
                    xml.convertAnother(referenceObject);
                }
            } else {
                Object referenceObject = beanWrapper.getPropertyValue(propertyName);
                if (referenceObject != null) {
                    GrailsDomainClass referencedDomainClass = property.getReferencedDomainClass();
                    // Embedded are now always fully rendered
                    if (referencedDomainClass == null || property.isEmbedded() || property.getType().isEnum()) {
                        xml.convertAnother(referenceObject);
                    } else if (property.isOneToOne() || property.isManyToOne() || property.isEmbedded()) {
                        asShortObject(referenceObject, xml, referencedDomainClass.getIdentifier(), referencedDomainClass);
                    } else {
                        GrailsDomainClassProperty referencedIdProperty = referencedDomainClass.getIdentifier();
                        @SuppressWarnings("unused") String refPropertyName = referencedDomainClass.getPropertyName();
                        if (referenceObject instanceof Collection) {
                            Collection o = (Collection) referenceObject;
                            for (Object el : o) {
                                xml.startNode(xml.getElementName(el));
                                asShortObject(el, xml, referencedIdProperty, referencedDomainClass);
                                xml.end();
                            }
                        } 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();
                                xml.startNode("entry").attribute("key", key);
                                asShortObject(o, xml, referencedIdProperty, referencedDomainClass);
                                xml.end();
                            }
                        }
                    }
                }
            }
        }
        xml.end();
    }
}
Also used : BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) GrailsDomainClass(grails.core.GrailsDomainClass) GrailsDomainClassProperty(grails.core.GrailsDomainClassProperty) IncludeExcludeSupport(org.grails.core.util.IncludeExcludeSupport) BeanWrapper(org.springframework.beans.BeanWrapper) GrailsDomainClass(grails.core.GrailsDomainClass)

Example 15 with BeanWrapper

use of org.springframework.beans.BeanWrapper in project apollo by ctripcorp.

the class BeanUtils method getNullPropertyNames.

private static String[] getNullPropertyNames(Object source) {
    final BeanWrapper src = new BeanWrapperImpl(source);
    PropertyDescriptor[] pds = src.getPropertyDescriptors();
    Set<String> emptyNames = new HashSet<String>();
    for (PropertyDescriptor pd : pds) {
        Object srcValue = src.getPropertyValue(pd.getName());
        if (srcValue == null)
            emptyNames.add(pd.getName());
    }
    String[] result = new String[emptyNames.size()];
    return emptyNames.toArray(result);
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) PropertyDescriptor(java.beans.PropertyDescriptor) HashSet(java.util.HashSet)

Aggregations

BeanWrapper (org.springframework.beans.BeanWrapper)144 BeanWrapperImpl (org.springframework.beans.BeanWrapperImpl)79 Test (org.junit.jupiter.api.Test)31 NumberTestBean (org.springframework.beans.testfixture.beans.NumberTestBean)21 BooleanTestBean (org.springframework.beans.testfixture.beans.BooleanTestBean)20 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)19 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)18 IndexedTestBean (org.springframework.beans.testfixture.beans.IndexedTestBean)18 TestBean (org.springframework.beans.testfixture.beans.TestBean)18 Consumes (javax.ws.rs.Consumes)16 PUT (javax.ws.rs.PUT)16 Path (javax.ws.rs.Path)16 PropertyEditorSupport (java.beans.PropertyEditorSupport)14 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)14 HashSet (java.util.HashSet)12 BeansException (org.springframework.beans.BeansException)12 PropertyDescriptor (java.beans.PropertyDescriptor)11 OnmsNode (org.opennms.netmgt.model.OnmsNode)9 Test (org.junit.Test)6 BigDecimal (java.math.BigDecimal)5