From: Artur Signell Date: Wed, 21 Dec 2011 07:10:17 +0000 (+0200) Subject: #8125 Removed Property.ConversionException and String constructor based X-Git-Tag: 7.0.0.alpha1~64 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=7e594fb40fd2a9ad1759d422a62a5c4045351e6d;p=vaadin-framework.git #8125 Removed Property.ConversionException and String constructor based conversion from Property classes --- diff --git a/src/com/vaadin/data/Property.java b/src/com/vaadin/data/Property.java index c8499722a2..052a174408 100644 --- a/src/com/vaadin/data/Property.java +++ b/src/com/vaadin/data/Property.java @@ -65,12 +65,8 @@ public interface Property extends Serializable { * * @throws Property.ReadOnlyException * if the object is in read-only mode - * @throws Property.ConversionException - * if newValue can't be converted into the Property's native - * type directly or using a converter */ - public void setValue(Object newValue) throws Property.ReadOnlyException, - Property.ConversionException; + public void setValue(Object newValue) throws Property.ReadOnlyException; /** * Returns the type of the Property. The methods getValue and @@ -136,62 +132,6 @@ public interface Property extends Serializable { } } - /** - * An exception that signals that the value passed to the - * setValue method couldn't be converted to the native type of - * the Property. - * - * @author Vaadin Ltd - * @version - * @VERSION@ - * @since 3.0 - */ - @SuppressWarnings("serial") - public class ConversionException extends RuntimeException { - - /** - * Constructs a new ConversionException without a detail - * message. - */ - public ConversionException() { - } - - /** - * Constructs a new ConversionException with the specified - * detail message. - * - * @param msg - * the detail message - */ - public ConversionException(String msg) { - super(msg); - } - - /** - * Constructs a new ConversionException from another - * exception. - * - * @param cause - * The cause of the the conversion failure - */ - public ConversionException(Throwable cause) { - super(cause); - } - - /** - * Constructs a new ConversionException with the specified - * detail message and cause. - * - * @param message - * the detail message - * @param cause - * The cause of the the conversion failure - */ - public ConversionException(String message, Throwable cause) { - super(message, cause); - } - } - /** * Interface implemented by the viewer classes capable of using a Property * as a data source. diff --git a/src/com/vaadin/data/util/IndexedContainer.java b/src/com/vaadin/data/util/IndexedContainer.java index 56ad2a929f..424fccb1e9 100644 --- a/src/com/vaadin/data/util/IndexedContainer.java +++ b/src/com/vaadin/data/util/IndexedContainer.java @@ -5,7 +5,6 @@ package com.vaadin.data.util; import java.io.Serializable; -import java.lang.reflect.Constructor; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -865,8 +864,7 @@ public class IndexedContainer extends * * @see com.vaadin.data.Property#setValue(java.lang.Object) */ - public void setValue(Object newValue) - throws Property.ReadOnlyException, Property.ConversionException { + public void setValue(Object newValue) throws Property.ReadOnlyException { // Gets the Property set final Map propertySet = items.get(itemId); @@ -877,22 +875,8 @@ public class IndexedContainer extends } else if (getType().isAssignableFrom(newValue.getClass())) { propertySet.put(propertyId, newValue); } else { - try { - - // Gets the string constructor - final Constructor constr = getType().getConstructor( - new Class[] { String.class }); - - // Creates new object from the string - propertySet.put(propertyId, constr - .newInstance(new Object[] { newValue.toString() })); - - } catch (final java.lang.Exception e) { - throw new Property.ConversionException( - "Conversion for value '" + newValue + "' of class " - + newValue.getClass().getName() + " to " - + getType().getName() + " failed", e); - } + throw new IllegalArgumentException("Value is of invalid type, " + + getType().getName() + " expected"); } // update the container filtering if this property is being filtered diff --git a/src/com/vaadin/data/util/MethodProperty.java b/src/com/vaadin/data/util/MethodProperty.java index ec9a4e0c0a..bffa2ae94e 100644 --- a/src/com/vaadin/data/util/MethodProperty.java +++ b/src/com/vaadin/data/util/MethodProperty.java @@ -644,8 +644,7 @@ public class MethodProperty extends AbstractProperty { * @see #invokeSetMethod(Object) */ @SuppressWarnings("unchecked") - public void setValue(Object newValue) throws Property.ReadOnlyException, - Property.ConversionException { + public void setValue(Object newValue) throws Property.ReadOnlyException { // Checks the mode if (isReadOnly()) { @@ -654,7 +653,7 @@ public class MethodProperty extends AbstractProperty { // Checks the type of the value if (newValue != null && !type.isAssignableFrom(newValue.getClass())) { - throw new Property.ConversionException( + throw new IllegalArgumentException( "Invalid value type for ObjectProperty."); } diff --git a/src/com/vaadin/data/util/NestedMethodProperty.java b/src/com/vaadin/data/util/NestedMethodProperty.java index b8b6ad7101..9cd83044e9 100644 --- a/src/com/vaadin/data/util/NestedMethodProperty.java +++ b/src/com/vaadin/data/util/NestedMethodProperty.java @@ -206,8 +206,7 @@ public class NestedMethodProperty extends AbstractProperty { * native type directly or through String. * @see #invokeSetMethod(Object) */ - public void setValue(Object newValue) throws ReadOnlyException, - ConversionException { + public void setValue(Object newValue) throws ReadOnlyException { // Checks the mode if (isReadOnly()) { throw new Property.ReadOnlyException(); @@ -215,7 +214,7 @@ public class NestedMethodProperty extends AbstractProperty { // Checks the type of the value if (newValue != null && !type.isAssignableFrom(newValue.getClass())) { - throw new Property.ConversionException( + throw new IllegalArgumentException( "Invalid value type for NestedMethodProperty."); } diff --git a/src/com/vaadin/data/util/ObjectProperty.java b/src/com/vaadin/data/util/ObjectProperty.java index 79e5436cc5..40e25dca89 100644 --- a/src/com/vaadin/data/util/ObjectProperty.java +++ b/src/com/vaadin/data/util/ObjectProperty.java @@ -114,12 +114,9 @@ public class ObjectProperty extends AbstractProperty { * the New value of the property. * @throws Property.ReadOnlyException if the object is in * read-only mode - * @throws Property.ConversionException if the newValue is not - * of a correct type */ @SuppressWarnings("unchecked") - public void setValue(Object newValue) throws Property.ReadOnlyException, - Property.ConversionException { + public void setValue(Object newValue) throws Property.ReadOnlyException { // Checks the mode if (isReadOnly()) { @@ -128,7 +125,7 @@ public class ObjectProperty extends AbstractProperty { // Checks the type of the value if (newValue != null && !type.isAssignableFrom(newValue.getClass())) { - throw new Property.ConversionException( + throw new IllegalArgumentException( "Invalid value type for ObjectProperty."); } diff --git a/src/com/vaadin/data/util/PropertyFormatter.java b/src/com/vaadin/data/util/PropertyFormatter.java index b778028613..1b27513594 100644 --- a/src/com/vaadin/data/util/PropertyFormatter.java +++ b/src/com/vaadin/data/util/PropertyFormatter.java @@ -204,8 +204,7 @@ public abstract class PropertyFormatter extends AbstractProperty } } - public void setValue(Object newValue) throws ReadOnlyException, - ConversionException { + public void setValue(Object newValue) throws ReadOnlyException { if (dataSource == null) { return; } @@ -220,10 +219,8 @@ public abstract class PropertyFormatter extends AbstractProperty if (!newValue.equals(getStringValue())) { fireValueChange(); } - } catch (ConversionException e) { - throw e; } catch (Exception e) { - throw new ConversionException(e); + throw new IllegalArgumentException("Could not parse value", e); } } } diff --git a/src/com/vaadin/data/util/TransactionalPropertyWrapper.java b/src/com/vaadin/data/util/TransactionalPropertyWrapper.java index 2cc0154b22..b593387d71 100644 --- a/src/com/vaadin/data/util/TransactionalPropertyWrapper.java +++ b/src/com/vaadin/data/util/TransactionalPropertyWrapper.java @@ -37,8 +37,7 @@ public class TransactionalPropertyWrapper extends AbstractProperty return wrappedProperty.getValue(); } - public void setValue(Object newValue) throws ReadOnlyException, - ConversionException { + public void setValue(Object newValue) throws ReadOnlyException { // Causes a value change to be sent to this listener which in turn fires // a new value change event for this property wrappedProperty.setValue(newValue); diff --git a/src/com/vaadin/data/util/sqlcontainer/ColumnProperty.java b/src/com/vaadin/data/util/sqlcontainer/ColumnProperty.java index f9b07f8c4c..5e152da79e 100644 --- a/src/com/vaadin/data/util/sqlcontainer/ColumnProperty.java +++ b/src/com/vaadin/data/util/sqlcontainer/ColumnProperty.java @@ -3,7 +3,6 @@ */ package com.vaadin.data.util.sqlcontainer; -import java.lang.reflect.Constructor; import java.sql.Date; import java.sql.Time; import java.sql.Timestamp; @@ -69,8 +68,7 @@ final public class ColumnProperty implements Property { return value; } - public void setValue(Object newValue) throws ReadOnlyException, - ConversionException { + public void setValue(Object newValue) throws ReadOnlyException { if (newValue == null && !nullable) { throw new NotNullableException( "Null values are not allowed for this property."); @@ -109,19 +107,9 @@ final public class ColumnProperty implements Property { } } - /* - * If the type is not correct, try to generate it through a possibly - * existing String constructor. - */ if (!getType().isAssignableFrom(newValue.getClass())) { - try { - final Constructor constr = getType().getConstructor( - new Class[] { String.class }); - newValue = constr.newInstance(new Object[] { newValue - .toString() }); - } catch (Exception e) { - throw new ConversionException(e); - } + throw new IllegalArgumentException( + "Illegal value type for ColumnProperty"); } /* diff --git a/src/com/vaadin/ui/AbstractField.java b/src/com/vaadin/ui/AbstractField.java index daef4c0ddb..182aff05b5 100644 --- a/src/com/vaadin/ui/AbstractField.java +++ b/src/com/vaadin/ui/AbstractField.java @@ -557,11 +557,11 @@ public abstract class AbstractField extends AbstractComponent implements * @throws Property.ConversionException */ public void setValue(Object newFieldValue) - throws Property.ReadOnlyException, Property.ConversionException { + throws Property.ReadOnlyException, Converter.ConversionException { // This check is needed as long as setValue accepts Object instead of T if (newFieldValue != null) { if (!getType().isAssignableFrom(newFieldValue.getClass())) { - throw new ConversionException("Value of type " + throw new Converter.ConversionException("Value of type " + newFieldValue.getClass() + " cannot be assigned to " + getClass().getName()); } @@ -577,10 +577,9 @@ public abstract class AbstractField extends AbstractComponent implements * @param repaintIsNotNeeded * True iff caller is sure that repaint is not needed. * @throws Property.ReadOnlyException - * @throws Property.ConversionException */ protected void setValue(T newFieldValue, boolean repaintIsNotNeeded) - throws Property.ReadOnlyException, Property.ConversionException, + throws Property.ReadOnlyException, Converter.ConversionException, InvalidValueException { if (!equals(newFieldValue, getInternalValue())) { @@ -826,7 +825,7 @@ public abstract class AbstractField extends AbstractComponent implements if (getType().isAssignableFrom(newValue.getClass())) { return (T) newValue; } else { - throw new ConversionException( + throw new Converter.ConversionException( "Unable to convert value of type " + newValue.getClass().getName() + " to " @@ -857,7 +856,7 @@ public abstract class AbstractField extends AbstractComponent implements return valueConverter.convertFromTargetToSource(fieldValue, getLocale()); } catch (com.vaadin.data.util.converter.Converter.ConversionException e) { - throw new ConversionException( + throw new Converter.ConversionException( getValueConversionError(valueConverter.getSourceType())); } } diff --git a/src/com/vaadin/ui/AbstractSelect.java b/src/com/vaadin/ui/AbstractSelect.java index 8b6def08a6..3e483333ff 100644 --- a/src/com/vaadin/ui/AbstractSelect.java +++ b/src/com/vaadin/ui/AbstractSelect.java @@ -516,16 +516,9 @@ public abstract class AbstractSelect extends AbstractField implements // Sets the caption property, if used if (getItemCaptionPropertyId() != null) { - try { - getContainerProperty(newItemCaption, - getItemCaptionPropertyId()).setValue( - newItemCaption); - } catch (final Property.ConversionException ignored) { - /* - * The conversion exception is safely ignored, the - * caption is just missing - */ - } + getContainerProperty(newItemCaption, + getItemCaptionPropertyId()) + .setValue(newItemCaption); } if (isMultiSelect()) { Set values = new HashSet((Collection) getValue()); @@ -615,8 +608,7 @@ public abstract class AbstractSelect extends AbstractField implements * @see com.vaadin.ui.AbstractField#setValue(java.lang.Object) */ @Override - public void setValue(Object newValue) throws Property.ReadOnlyException, - Property.ConversionException { + public void setValue(Object newValue) throws Property.ReadOnlyException { if (newValue == getNullSelectionItemId()) { newValue = null; } @@ -642,7 +634,7 @@ public abstract class AbstractSelect extends AbstractField implements */ @Override protected void setValue(Object newValue, boolean repaintIsNotNeeded) - throws Property.ReadOnlyException, Property.ConversionException { + throws Property.ReadOnlyException { if (isMultiSelect()) { if (newValue == null) { diff --git a/src/com/vaadin/ui/AbstractTextField.java b/src/com/vaadin/ui/AbstractTextField.java index a958b61c2f..7d2509b978 100644 --- a/src/com/vaadin/ui/AbstractTextField.java +++ b/src/com/vaadin/ui/AbstractTextField.java @@ -505,8 +505,7 @@ public abstract class AbstractTextField extends AbstractField implements } @Override - public void setValue(Object newValue) throws ReadOnlyException, - ConversionException { + public void setValue(Object newValue) throws ReadOnlyException { super.setValue(newValue); /* * Make sure w reset lastKnownTextContent field on value change. The diff --git a/src/com/vaadin/ui/DateField.java b/src/com/vaadin/ui/DateField.java index c3e068e4c2..085f0b4826 100644 --- a/src/com/vaadin/ui/DateField.java +++ b/src/com/vaadin/ui/DateField.java @@ -4,7 +4,6 @@ package com.vaadin.ui; -import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; @@ -19,6 +18,7 @@ import java.util.TimeZone; import com.vaadin.data.Property; import com.vaadin.data.Validator; import com.vaadin.data.Validator.InvalidValueException; +import com.vaadin.data.util.converter.Converter; import com.vaadin.event.FieldEvents; import com.vaadin.event.FieldEvents.BlurEvent; import com.vaadin.event.FieldEvents.BlurListener; @@ -424,7 +424,7 @@ public class DateField extends AbstractField implements * this case the invalid text remains in the DateField. */ requestRepaint(); - } catch (ConversionException e) { + } catch (Converter.ConversionException e) { /* * Datefield now contains some text that could't be parsed @@ -504,9 +504,9 @@ public class DateField extends AbstractField implements * to keep the old value and indicate an error */ protected Date handleUnparsableDateString(String dateString) - throws Property.ConversionException { + throws Converter.ConversionException { currentParseErrorMessage = null; - throw new Property.ConversionException(getParseErrorMessage()); + throw new Converter.ConversionException(getParseErrorMessage()); } /* Property features */ @@ -527,7 +527,7 @@ public class DateField extends AbstractField implements */ @Override protected void setValue(Date newValue, boolean repaintIsNotNeeded) - throws Property.ReadOnlyException, Property.ConversionException { + throws Property.ReadOnlyException { /* * First handle special case when the client side component have a date @@ -560,23 +560,7 @@ public class DateField extends AbstractField implements return; } - if (newValue == null || newValue instanceof Date) { - super.setValue(newValue, repaintIsNotNeeded); - } else { - // Try to parse the given string value to Date - try { - final SimpleDateFormat parser = new SimpleDateFormat(); - final TimeZone currentTimeZone = getTimeZone(); - if (currentTimeZone != null) { - parser.setTimeZone(currentTimeZone); - } - final Date val = parser.parse(newValue.toString()); - super.setValue(val, repaintIsNotNeeded); - } catch (final ParseException e) { - uiHasValidDateString = false; - throw new Property.ConversionException(getParseErrorMessage()); - } - } + super.setValue(newValue, repaintIsNotNeeded); } /** diff --git a/src/com/vaadin/ui/Slider.java b/src/com/vaadin/ui/Slider.java index 3e7b4393a0..f077afb8ff 100644 --- a/src/com/vaadin/ui/Slider.java +++ b/src/com/vaadin/ui/Slider.java @@ -269,8 +269,7 @@ public class Slider extends AbstractField { @Override public void setValue(Object newFieldValue) - throws com.vaadin.data.Property.ReadOnlyException, - com.vaadin.data.Property.ConversionException { + throws com.vaadin.data.Property.ReadOnlyException { if (newFieldValue != null && newFieldValue instanceof Number && !(newFieldValue instanceof Double)) { // Support setting all types of Numbers diff --git a/tests/server-side/com/vaadin/data/util/filter/AbstractFilterTest.java b/tests/server-side/com/vaadin/data/util/filter/AbstractFilterTest.java index c26847fb63..efc31f0bd4 100644 --- a/tests/server-side/com/vaadin/data/util/filter/AbstractFilterTest.java +++ b/tests/server-side/com/vaadin/data/util/filter/AbstractFilterTest.java @@ -28,8 +28,7 @@ public abstract class AbstractFilterTest extends return null; } - public void setValue(Object newValue) throws ReadOnlyException, - ConversionException { + public void setValue(Object newValue) throws ReadOnlyException { throw new ReadOnlyException(); } diff --git a/tests/server-side/com/vaadin/tests/server/component/abstractfield/AbstractFieldValueConversions.java b/tests/server-side/com/vaadin/tests/server/component/abstractfield/AbstractFieldValueConversions.java index bf42b0784f..0bc40097d2 100644 --- a/tests/server-side/com/vaadin/tests/server/component/abstractfield/AbstractFieldValueConversions.java +++ b/tests/server-side/com/vaadin/tests/server/component/abstractfield/AbstractFieldValueConversions.java @@ -87,7 +87,7 @@ public class AbstractFieldValueConversions extends TestCase { try { tf.setValue(1); fail("setValue(Integer) should throw an exception"); - } catch (com.vaadin.data.Property.ConversionException e) { + } catch (Converter.ConversionException e) { // OK, expected } } diff --git a/tests/testbench/com/vaadin/tests/components/customfield/AddressField.java b/tests/testbench/com/vaadin/tests/components/customfield/AddressField.java index a02b3235d8..68f7baf5e9 100644 --- a/tests/testbench/com/vaadin/tests/components/customfield/AddressField.java +++ b/tests/testbench/com/vaadin/tests/components/customfield/AddressField.java @@ -48,8 +48,7 @@ public class AddressField extends CustomField
{ } @Override - public void setInternalValue(Address address) throws ReadOnlyException, - ConversionException { + public void setInternalValue(Address address) throws ReadOnlyException { // create the address if not given if (null == address) { address = new Address(); diff --git a/tests/testbench/com/vaadin/tests/components/datefield/DateFieldUnparsableDate.java b/tests/testbench/com/vaadin/tests/components/datefield/DateFieldUnparsableDate.java index 32456e34c5..3eb6e020b0 100644 --- a/tests/testbench/com/vaadin/tests/components/datefield/DateFieldUnparsableDate.java +++ b/tests/testbench/com/vaadin/tests/components/datefield/DateFieldUnparsableDate.java @@ -3,6 +3,7 @@ package com.vaadin.tests.components.datefield; import java.util.Date; import com.vaadin.data.Property; +import com.vaadin.data.util.converter.Converter; import com.vaadin.tests.components.TestBase; import com.vaadin.ui.DateField; @@ -16,14 +17,14 @@ public class DateFieldUnparsableDate extends TestBase { addListener(new Property.ValueChangeListener() { public void valueChange( com.vaadin.data.Property.ValueChangeEvent event) { - oldDate = (Date) getValue(); + oldDate = getValue(); } }); } @Override protected Date handleUnparsableDateString(String dateString) - throws ConversionException { + throws Converter.ConversionException { return oldDate; } } @@ -35,7 +36,7 @@ public class DateFieldUnparsableDate extends TestBase { @Override protected Date handleUnparsableDateString(String dateString) - throws ConversionException { + throws Converter.ConversionException { return null; } } @@ -47,8 +48,9 @@ public class DateFieldUnparsableDate extends TestBase { @Override protected Date handleUnparsableDateString(String dateString) - throws ConversionException { - throw new ConversionException("You should not enter invalid dates!"); + throws Converter.ConversionException { + throw new Converter.ConversionException( + "You should not enter invalid dates!"); } } @@ -59,11 +61,12 @@ public class DateFieldUnparsableDate extends TestBase { @Override protected Date handleUnparsableDateString(String dateString) - throws ConversionException { + throws Converter.ConversionException { if (dateString != null && dateString.equals("today")) { return new Date(); } - throw new ConversionException("You should not enter invalid dates!"); + throw new Converter.ConversionException( + "You should not enter invalid dates!"); } } diff --git a/tests/testbench/com/vaadin/tests/components/textfield/TextFieldWithPropertyFormatter.java b/tests/testbench/com/vaadin/tests/components/textfield/TextFieldWithPropertyFormatter.java index e8219b4b80..ea4b572604 100644 --- a/tests/testbench/com/vaadin/tests/components/textfield/TextFieldWithPropertyFormatter.java +++ b/tests/testbench/com/vaadin/tests/components/textfield/TextFieldWithPropertyFormatter.java @@ -32,14 +32,14 @@ public class TextFieldWithPropertyFormatter extends TestBase { return value; } - public void setValue(Object newValue) throws ReadOnlyException, - ConversionException { + public void setValue(Object newValue) throws ReadOnlyException { if (newValue == null) { value = null; } else if (newValue instanceof BigDecimal) { value = (BigDecimal) newValue; } else { - throw new ConversionException(); + throw new IllegalArgumentException( + "Value must be of type BigDecimal"); } }