type formatters
This commit is contained in:
parent
b2c723a76e
commit
3cd3cddbe0
|
|
@ -1,6 +1,5 @@
|
|||
package org.springframework.ui.binding.support;
|
||||
|
||||
import org.springframework.ui.binding.Binding;
|
||||
import org.springframework.ui.binding.config.Condition;
|
||||
import org.springframework.ui.format.Formatter;
|
||||
|
||||
|
|
@ -18,7 +17,4 @@ public interface BindingRule {
|
|||
|
||||
Condition getVisibleCondition();
|
||||
|
||||
// TODO - does this belong here?
|
||||
Binding getBinding(String property, Object model);
|
||||
|
||||
}
|
||||
|
|
@ -37,4 +37,8 @@ class BindingStatusResult implements BindingResult {
|
|||
return bindingStatusAlert;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return getAlert().toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -15,7 +15,8 @@
|
|||
*/
|
||||
package org.springframework.ui.binding.support;
|
||||
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
import java.beans.PropertyDescriptor;
|
||||
|
||||
import org.springframework.ui.format.AnnotationFormatterFactory;
|
||||
import org.springframework.ui.format.Formatter;
|
||||
|
||||
|
|
@ -28,12 +29,13 @@ import org.springframework.ui.format.Formatter;
|
|||
*/
|
||||
public interface FormatterRegistry {
|
||||
|
||||
Formatter<?> getFormatter(PropertyDescriptor property);
|
||||
|
||||
/**
|
||||
* Get the Formatter for the property type.
|
||||
* @param propertyType the property type descriptor, which provides additional property metadata.
|
||||
* Get the Formatter for the type.
|
||||
* @return the Formatter, or <code>null</code> if none is registered
|
||||
*/
|
||||
Formatter<?> getFormatter(TypeDescriptor<?> propertyType);
|
||||
Formatter<?> getFormatter(Class<?> type);
|
||||
|
||||
/**
|
||||
* Adds a Formatter that will format the values of properties of the provided type.
|
||||
|
|
|
|||
|
|
@ -15,11 +15,17 @@
|
|||
*/
|
||||
package org.springframework.ui.binding.support;
|
||||
|
||||
import java.beans.BeanInfo;
|
||||
import java.beans.IntrospectionException;
|
||||
import java.beans.Introspector;
|
||||
import java.beans.PropertyDescriptor;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.context.MessageSource;
|
||||
import org.springframework.core.GenericCollectionTypeResolver;
|
||||
import org.springframework.core.convert.TypeConverter;
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
import org.springframework.core.convert.support.DefaultTypeConverter;
|
||||
import org.springframework.ui.binding.Binder;
|
||||
import org.springframework.ui.binding.Binding;
|
||||
|
|
@ -45,6 +51,8 @@ public class GenericBinder implements Binder {
|
|||
|
||||
private Map<String, GenericBindingRule> bindingRules;
|
||||
|
||||
private FormatterRegistry formatterRegistry;
|
||||
|
||||
private TypeConverter typeConverter;
|
||||
|
||||
private MessageSource messageSource;
|
||||
|
|
@ -57,9 +65,20 @@ public class GenericBinder implements Binder {
|
|||
Assert.notNull(model, "The model to bind to is required");
|
||||
this.model = model;
|
||||
bindingRules = new HashMap<String, GenericBindingRule>();
|
||||
formatterRegistry = new GenericFormatterRegistry();
|
||||
typeConverter = new DefaultTypeConverter();
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures the registry of Formatters to query when no explicit Formatter has been registered for a Binding.
|
||||
* Allows Formatters to be applied by property type and by property annotation.
|
||||
* @param registry the formatter registry
|
||||
*/
|
||||
public void setFormatterRegistry(FormatterRegistry formatterRegistry) {
|
||||
Assert.notNull(formatterRegistry, "The FormatterRegistry is required");
|
||||
this.formatterRegistry = formatterRegistry;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure the MessageSource that resolves localized {@link BindingResult} alert messages.
|
||||
* @param messageSource the message source
|
||||
|
|
@ -148,7 +167,7 @@ public class GenericBinder implements Binder {
|
|||
private GenericBindingRule getBindingRule(String property) {
|
||||
GenericBindingRule rule = bindingRules.get(property);
|
||||
if (rule == null) {
|
||||
rule = new GenericBindingRule(property);
|
||||
rule = new GenericBindingRule(property, model.getClass());
|
||||
bindingRules.put(property, rule);
|
||||
}
|
||||
return rule;
|
||||
|
|
@ -169,15 +188,17 @@ public class GenericBinder implements Binder {
|
|||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
class GenericBindingRule implements BindingRule, BindingRuleConfiguration {
|
||||
class GenericBindingRule implements BindingRuleConfiguration, BindingContext {
|
||||
|
||||
private String property;
|
||||
private Class<?> modelClass;
|
||||
|
||||
private Formatter formatter = DefaultFormatter.INSTANCE;
|
||||
private PropertyDescriptor property;
|
||||
|
||||
private Formatter elementFormatter = DefaultFormatter.INSTANCE;
|
||||
private Formatter formatter;
|
||||
|
||||
private Formatter keyFormatter = DefaultFormatter.INSTANCE;
|
||||
private Formatter elementFormatter;
|
||||
|
||||
private Formatter keyFormatter;
|
||||
|
||||
private Condition editableCondition = Condition.ALWAYS_TRUE;
|
||||
|
||||
|
|
@ -189,26 +210,39 @@ public class GenericBinder implements Binder {
|
|||
|
||||
private Binding binding;
|
||||
|
||||
public GenericBindingRule(String property) {
|
||||
this.property = property;
|
||||
public GenericBindingRule(String property, Class modelClass) {
|
||||
this.modelClass = modelClass;
|
||||
this.property = findPropertyDescriptor(property);
|
||||
}
|
||||
|
||||
// implementing BindingRule
|
||||
// implementing BindingContext
|
||||
|
||||
public Binding getBinding(String property, Object model) {
|
||||
return getBindingRule(property).getBinding(model);
|
||||
public TypeConverter getTypeConverter() {
|
||||
return typeConverter;
|
||||
}
|
||||
|
||||
public Formatter<?> getFormatter() {
|
||||
if (formatter != null) {
|
||||
return formatter;
|
||||
} else {
|
||||
return formatterRegistry.getFormatter(property);
|
||||
}
|
||||
}
|
||||
|
||||
public Formatter<?> getElementFormatter() {
|
||||
return elementFormatter;
|
||||
if (elementFormatter != null) {
|
||||
return formatter;
|
||||
} else {
|
||||
return formatterRegistry.getFormatter(getElementType());
|
||||
}
|
||||
}
|
||||
|
||||
public Formatter<?> getKeyFormatter() {
|
||||
if (keyFormatter != null) {
|
||||
return keyFormatter;
|
||||
} else {
|
||||
return formatterRegistry.getFormatter(getKeyType());
|
||||
}
|
||||
}
|
||||
|
||||
public Condition getEnabledCondition() {
|
||||
|
|
@ -223,6 +257,10 @@ public class GenericBinder implements Binder {
|
|||
return visibleCondition;
|
||||
}
|
||||
|
||||
public Binding getBinding(String property, Object model) {
|
||||
return getBindingRule(property).getBinding(model);
|
||||
}
|
||||
|
||||
// implementing BindingRuleConfiguration
|
||||
|
||||
public BindingRuleConfiguration formatWith(Formatter<?> formatter) {
|
||||
|
|
@ -255,10 +293,20 @@ public class GenericBinder implements Binder {
|
|||
return this;
|
||||
}
|
||||
|
||||
// internal helpers
|
||||
|
||||
private Class<?> getElementType() {
|
||||
return GenericCollectionTypeResolver.getCollectionReturnType(property.getReadMethod());
|
||||
}
|
||||
|
||||
private Class<?> getKeyType() {
|
||||
return GenericCollectionTypeResolver.getMapKeyReturnType(property.getReadMethod());
|
||||
}
|
||||
|
||||
GenericBindingRule getBindingRule(String property) {
|
||||
GenericBindingRule rule = nestedBindingRules.get(property);
|
||||
if (rule == null) {
|
||||
rule = new GenericBindingRule(property);
|
||||
rule = new GenericBindingRule(property, this.property.getPropertyType());
|
||||
nestedBindingRules.put(property, rule);
|
||||
}
|
||||
return rule;
|
||||
|
|
@ -266,11 +314,52 @@ public class GenericBinder implements Binder {
|
|||
|
||||
Binding getBinding(Object model) {
|
||||
if (binding == null) {
|
||||
binding = new PropertyBinding(property, model, typeConverter, this);
|
||||
binding = new PropertyBinding(property, model, this);
|
||||
}
|
||||
return binding;
|
||||
}
|
||||
|
||||
private PropertyDescriptor findPropertyDescriptor(String property) {
|
||||
PropertyDescriptor[] propDescs = getBeanInfo(modelClass).getPropertyDescriptors();
|
||||
for (PropertyDescriptor propDesc : propDescs) {
|
||||
if (propDesc.getName().equals(property)) {
|
||||
return propDesc;
|
||||
}
|
||||
}
|
||||
throw new IllegalArgumentException("No property '" + property + "' found on model ["
|
||||
+ modelClass.getName() + "]");
|
||||
}
|
||||
|
||||
private BeanInfo getBeanInfo(Class<?> clazz) {
|
||||
try {
|
||||
return Introspector.getBeanInfo(clazz);
|
||||
} catch (IntrospectionException e) {
|
||||
throw new IllegalStateException("Unable to introspect model type " + clazz);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public interface BindingContext {
|
||||
|
||||
TypeConverter getTypeConverter();
|
||||
|
||||
Condition getEditableCondition();
|
||||
|
||||
Condition getEnabledCondition();
|
||||
|
||||
Condition getVisibleCondition();
|
||||
|
||||
Binding getBinding(String property, Object model);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
Formatter getFormatter();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
Formatter getElementFormatter();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
Formatter getKeyFormatter();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@
|
|||
*/
|
||||
package org.springframework.ui.binding.support;
|
||||
|
||||
import java.beans.PropertyDescriptor;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
|
|
@ -24,6 +25,7 @@ import java.util.Map;
|
|||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import org.springframework.core.GenericTypeResolver;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
import org.springframework.ui.format.AnnotationFormatterFactory;
|
||||
|
|
@ -46,7 +48,8 @@ public class GenericFormatterRegistry implements FormatterRegistry {
|
|||
|
||||
private Map<Class, AnnotationFormatterFactory> annotationFormatters = new HashMap<Class, AnnotationFormatterFactory>();
|
||||
|
||||
public Formatter<?> getFormatter(TypeDescriptor<?> propertyType) {
|
||||
public Formatter<?> getFormatter(PropertyDescriptor property) {
|
||||
TypeDescriptor<?> propertyType = new TypeDescriptor(new MethodParameter(property.getReadMethod(), -1));
|
||||
Annotation[] annotations = propertyType.getAnnotations();
|
||||
for (Annotation a : annotations) {
|
||||
AnnotationFormatterFactory factory = annotationFormatters.get(a.annotationType());
|
||||
|
|
@ -66,7 +69,11 @@ public class GenericFormatterRegistry implements FormatterRegistry {
|
|||
} else {
|
||||
type = propertyType.getType();
|
||||
}
|
||||
formatter = typeFormatters.get(type);
|
||||
return getFormatter(type);
|
||||
}
|
||||
|
||||
public Formatter<?> getFormatter(Class<?> type) {
|
||||
Formatter formatter = typeFormatters.get(type);
|
||||
if (formatter != null) {
|
||||
return formatter;
|
||||
} else {
|
||||
|
|
@ -84,11 +91,12 @@ public class GenericFormatterRegistry implements FormatterRegistry {
|
|||
typeFormatters.put(type, formatter);
|
||||
return formatter;
|
||||
} else {
|
||||
return null;
|
||||
return DefaultFormatter.INSTANCE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void add(Class<?> propertyType, Formatter<?> formatter) {
|
||||
if (propertyType.isAnnotation()) {
|
||||
annotationFormatters.put(propertyType, new SimpleAnnotationFormatterFactory(formatter));
|
||||
|
|
|
|||
|
|
@ -3,9 +3,6 @@
|
|||
*/
|
||||
package org.springframework.ui.binding.support;
|
||||
|
||||
import java.beans.BeanInfo;
|
||||
import java.beans.IntrospectionException;
|
||||
import java.beans.Introspector;
|
||||
import java.beans.PropertyDescriptor;
|
||||
import java.lang.reflect.Array;
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
|
|
@ -26,21 +23,18 @@ import org.springframework.core.convert.TypeDescriptor;
|
|||
import org.springframework.ui.alert.Alert;
|
||||
import org.springframework.ui.alert.Severity;
|
||||
import org.springframework.ui.binding.Binding;
|
||||
import org.springframework.ui.binding.support.GenericBinder.BindingContext;
|
||||
import org.springframework.ui.format.Formatter;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public class PropertyBinding implements Binding {
|
||||
|
||||
private String property;
|
||||
private PropertyDescriptor property;
|
||||
|
||||
private Object model;
|
||||
private Object object;
|
||||
|
||||
private TypeConverter typeConverter;
|
||||
|
||||
private BindingRule bindingRule;
|
||||
|
||||
private PropertyDescriptor propertyDescriptor;
|
||||
private BindingContext bindingContext;
|
||||
|
||||
private Object sourceValue;
|
||||
|
||||
|
|
@ -51,11 +45,10 @@ public class PropertyBinding implements Binding {
|
|||
|
||||
private BindingStatus status;
|
||||
|
||||
public PropertyBinding(String property, Object model, TypeConverter typeConverter, BindingRule bindingRule) {
|
||||
initProperty(property, model);
|
||||
this.model = model;
|
||||
this.typeConverter = typeConverter;
|
||||
this.bindingRule = bindingRule;
|
||||
public PropertyBinding(PropertyDescriptor property, Object object, BindingContext bindingContext) {
|
||||
this.property = property;
|
||||
this.object = object;
|
||||
this.bindingContext = bindingContext;
|
||||
this.buffer = new ValueBuffer(getModel());
|
||||
status = BindingStatus.CLEAN;
|
||||
}
|
||||
|
|
@ -71,15 +64,15 @@ public class PropertyBinding implements Binding {
|
|||
}
|
||||
|
||||
public boolean isEditable() {
|
||||
return isWriteableProperty() && bindingRule.getEditableCondition().isTrue();
|
||||
return isWriteableProperty() && bindingContext.getEditableCondition().isTrue();
|
||||
}
|
||||
|
||||
public boolean isEnabled() {
|
||||
return bindingRule.getEnabledCondition().isTrue();
|
||||
return bindingContext.getEnabledCondition().isTrue();
|
||||
}
|
||||
|
||||
public boolean isVisible() {
|
||||
return bindingRule.getVisibleCondition().isTrue();
|
||||
return bindingContext.getVisibleCondition().isTrue();
|
||||
}
|
||||
|
||||
public void applySourceValue(Object sourceValue) {
|
||||
|
|
@ -87,7 +80,7 @@ public class PropertyBinding implements Binding {
|
|||
assertEnabled();
|
||||
if (sourceValue instanceof String) {
|
||||
try {
|
||||
buffer.setValue(bindingRule.getFormatter().parse((String) sourceValue, getLocale()));
|
||||
buffer.setValue(bindingContext.getFormatter().parse((String) sourceValue, getLocale()));
|
||||
sourceValue = null;
|
||||
status = BindingStatus.DIRTY;
|
||||
} catch (ParseException e) {
|
||||
|
|
@ -97,7 +90,7 @@ public class PropertyBinding implements Binding {
|
|||
}
|
||||
} else if (sourceValue instanceof String[]) {
|
||||
String[] sourceValues = (String[]) sourceValue;
|
||||
Class<?> parsedType = getFormattedObjectType(bindingRule.getElementFormatter().getClass());
|
||||
Class<?> parsedType = getFormattedObjectType(bindingContext.getElementFormatter().getClass());
|
||||
if (parsedType == null) {
|
||||
parsedType = String.class;
|
||||
}
|
||||
|
|
@ -105,7 +98,8 @@ public class PropertyBinding implements Binding {
|
|||
for (int i = 0; i < sourceValues.length; i++) {
|
||||
Object parsedValue;
|
||||
try {
|
||||
parsedValue = bindingRule.getElementFormatter().parse(sourceValues[i], LocaleContextHolder.getLocale());
|
||||
parsedValue = bindingContext.getElementFormatter().parse(sourceValues[i],
|
||||
LocaleContextHolder.getLocale());
|
||||
Array.set(parsed, i, parsedValue);
|
||||
} catch (ParseException e) {
|
||||
this.sourceValue = sourceValue;
|
||||
|
|
@ -128,7 +122,7 @@ public class PropertyBinding implements Binding {
|
|||
|
||||
public Alert getStatusAlert() {
|
||||
if (status == BindingStatus.INVALID_SOURCE_VALUE) {
|
||||
return new Alert() {
|
||||
return new AbstractAlert() {
|
||||
public String getCode() {
|
||||
return "typeMismatch";
|
||||
}
|
||||
|
|
@ -143,7 +137,7 @@ public class PropertyBinding implements Binding {
|
|||
};
|
||||
} else if (status == BindingStatus.COMMIT_FAILURE) {
|
||||
if (buffer.getFlushException() instanceof ConversionFailedException) {
|
||||
return new Alert() {
|
||||
return new AbstractAlert() {
|
||||
public String getCode() {
|
||||
return "typeMismatch";
|
||||
}
|
||||
|
|
@ -157,13 +151,13 @@ public class PropertyBinding implements Binding {
|
|||
}
|
||||
};
|
||||
} else {
|
||||
return new Alert() {
|
||||
return new AbstractAlert() {
|
||||
public String getCode() {
|
||||
return "internalError";
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return "Internal error occurred";
|
||||
return "Internal error occurred " + buffer.getFlushException();
|
||||
}
|
||||
|
||||
public Severity getSeverity() {
|
||||
|
|
@ -172,7 +166,7 @@ public class PropertyBinding implements Binding {
|
|||
};
|
||||
}
|
||||
} else if (status == BindingStatus.COMMITTED) {
|
||||
return new Alert() {
|
||||
return new AbstractAlert() {
|
||||
public String getCode() {
|
||||
return "bindSucces";
|
||||
}
|
||||
|
|
@ -221,19 +215,20 @@ public class PropertyBinding implements Binding {
|
|||
public Model getModel() {
|
||||
return new Model() {
|
||||
public Object getValue() {
|
||||
return ReflectionUtils.invokeMethod(propertyDescriptor.getReadMethod(), model);
|
||||
return ReflectionUtils.invokeMethod(property.getReadMethod(), object);
|
||||
}
|
||||
|
||||
public Class<?> getValueType() {
|
||||
return propertyDescriptor.getPropertyType();
|
||||
return property.getPropertyType();
|
||||
}
|
||||
|
||||
public void setValue(Object value) {
|
||||
TypeDescriptor targetType = new TypeDescriptor(new MethodParameter(propertyDescriptor.getWriteMethod(), 0));
|
||||
if (value != null && typeConverter.canConvert(value.getClass(), targetType)) {
|
||||
value = typeConverter.convert(value, targetType);
|
||||
TypeDescriptor targetType = new TypeDescriptor(new MethodParameter(property.getWriteMethod(), 0));
|
||||
TypeConverter converter = bindingContext.getTypeConverter();
|
||||
if (value != null && converter.canConvert(value.getClass(), targetType)) {
|
||||
value = converter.convert(value, targetType);
|
||||
}
|
||||
ReflectionUtils.invokeMethod(propertyDescriptor.getWriteMethod(), model, value);
|
||||
ReflectionUtils.invokeMethod(property.getWriteMethod(), object, value);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
@ -243,7 +238,7 @@ public class PropertyBinding implements Binding {
|
|||
if (getValue() == null) {
|
||||
createValue();
|
||||
}
|
||||
return bindingRule.getBinding(property, getValue());
|
||||
return bindingContext.getBinding(property, getValue());
|
||||
}
|
||||
|
||||
public boolean isList() {
|
||||
|
|
@ -264,7 +259,7 @@ public class PropertyBinding implements Binding {
|
|||
assertMapProperty();
|
||||
if (key instanceof String) {
|
||||
try {
|
||||
key = bindingRule.getKeyFormatter().parse((String) key, getLocale());
|
||||
key = bindingContext.getKeyFormatter().parse((String) key, getLocale());
|
||||
} catch (ParseException e) {
|
||||
throw new IllegalArgumentException("Invald key", e);
|
||||
}
|
||||
|
|
@ -276,41 +271,17 @@ public class PropertyBinding implements Binding {
|
|||
public String formatValue(Object value) {
|
||||
Formatter formatter;
|
||||
if (isList() || isMap()) {
|
||||
formatter = bindingRule.getElementFormatter();
|
||||
formatter = bindingContext.getElementFormatter();
|
||||
} else {
|
||||
formatter = bindingRule.getFormatter();
|
||||
formatter = bindingContext.getFormatter();
|
||||
}
|
||||
Class<?> formattedType = getFormattedObjectType(formatter.getClass());
|
||||
value = typeConverter.convert(value, formattedType);
|
||||
value = bindingContext.getTypeConverter().convert(value, formattedType);
|
||||
return formatter.format(value, getLocale());
|
||||
}
|
||||
|
||||
// internal helpers
|
||||
|
||||
private void initProperty(String property, Object model) {
|
||||
this.propertyDescriptor = findPropertyDescriptor(property, model);
|
||||
this.property = property;
|
||||
}
|
||||
|
||||
private PropertyDescriptor findPropertyDescriptor(String property, Object model) {
|
||||
PropertyDescriptor[] propDescs = getBeanInfo(model.getClass()).getPropertyDescriptors();
|
||||
for (PropertyDescriptor propDesc : propDescs) {
|
||||
if (propDesc.getName().equals(property)) {
|
||||
return propDesc;
|
||||
}
|
||||
}
|
||||
throw new IllegalArgumentException("No property '" + property + "' found on model ["
|
||||
+ model.getClass().getName() + "]");
|
||||
}
|
||||
|
||||
private BeanInfo getBeanInfo(Class<?> clazz) {
|
||||
try {
|
||||
return Introspector.getBeanInfo(clazz);
|
||||
} catch (IntrospectionException e) {
|
||||
throw new IllegalStateException("Unable to introspect model type " + clazz);
|
||||
}
|
||||
}
|
||||
|
||||
private Locale getLocale() {
|
||||
return LocaleContextHolder.getLocale();
|
||||
}
|
||||
|
|
@ -320,11 +291,11 @@ public class PropertyBinding implements Binding {
|
|||
Object value = getModel().getValueType().newInstance();
|
||||
getModel().setValue(value);
|
||||
} catch (InstantiationException e) {
|
||||
throw new IllegalStateException("Could not lazily instantiate model of type [" + getModel().getValueType().getName()
|
||||
+ "] to access property" + property, e);
|
||||
throw new IllegalStateException("Could not lazily instantiate object of type ["
|
||||
+ getModel().getValueType().getName() + "] to access property" + property, e);
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new IllegalStateException("Could not lazily instantiate model of type [" + getModel().getValueType().getName()
|
||||
+ "] to access property" + property, e);
|
||||
throw new IllegalStateException("Could not lazily instantiate object of type ["
|
||||
+ getModel().getValueType().getName() + "] to access property" + property, e);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -358,8 +329,7 @@ public class PropertyBinding implements Binding {
|
|||
|
||||
@SuppressWarnings("unused")
|
||||
private CollectionTypeDescriptor getCollectionTypeDescriptor() {
|
||||
Class<?> elementType = GenericCollectionTypeResolver.getCollectionReturnType(propertyDescriptor
|
||||
.getReadMethod());
|
||||
Class<?> elementType = GenericCollectionTypeResolver.getCollectionReturnType(property.getReadMethod());
|
||||
return new CollectionTypeDescriptor(getModel().getValueType(), elementType);
|
||||
}
|
||||
|
||||
|
|
@ -397,6 +367,13 @@ public class PropertyBinding implements Binding {
|
|||
}
|
||||
|
||||
private boolean isWriteableProperty() {
|
||||
return propertyDescriptor.getWriteMethod() != null;
|
||||
return property.getWriteMethod() != null;
|
||||
}
|
||||
|
||||
static abstract class AbstractAlert implements Alert {
|
||||
public String toString() {
|
||||
return getCode() + " - " + getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -97,32 +97,29 @@ public class GenericBinderTests {
|
|||
assertEquals(new DateFormatter().parse("2009-06-01", Locale.US), bean.getDate());
|
||||
}
|
||||
|
||||
/*
|
||||
@Test
|
||||
public void bindSingleValuePropertyFormatterParseException() {
|
||||
BindingRulesBuilder builder = new BindingRulesBuilder(TestBean.class);
|
||||
builder.bind("date").formatWith(new DateFormatter());
|
||||
GenericBinder binder = new GenericBinder(bean, builder.getBindingRules());
|
||||
|
||||
GenericBinder binder = new GenericBinder(bean);
|
||||
binder.bindingRule("date").formatWith(new DateFormatter());
|
||||
BindingResults results = binder.bind(Collections.singletonMap("date", "bogus"));
|
||||
assertEquals(1, results.size());
|
||||
assertTrue(results.get(0).isFailure());
|
||||
assertEquals("invalidFormat", results.get(0).getAlert().getCode());
|
||||
assertEquals("typeMismatch", results.get(0).getAlert().getCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bindSingleValueWithFormatterRegistedByType() throws ParseException {
|
||||
BindingRulesBuilder builder = new BindingRulesBuilder(TestBean.class);
|
||||
builder.bind("date").formatWith(new DateFormatter());
|
||||
GenericBinder binder = new GenericBinder(bean, builder.getBindingRules());
|
||||
GenericBinder binder = new GenericBinder(bean);
|
||||
|
||||
GenericFormatterRegistry formatterRegistry = new GenericFormatterRegistry();
|
||||
formatterRegistry.add(Date.class, new DateFormatter());
|
||||
binder.setFormatterRegistry(formatterRegistry);
|
||||
|
||||
binder.bind(Collections.singletonMap("date", "2009-06-01"));
|
||||
assertEquals(new DateFormatter().parse("2009-06-01", Locale.US), bean.getDate());
|
||||
}
|
||||
|
||||
/*
|
||||
@Test
|
||||
public void bindSingleValueWithAnnotationFormatterFactoryRegistered() throws ParseException {
|
||||
binder.addBinding("currency");
|
||||
|
|
|
|||
Loading…
Reference in New Issue