summaryrefslogtreecommitdiffstats
path: root/src/com/vaadin/data/util
diff options
context:
space:
mode:
authorHenri Sara <hesara@vaadin.com>2011-12-18 11:53:27 +0200
committerHenri Sara <hesara@vaadin.com>2011-12-18 11:53:27 +0200
commitd62a07bd1bd0f0d0b14df87543004e8e8be41558 (patch)
tree1a36f092a710ac48b0908deea3d0327fe180c81f /src/com/vaadin/data/util
parent96b11d0213bc2012a72832feb5732b6ad1aacf1b (diff)
downloadvaadin-framework-d62a07bd1bd0f0d0b14df87543004e8e8be41558.tar.gz
vaadin-framework-d62a07bd1bd0f0d0b14df87543004e8e8be41558.zip
Remove conversions using String constructor from Properties (#8125).
ObjectProperty and MethodProperty no longer try to automatically convert values using a String constructor of the property type. Related fixes to tests.
Diffstat (limited to 'src/com/vaadin/data/util')
-rw-r--r--src/com/vaadin/data/util/MethodProperty.java49
-rw-r--r--src/com/vaadin/data/util/NestedMethodProperty.java10
-rw-r--r--src/com/vaadin/data/util/ObjectProperty.java43
3 files changed, 33 insertions, 69 deletions
diff --git a/src/com/vaadin/data/util/MethodProperty.java b/src/com/vaadin/data/util/MethodProperty.java
index 1ba84300b7..ec9a4e0c0a 100644
--- a/src/com/vaadin/data/util/MethodProperty.java
+++ b/src/com/vaadin/data/util/MethodProperty.java
@@ -5,7 +5,6 @@
package com.vaadin.data.util;
import java.io.IOException;
-import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.logging.Level;
@@ -630,9 +629,10 @@ public class MethodProperty<T> extends AbstractProperty<T> {
}
/**
- * Sets the value of the property. This method supports setting from
- * <code>String</code>s if either <code>String</code> is directly assignable
- * to property type, or the type class contains a string constructor.
+ * Sets the value of the property.
+ *
+ * Note that since Vaadin 7, no conversions are performed and the value must
+ * be of the correct type.
*
* @param newValue
* the New value of the property.
@@ -643,6 +643,7 @@ public class MethodProperty<T> extends AbstractProperty<T> {
* native type directly or through <code>String</code>.
* @see #invokeSetMethod(Object)
*/
+ @SuppressWarnings("unchecked")
public void setValue(Object newValue) throws Property.ReadOnlyException,
Property.ConversionException {
@@ -651,40 +652,14 @@ public class MethodProperty<T> extends AbstractProperty<T> {
throw new Property.ReadOnlyException();
}
- Object value = convertValue(newValue, type);
-
- invokeSetMethod(value);
- fireValueChange();
- }
-
- /**
- * Convert a value to the given type, using a constructor of the type that
- * takes a single String parameter (toString() for the value) if necessary.
- *
- * @param value
- * to convert
- * @param type
- * type into which the value should be converted
- * @return converted value
- */
- static Object convertValue(Object value, Class<?> type) {
- if (null == value || type.isAssignableFrom(value.getClass())) {
- return value;
+ // Checks the type of the value
+ if (newValue != null && !type.isAssignableFrom(newValue.getClass())) {
+ throw new Property.ConversionException(
+ "Invalid value type for ObjectProperty.");
}
- // convert using a string constructor
- try {
- // Gets the string constructor
- @SuppressWarnings("rawtypes")
- final Constructor constr = type
- .getConstructor(new Class[] { String.class });
-
- // Create a new object from the string
- return constr.newInstance(new Object[] { value.toString() });
-
- } catch (final java.lang.Exception e) {
- throw new Property.ConversionException(e);
- }
+ invokeSetMethod((T) newValue);
+ fireValueChange();
}
/**
@@ -693,7 +668,7 @@ public class MethodProperty<T> extends AbstractProperty<T> {
*
* @param value
*/
- protected void invokeSetMethod(Object value) {
+ protected void invokeSetMethod(T value) {
try {
// Construct a temporary argument array only if needed
diff --git a/src/com/vaadin/data/util/NestedMethodProperty.java b/src/com/vaadin/data/util/NestedMethodProperty.java
index b6748c9434..b8b6ad7101 100644
--- a/src/com/vaadin/data/util/NestedMethodProperty.java
+++ b/src/com/vaadin/data/util/NestedMethodProperty.java
@@ -213,9 +213,13 @@ public class NestedMethodProperty<T> extends AbstractProperty<T> {
throw new Property.ReadOnlyException();
}
- Object value = MethodProperty.convertValue(newValue, type);
+ // Checks the type of the value
+ if (newValue != null && !type.isAssignableFrom(newValue.getClass())) {
+ throw new Property.ConversionException(
+ "Invalid value type for NestedMethodProperty.");
+ }
- invokeSetMethod(value);
+ invokeSetMethod((T) newValue);
fireValueChange();
}
@@ -225,7 +229,7 @@ public class NestedMethodProperty<T> extends AbstractProperty<T> {
*
* @param value
*/
- protected void invokeSetMethod(Object value) {
+ protected void invokeSetMethod(T value) {
try {
Object object = instance;
for (int i = 0; i < getMethods.size() - 1; i++) {
diff --git a/src/com/vaadin/data/util/ObjectProperty.java b/src/com/vaadin/data/util/ObjectProperty.java
index 2bc1daad76..79e5436cc5 100644
--- a/src/com/vaadin/data/util/ObjectProperty.java
+++ b/src/com/vaadin/data/util/ObjectProperty.java
@@ -4,8 +4,6 @@
package com.vaadin.data.util;
-import java.lang.reflect.Constructor;
-
import com.vaadin.data.Property;
/**
@@ -107,18 +105,19 @@ public class ObjectProperty<T> extends AbstractProperty<T> {
}
/**
- * Sets the value of the property. This method supports setting from
- * <code>String</code> if either <code>String</code> is directly assignable
- * to property type, or the type class contains a string constructor.
+ * Sets the value of the property.
+ *
+ * Note that since Vaadin 7, no conversions are performed and the value must
+ * be of the correct type.
*
* @param newValue
* 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 can't
- * be converted into the Property's native type directly or through
- * <code>String</code>
+ * @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 {
@@ -127,29 +126,15 @@ public class ObjectProperty<T> extends AbstractProperty<T> {
throw new Property.ReadOnlyException();
}
- // Tries to assign the compatible value directly
- if (newValue == null || type.isAssignableFrom(newValue.getClass())) {
- @SuppressWarnings("unchecked")
- // the cast is safe after an isAssignableFrom check
- T value = (T) newValue;
- this.value = value;
- } else {
- try {
-
- // Gets the string constructor
- final Constructor<T> constr = getType().getConstructor(
- new Class[] { String.class });
-
- // Creates new object from the string
- value = constr
- .newInstance(new Object[] { newValue.toString() });
-
- } catch (final java.lang.Exception e) {
- throw new Property.ConversionException(e);
- }
+ // Checks the type of the value
+ if (newValue != null && !type.isAssignableFrom(newValue.getClass())) {
+ throw new Property.ConversionException(
+ "Invalid value type for ObjectProperty.");
}
+ // the cast is safe after an isAssignableFrom check
+ this.value = (T) newValue;
+
fireValueChange();
}
-
}