From 883c3a6b4f4a57318ae851994fd2fb61ed7bd6b4 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 13 Sep 2016 21:55:20 +0200 Subject: [PATCH] Revised IllegalArgumentException handling for Formatter parse calls Issue: SPR-14661 (cherry picked from commit 73bbe08) --- .../beans/AbstractNestablePropertyAccessor.java | 6 ++---- .../org/springframework/beans/TypeConverterSupport.java | 2 +- .../format/support/FormatterPropertyEditorAdapter.java | 6 ++++-- .../format/support/FormattingConversionService.java | 8 +++++--- 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/AbstractNestablePropertyAccessor.java b/spring-beans/src/main/java/org/springframework/beans/AbstractNestablePropertyAccessor.java index cf3ebd9a64b..78053009d15 100644 --- a/spring-beans/src/main/java/org/springframework/beans/AbstractNestablePropertyAccessor.java +++ b/spring-beans/src/main/java/org/springframework/beans/AbstractNestablePropertyAccessor.java @@ -590,7 +590,7 @@ public abstract class AbstractNestablePropertyAccessor extends AbstractPropertyA new PropertyChangeEvent(this.rootObject, this.nestedPath + propertyName, oldValue, newValue); throw new ConversionNotSupportedException(pce, requiredType, ex); } - catch (Throwable ex) { + catch (IllegalArgumentException ex) { PropertyChangeEvent pce = new PropertyChangeEvent(this.rootObject, this.nestedPath + propertyName, oldValue, newValue); throw new TypeMismatchException(pce, requiredType, ex); @@ -914,9 +914,7 @@ public abstract class AbstractNestablePropertyAccessor extends AbstractPropertyA return BeanUtils.instantiate(type); } } - catch (Exception ex) { - // TODO: Root cause exception context is lost here; just exception message preserved. - // Should we throw another exception type that preserves context instead? + catch (Throwable ex) { throw new NullValueInNestedPathException(getRootClass(), this.nestedPath + name, "Could not instantiate property type [" + type.getName() + "] to auto-grow nested property path: " + ex); } diff --git a/spring-beans/src/main/java/org/springframework/beans/TypeConverterSupport.java b/spring-beans/src/main/java/org/springframework/beans/TypeConverterSupport.java index ba9fc09cca0..9be206c6943 100644 --- a/spring-beans/src/main/java/org/springframework/beans/TypeConverterSupport.java +++ b/spring-beans/src/main/java/org/springframework/beans/TypeConverterSupport.java @@ -73,7 +73,7 @@ public abstract class TypeConverterSupport extends PropertyEditorRegistrySupport catch (IllegalStateException ex) { throw new ConversionNotSupportedException(value, requiredType, ex); } - catch (Throwable ex) { + catch (IllegalArgumentException ex) { throw new TypeMismatchException(value, requiredType, ex); } } diff --git a/spring-context/src/main/java/org/springframework/format/support/FormatterPropertyEditorAdapter.java b/spring-context/src/main/java/org/springframework/format/support/FormatterPropertyEditorAdapter.java index 7d490f5d32e..c3812473a11 100644 --- a/spring-context/src/main/java/org/springframework/format/support/FormatterPropertyEditorAdapter.java +++ b/spring-context/src/main/java/org/springframework/format/support/FormatterPropertyEditorAdapter.java @@ -18,7 +18,6 @@ package org.springframework.format.support; import java.beans.PropertyEditor; import java.beans.PropertyEditorSupport; -import java.text.ParseException; import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.format.Formatter; @@ -65,7 +64,10 @@ public class FormatterPropertyEditorAdapter extends PropertyEditorSupport { try { setValue(this.formatter.parse(text, LocaleContextHolder.getLocale())); } - catch (ParseException ex) { + catch (IllegalArgumentException ex) { + throw ex; + } + catch (Throwable ex) { throw new IllegalArgumentException("Parse attempt failed for value [" + text + "]", ex); } } diff --git a/spring-context/src/main/java/org/springframework/format/support/FormattingConversionService.java b/spring-context/src/main/java/org/springframework/format/support/FormattingConversionService.java index e65e4fbda99..342607ed349 100644 --- a/spring-context/src/main/java/org/springframework/format/support/FormattingConversionService.java +++ b/spring-context/src/main/java/org/springframework/format/support/FormattingConversionService.java @@ -17,7 +17,6 @@ package org.springframework.format.support; import java.lang.annotation.Annotation; -import java.text.ParseException; import java.util.Collections; import java.util.Map; import java.util.Set; @@ -193,11 +192,14 @@ public class FormattingConversionService extends GenericConversionService try { result = this.parser.parse(text, LocaleContextHolder.getLocale()); } - catch (ParseException ex) { + catch (IllegalArgumentException ex) { + throw ex; + } + catch (Throwable ex) { throw new IllegalArgumentException("Parse attempt failed for value [" + text + "]", ex); } if (result == null) { - throw new IllegalStateException("Parsers are not allowed to return null"); + throw new IllegalStateException("Parsers are not allowed to return null: " + this.parser); } TypeDescriptor resultType = TypeDescriptor.valueOf(result.getClass()); if (!resultType.isAssignableTo(targetType)) {