Browse Source

#8125 Removed Property.ConversionException and String constructor based

conversion from Property classes
tags/7.0.0.alpha1
Artur Signell 12 years ago
parent
commit
7e594fb40f

+ 1
- 61
src/com/vaadin/data/Property.java View 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.

+ 3
- 19
src/com/vaadin/data/util/IndexedContainer.java View 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

+ 2
- 3
src/com/vaadin/data/util/MethodProperty.java View 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.");
}


+ 2
- 3
src/com/vaadin/data/util/NestedMethodProperty.java View 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.");
}


+ 2
- 5
src/com/vaadin/data/util/ObjectProperty.java View 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.");
}


+ 2
- 5
src/com/vaadin/data/util/PropertyFormatter.java View 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);
}
}
}

+ 1
- 2
src/com/vaadin/data/util/TransactionalPropertyWrapper.java View File

@@ -37,8 +37,7 @@ public class TransactionalPropertyWrapper<T> extends AbstractProperty<T>
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);

+ 3
- 15
src/com/vaadin/data/util/sqlcontainer/ColumnProperty.java View 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");
}

/*

+ 5
- 6
src/com/vaadin/ui/AbstractField.java View 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()));
}
}

+ 5
- 13
src/com/vaadin/ui/AbstractSelect.java View 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) {

+ 1
- 2
src/com/vaadin/ui/AbstractTextField.java View 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

+ 6
- 22
src/com/vaadin/ui/DateField.java View 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);
}

/**

+ 1
- 2
src/com/vaadin/ui/Slider.java View 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

+ 1
- 2
tests/server-side/com/vaadin/data/util/filter/AbstractFilterTest.java View 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();
}


+ 1
- 1
tests/server-side/com/vaadin/tests/server/component/abstractfield/AbstractFieldValueConversions.java View File

@@ -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
}
}

+ 1
- 2
tests/testbench/com/vaadin/tests/components/customfield/AddressField.java View File

@@ -48,8 +48,7 @@ public class AddressField extends CustomField<Address> {
}
@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();

+ 10
- 7
tests/testbench/com/vaadin/tests/components/datefield/DateFieldUnparsableDate.java View File

@@ -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!");
}
}

+ 3
- 3
tests/testbench/com/vaadin/tests/components/textfield/TextFieldWithPropertyFormatter.java View 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");
}
}


Loading…
Cancel
Save