From 34357d2b1f4bdc605d2c033200513381007165dd Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 15 Sep 2009 10:51:50 +0000 Subject: [PATCH] revised DefaultBindingErrorProcessor to use direct getPropertyName() and getValue() calls on PropertyAccessException itself (SPR-6111) --- .../beans/PropertyAccessException.java | 18 ++++++++++++++++-- .../beans/PropertyBatchUpdateException.java | 12 ++++++------ .../beans/TypeMismatchException.java | 3 ++- .../DefaultBindingErrorProcessor.java | 10 ++++------ 4 files changed, 28 insertions(+), 15 deletions(-) diff --git a/org.springframework.beans/src/main/java/org/springframework/beans/PropertyAccessException.java b/org.springframework.beans/src/main/java/org/springframework/beans/PropertyAccessException.java index 0b42d3dae4a..830ea5f40d0 100644 --- a/org.springframework.beans/src/main/java/org/springframework/beans/PropertyAccessException.java +++ b/org.springframework.beans/src/main/java/org/springframework/beans/PropertyAccessException.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2007 the original author or authors. + * Copyright 2002-2009 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -55,11 +55,25 @@ public abstract class PropertyAccessException extends BeansException implements /** * Return the PropertyChangeEvent that resulted in the problem. - * May be null; only available if an actual bean property + *

May be null; only available if an actual bean property * was affected. */ public PropertyChangeEvent getPropertyChangeEvent() { return this.propertyChangeEvent; } + /** + * Return the name of the affected property, if available. + */ + public String getPropertyName() { + return (this.propertyChangeEvent != null ? this.propertyChangeEvent.getPropertyName() : null); + } + + /** + * Return the affected value that was about to be set, if any. + */ + public Object getValue() { + return (this.propertyChangeEvent != null ? this.propertyChangeEvent.getNewValue() : null); + } + } diff --git a/org.springframework.beans/src/main/java/org/springframework/beans/PropertyBatchUpdateException.java b/org.springframework.beans/src/main/java/org/springframework/beans/PropertyBatchUpdateException.java index f15ba5a45a6..ca66d655e28 100644 --- a/org.springframework.beans/src/main/java/org/springframework/beans/PropertyBatchUpdateException.java +++ b/org.springframework.beans/src/main/java/org/springframework/beans/PropertyBatchUpdateException.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2008 the original author or authors. + * Copyright 2002-2009 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,6 +20,7 @@ import java.io.PrintStream; import java.io.PrintWriter; import org.springframework.util.Assert; +import org.springframework.util.ObjectUtils; /** * Combined exception, composed of individual PropertyAccessException instances. @@ -60,19 +61,18 @@ public class PropertyBatchUpdateException extends BeansException { /** * Return an array of the propertyAccessExceptions stored in this object. - * Will return the empty array (not null) if there were no errors. + *

Will return the empty array (not null) if there were no errors. */ public final PropertyAccessException[] getPropertyAccessExceptions() { return this.propertyAccessExceptions; } /** - * Return the exception for this field, or null if there isn't one. + * Return the exception for this field, or null if there isn't any. */ public PropertyAccessException getPropertyAccessException(String propertyName) { - for (int i = 0; i < this.propertyAccessExceptions.length; i++) { - PropertyAccessException pae = this.propertyAccessExceptions[i]; - if (propertyName.equals(pae.getPropertyChangeEvent().getPropertyName())) { + for (PropertyAccessException pae : this.propertyAccessExceptions) { + if (ObjectUtils.nullSafeEquals(propertyName, pae.getPropertyName())) { return pae; } } diff --git a/org.springframework.beans/src/main/java/org/springframework/beans/TypeMismatchException.java b/org.springframework.beans/src/main/java/org/springframework/beans/TypeMismatchException.java index b50580bf309..f2d05a31f0f 100644 --- a/org.springframework.beans/src/main/java/org/springframework/beans/TypeMismatchException.java +++ b/org.springframework.beans/src/main/java/org/springframework/beans/TypeMismatchException.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2008 the original author or authors. + * Copyright 2002-2009 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -94,6 +94,7 @@ public class TypeMismatchException extends PropertyAccessException { /** * Return the offending value (may be null) */ + @Override public Object getValue() { return this.value; } diff --git a/org.springframework.context/src/main/java/org/springframework/validation/DefaultBindingErrorProcessor.java b/org.springframework.context/src/main/java/org/springframework/validation/DefaultBindingErrorProcessor.java index ecda92a9b32..50219cc6143 100644 --- a/org.springframework.context/src/main/java/org/springframework/validation/DefaultBindingErrorProcessor.java +++ b/org.springframework.context/src/main/java/org/springframework/validation/DefaultBindingErrorProcessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2008 the original author or authors. + * Copyright 2002-2009 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -62,12 +62,11 @@ public class DefaultBindingErrorProcessor implements BindingErrorProcessor { public void processPropertyAccessException(PropertyAccessException ex, BindingResult bindingResult) { // Create field error with the exceptions's code, e.g. "typeMismatch". - String field = ex.getPropertyChangeEvent().getPropertyName(); - Object value = ex.getPropertyChangeEvent().getNewValue(); + String field = ex.getPropertyName(); String[] codes = bindingResult.resolveMessageCodes(ex.getErrorCode(), field); Object[] arguments = getArgumentsForBindError(bindingResult.getObjectName(), field); bindingResult.addError(new FieldError( - bindingResult.getObjectName(), field, value, true, + bindingResult.getObjectName(), field, ex.getValue(), true, codes, arguments, ex.getLocalizedMessage())); } @@ -83,8 +82,7 @@ public class DefaultBindingErrorProcessor implements BindingErrorProcessor { */ protected Object[] getArgumentsForBindError(String objectName, String field) { String[] codes = new String[] {objectName + Errors.NESTED_PATH_SEPARATOR + field, field}; - String defaultMessage = field; - return new Object[] {new DefaultMessageSourceResolvable(codes, defaultMessage)}; + return new Object[] {new DefaultMessageSourceResolvable(codes, field)}; } }