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 + "]");
}
}
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;
}
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;
}
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();
}
}
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);
}
Aggregations