]> source.dussan.org Git - vaadin-framework.git/commitdiff
#8125 Removed Property.ConversionException and String constructor based
authorArtur Signell <artur@vaadin.com>
Wed, 21 Dec 2011 07:10:17 +0000 (09:10 +0200)
committerArtur Signell <artur@vaadin.com>
Wed, 21 Dec 2011 07:10:56 +0000 (09:10 +0200)
conversion from Property classes

18 files changed:
src/com/vaadin/data/Property.java
src/com/vaadin/data/util/IndexedContainer.java
src/com/vaadin/data/util/MethodProperty.java
src/com/vaadin/data/util/NestedMethodProperty.java
src/com/vaadin/data/util/ObjectProperty.java
src/com/vaadin/data/util/PropertyFormatter.java
src/com/vaadin/data/util/TransactionalPropertyWrapper.java
src/com/vaadin/data/util/sqlcontainer/ColumnProperty.java
src/com/vaadin/ui/AbstractField.java
src/com/vaadin/ui/AbstractSelect.java
src/com/vaadin/ui/AbstractTextField.java
src/com/vaadin/ui/DateField.java
src/com/vaadin/ui/Slider.java
tests/server-side/com/vaadin/data/util/filter/AbstractFilterTest.java
tests/server-side/com/vaadin/tests/server/component/abstractfield/AbstractFieldValueConversions.java
tests/testbench/com/vaadin/tests/components/customfield/AddressField.java
tests/testbench/com/vaadin/tests/components/datefield/DateFieldUnparsableDate.java
tests/testbench/com/vaadin/tests/components/textfield/TextFieldWithPropertyFormatter.java

index c8499722a2866cc4b8cac24e5049158f26f74dfc..052a174408645b0d6fcd20c4cb04e513855b9b2b 100644 (file)
@@ -65,12 +65,8 @@ public interface Property<T> 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 <code>getValue</code> and
@@ -136,62 +132,6 @@ public interface Property<T> extends Serializable {
         }
     }
 
-    /**
-     * An exception that signals that the value passed to the
-     * <code>setValue</code> 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 <code>ConversionException</code> without a detail
-         * message.
-         */
-        public ConversionException() {
-        }
-
-        /**
-         * Constructs a new <code>ConversionException</code> with the specified
-         * detail message.
-         * 
-         * @param msg
-         *            the detail message
-         */
-        public ConversionException(String msg) {
-            super(msg);
-        }
-
-        /**
-         * Constructs a new <code>ConversionException</code> from another
-         * exception.
-         * 
-         * @param cause
-         *            The cause of the the conversion failure
-         */
-        public ConversionException(Throwable cause) {
-            super(cause);
-        }
-
-        /**
-         * Constructs a new <code>ConversionException</code> 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.
index 56ad2a929fbd04bec04a7d60efb71557d1727eda..424fccb1e9c9c33da0a1a9a2a39e7380a8cac910 100644 (file)
@@ -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<Object, Object> 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
index ec9a4e0c0a695a86220bd563226cf382305a88da..bffa2ae94eca2e873559d655a2f5079c7c204504 100644 (file)
@@ -644,8 +644,7 @@ public class MethodProperty<T> extends AbstractProperty<T> {
      * @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<T> extends AbstractProperty<T> {
 
         // 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.");
         }
 
index b8b6ad71019fed54f48e6fc85e9a0e0dc4abffa8..9cd83044e96cc19e24d45156690bc996602331ac 100644 (file)
@@ -206,8 +206,7 @@ public class NestedMethodProperty<T> extends AbstractProperty<T> {
      *         native type directly or through <code>String</code>.
      * @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<T> extends AbstractProperty<T> {
 
         // 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.");
         }
 
index 79e5436cc5c4c186f6afd4b212e1c6b50d7665be..40e25dca893c7bef85fe73148c919bf57c0485e9 100644 (file)
@@ -114,12 +114,9 @@ public class ObjectProperty<T> extends AbstractProperty<T> {
      *            the New value of the property.
      * @throws <code>Property.ReadOnlyException</code> if the object is in
      *         read-only mode
-     * @throws <code>Property.ConversionException</code> 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<T> extends AbstractProperty<T> {
 
         // 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.");
         }
 
index b778028613fe411a647a3ea7292967894d8ce96d..1b275135944777fa57d36f45188eab7af55859df 100644 (file)
@@ -204,8 +204,7 @@ public abstract class PropertyFormatter<T> extends AbstractProperty<String>
         }
     }
 
-    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<T> extends AbstractProperty<String>
                 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);
             }
         }
     }
index 2cc0154b22011c2ab8253aa4abb11ca0156c30e3..b593387d71d5cbaf793057c744c566960f852b52 100644 (file)
@@ -37,8 +37,7 @@ public class TransactionalPropertyWrapper<T> extends AbstractProperty<T>
         return wrappedProperty.getValue();\r
     }\r
 \r
-    public void setValue(Object newValue) throws ReadOnlyException,\r
-            ConversionException {\r
+    public void setValue(Object newValue) throws ReadOnlyException {\r
         // Causes a value change to be sent to this listener which in turn fires\r
         // a new value change event for this property\r
         wrappedProperty.setValue(newValue);\r
index f9b07f8c4c26d22e6ddd0e0e9f6b408af7850a3c..5e152da79e7e9c76fe2237133c72a69c8e64ed2b 100644 (file)
@@ -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");
             }
 
             /*
index daef4c0ddbc642041a67881d487caf09e7748919..182aff05b52399e27cd765b4f75da51087a1dee8 100644 (file)
@@ -557,11 +557,11 @@ public abstract class AbstractField<T> 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<T> 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<T> 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<T> 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()));
             }
         }
index 8b6def08a6ece2d52f4dad9e966c09fa15172e10..3e483333ff85139988da57958f1f4c33e2d37a09 100644 (file)
@@ -516,16 +516,9 @@ public abstract class AbstractSelect extends AbstractField<Object> 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<Object> 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<Object> implements
      */
     @Override
     protected void setValue(Object newValue, boolean repaintIsNotNeeded)
-            throws Property.ReadOnlyException, Property.ConversionException {
+            throws Property.ReadOnlyException {
 
         if (isMultiSelect()) {
             if (newValue == null) {
index a958b61c2fe6afadc2318eaa1f14a6a18fac651f..7d2509b978083c652d1ec1daefef334b0c04c2ce 100644 (file)
@@ -505,8 +505,7 @@ public abstract class AbstractTextField extends AbstractField<String> 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
index c3e068e4c2e9bb0788f6e223e91b04aa42432342..085f0b48267c0689afe07ba64e3252bc742e0238 100644 (file)
@@ -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<Date> 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<Date> 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<Date> 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<Date> 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);
     }
 
     /**
index 3e7b4393a0369747cb4a30f10d1e97cc5524a2c5..f077afb8ffe6d419ee073190b162111914910ef5 100644 (file)
@@ -269,8 +269,7 @@ public class Slider extends AbstractField<Double> {
 
     @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
index c26847fb633c9b1d63584f93f6b22f49a465e6ca..efc31f0bd480d58fb4553cab94e175883f8c3dc9 100644 (file)
@@ -28,8 +28,7 @@ public abstract class AbstractFilterTest<FILTERTYPE extends Filter> extends
             return null;
         }
 
-        public void setValue(Object newValue) throws ReadOnlyException,
-                ConversionException {
+        public void setValue(Object newValue) throws ReadOnlyException {
             throw new ReadOnlyException();
         }
 
index bf42b0784f4e1908ba89a8780f8af14fa59c85ba..0bc40097d27bb909c27c55a78e0610478b47f2a1 100644 (file)
@@ -87,7 +87,7 @@ public class AbstractFieldValueConversions extends TestCase {
         try {\r
             tf.setValue(1);\r
             fail("setValue(Integer) should throw an exception");\r
-        } catch (com.vaadin.data.Property.ConversionException e) {\r
+        } catch (Converter.ConversionException e) {\r
             // OK, expected\r
         }\r
     }\r
index a02b3235d81a19541619deb03af88910dca58d2a..68f7baf5e92e171353f4012f157767101bf69853 100644 (file)
@@ -48,8 +48,7 @@ public class AddressField extends CustomField<Address> {
     }\r
 \r
     @Override\r
-    public void setInternalValue(Address address) throws ReadOnlyException,\r
-            ConversionException {\r
+    public void setInternalValue(Address address) throws ReadOnlyException {\r
         // create the address if not given\r
         if (null == address) {\r
             address = new Address();\r
index 32456e34c5ef6d019515809eaf51584916fbd8e9..3eb6e020b011b1dc48f4ccea3268abc7aec03391 100644 (file)
@@ -3,6 +3,7 @@ package com.vaadin.tests.components.datefield;
 import java.util.Date;\r
 \r
 import com.vaadin.data.Property;\r
+import com.vaadin.data.util.converter.Converter;\r
 import com.vaadin.tests.components.TestBase;\r
 import com.vaadin.ui.DateField;\r
 \r
@@ -16,14 +17,14 @@ public class DateFieldUnparsableDate extends TestBase {
             addListener(new Property.ValueChangeListener() {\r
                 public void valueChange(\r
                         com.vaadin.data.Property.ValueChangeEvent event) {\r
-                    oldDate = (Date) getValue();\r
+                    oldDate = getValue();\r
                 }\r
             });\r
         }\r
 \r
         @Override\r
         protected Date handleUnparsableDateString(String dateString)\r
-                throws ConversionException {\r
+                throws Converter.ConversionException {\r
             return oldDate;\r
         }\r
     }\r
@@ -35,7 +36,7 @@ public class DateFieldUnparsableDate extends TestBase {
 \r
         @Override\r
         protected Date handleUnparsableDateString(String dateString)\r
-                throws ConversionException {\r
+                throws Converter.ConversionException {\r
             return null;\r
         }\r
     }\r
@@ -47,8 +48,9 @@ public class DateFieldUnparsableDate extends TestBase {
 \r
         @Override\r
         protected Date handleUnparsableDateString(String dateString)\r
-                throws ConversionException {\r
-            throw new ConversionException("You should not enter invalid dates!");\r
+                throws Converter.ConversionException {\r
+            throw new Converter.ConversionException(\r
+                    "You should not enter invalid dates!");\r
         }\r
     }\r
 \r
@@ -59,11 +61,12 @@ public class DateFieldUnparsableDate extends TestBase {
 \r
         @Override\r
         protected Date handleUnparsableDateString(String dateString)\r
-                throws ConversionException {\r
+                throws Converter.ConversionException {\r
             if (dateString != null && dateString.equals("today")) {\r
                 return new Date();\r
             }\r
-            throw new ConversionException("You should not enter invalid dates!");\r
+            throw new Converter.ConversionException(\r
+                    "You should not enter invalid dates!");\r
         }\r
     }\r
 \r
index e8219b4b803375c267f12a883a499e00142da02f..ea4b57260495b8c08d987875e4a526db225a21ba 100644 (file)
@@ -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");
                 }
             }