summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIlia Motornyi <elmot@vaadin.com>2017-02-23 16:06:51 +0200
committerHenri Sara <henri.sara@gmail.com>2017-02-23 16:06:51 +0200
commit23c3f22661a1919e5dc620518ce0ece098e2ce1b (patch)
tree60b54fbc62752b80ff89684528ac4b01cbc31bef
parentc49170acd9578460310537dfb411cdb5c877f57b (diff)
downloadvaadin-framework-23c3f22661a1919e5dc620518ce0ece098e2ce1b.tar.gz
vaadin-framework-23c3f22661a1919e5dc620518ce0ece098e2ce1b.zip
Fix some issues with Binder serialization (#8660)
Partial fix for #8640
-rw-r--r--server/src/main/java/com/vaadin/data/BeanPropertySet.java13
-rw-r--r--server/src/test/java/com/vaadin/data/BeanBinderTest.java10
-rw-r--r--server/src/test/java/com/vaadin/data/BinderConverterValidatorTest.java3
-rw-r--r--server/src/test/java/com/vaadin/data/BinderMultiSelectTest.java2
-rw-r--r--server/src/test/java/com/vaadin/data/BinderTestBase.java19
-rw-r--r--server/src/test/java/com/vaadin/tests/data/bean/BeanToValidate.java3
-rw-r--r--server/src/test/java/com/vaadin/tests/data/bean/BeanWithEnums.java3
-rw-r--r--server/src/test/java/com/vaadin/tests/data/bean/Person.java3
8 files changed, 44 insertions, 12 deletions
diff --git a/server/src/main/java/com/vaadin/data/BeanPropertySet.java b/server/src/main/java/com/vaadin/data/BeanPropertySet.java
index 073e9ce62f..0b1bfa5bf1 100644
--- a/server/src/main/java/com/vaadin/data/BeanPropertySet.java
+++ b/server/src/main/java/com/vaadin/data/BeanPropertySet.java
@@ -134,13 +134,18 @@ public class BeanPropertySet<T> implements PropertySet<T> {
@Override
public Optional<Setter<T, V>> getSetter() {
- Method setter = descriptor.getWriteMethod();
- if (setter == null) {
+ if (descriptor.getWriteMethod() == null) {
return Optional.empty();
}
- return Optional.of(
- (bean, value) -> invokeWrapExceptions(setter, bean, value));
+ Setter<T, V> setter = (bean, value) -> {
+ // Do not "optimize" this getter call,
+ // if its done outside the code block, that will produce
+ // NotSerializableException because of some lambda compilation magic
+ Method innerSetter = descriptor.getWriteMethod();
+ invokeWrapExceptions(innerSetter, bean, value);
+ };
+ return Optional.of(setter);
}
@SuppressWarnings("unchecked")
diff --git a/server/src/test/java/com/vaadin/data/BeanBinderTest.java b/server/src/test/java/com/vaadin/data/BeanBinderTest.java
index 652c406073..810f7e4e48 100644
--- a/server/src/test/java/com/vaadin/data/BeanBinderTest.java
+++ b/server/src/test/java/com/vaadin/data/BeanBinderTest.java
@@ -3,6 +3,7 @@ package com.vaadin.data;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
+import java.io.Serializable;
import java.util.List;
import java.util.Set;
@@ -32,7 +33,7 @@ public class BeanBinderTest
private TextField number = new TextField();
}
- private class TestBean {
+ private static class TestBean implements Serializable{
private Set<TestEnum> enums;
private int number;
@@ -53,7 +54,7 @@ public class BeanBinderTest
}
}
- public class RequiredConstraints {
+ public static class RequiredConstraints implements Serializable{
@NotNull
@Max(10)
private String firstname;
@@ -107,6 +108,7 @@ public class BeanBinderTest
// Should correctly bind the enum field without throwing
otherBinder.bindInstanceFields(testClass);
+ testSerialization(otherBinder);
}
@Test
@@ -122,6 +124,7 @@ public class BeanBinderTest
otherBinder.setBean(bean);
testClass.number.setValue("50");
assertEquals(50, bean.number);
+ testSerialization(otherBinder);
}
@Test(expected = IllegalStateException.class)
@@ -304,6 +307,7 @@ public class BeanBinderTest
binder.setBean(bean);
Assert.assertTrue(field.isRequiredIndicatorVisible());
+ testSerialization(binder);
}
@Test
@@ -317,6 +321,7 @@ public class BeanBinderTest
binder.setBean(bean);
Assert.assertTrue(field.isRequiredIndicatorVisible());
+ testSerialization(binder);
}
@Test
@@ -330,6 +335,7 @@ public class BeanBinderTest
binder.setBean(bean);
Assert.assertTrue(field.isRequiredIndicatorVisible());
+ testSerialization(binder);
}
private void assertInvalid(HasValue<?> field, String message) {
diff --git a/server/src/test/java/com/vaadin/data/BinderConverterValidatorTest.java b/server/src/test/java/com/vaadin/data/BinderConverterValidatorTest.java
index 64bde4d772..3ab4e962a0 100644
--- a/server/src/test/java/com/vaadin/data/BinderConverterValidatorTest.java
+++ b/server/src/test/java/com/vaadin/data/BinderConverterValidatorTest.java
@@ -21,6 +21,7 @@ import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
+import java.io.Serializable;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
@@ -43,7 +44,7 @@ import com.vaadin.ui.TextField;
public class BinderConverterValidatorTest
extends BinderTestBase<Binder<Person>, Person> {
- private static class StatusBean {
+ private static class StatusBean implements Serializable {
private String status;
public String getStatus() {
diff --git a/server/src/test/java/com/vaadin/data/BinderMultiSelectTest.java b/server/src/test/java/com/vaadin/data/BinderMultiSelectTest.java
index 78a132b04d..ec4737a2b4 100644
--- a/server/src/test/java/com/vaadin/data/BinderMultiSelectTest.java
+++ b/server/src/test/java/com/vaadin/data/BinderMultiSelectTest.java
@@ -34,7 +34,7 @@ import com.vaadin.ui.CheckBoxGroup;
public class BinderMultiSelectTest
extends BinderTestBase<Binder<BeanWithEnums>, BeanWithEnums> {
- public class TestEnumSetToStringConverter
+ public static class TestEnumSetToStringConverter
implements Converter<Set<TestEnum>, String> {
@Override
public Result<String> convertToModel(Set<TestEnum> value,
diff --git a/server/src/test/java/com/vaadin/data/BinderTestBase.java b/server/src/test/java/com/vaadin/data/BinderTestBase.java
index f3f04982d3..92edd734eb 100644
--- a/server/src/test/java/com/vaadin/data/BinderTestBase.java
+++ b/server/src/test/java/com/vaadin/data/BinderTestBase.java
@@ -15,8 +15,12 @@
*/
package com.vaadin.data;
+import java.io.ByteArrayOutputStream;
+import java.io.ObjectOutputStream;
+import java.io.Serializable;
import java.util.Locale;
+import org.junit.After;
import org.junit.Before;
import com.vaadin.ui.TextField;
@@ -28,7 +32,7 @@ import com.vaadin.ui.TextField;
*
* @since 8.0
*/
-public abstract class BinderTestBase<BINDER extends Binder<ITEM>, ITEM> {
+public abstract class BinderTestBase<BINDER extends Binder<ITEM>, ITEM> implements Serializable {
protected static final String NEGATIVE_ERROR_MESSAGE = "Value must be non-negative";
@@ -50,6 +54,14 @@ public abstract class BinderTestBase<BINDER extends Binder<ITEM>, ITEM> {
protected Validator<Integer> notNegative = Validator.from(x -> x >= 0,
NEGATIVE_ERROR_MESSAGE);
+ public static void testSerialization(Object toSerialize) {
+ try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(new ByteArrayOutputStream())) {
+ objectOutputStream.writeObject(toSerialize);
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+
@Before
public void setUpBase() {
nameField = new TextField();
@@ -57,4 +69,9 @@ public abstract class BinderTestBase<BINDER extends Binder<ITEM>, ITEM> {
ageField = new TextField();
ageField.setLocale(Locale.US);
}
+
+ @After
+ public void testBinderSerialization() {
+ testSerialization(binder);
+ }
}
diff --git a/server/src/test/java/com/vaadin/tests/data/bean/BeanToValidate.java b/server/src/test/java/com/vaadin/tests/data/bean/BeanToValidate.java
index 591debe9ba..66637c7cbc 100644
--- a/server/src/test/java/com/vaadin/tests/data/bean/BeanToValidate.java
+++ b/server/src/test/java/com/vaadin/tests/data/bean/BeanToValidate.java
@@ -1,5 +1,6 @@
package com.vaadin.tests.data.bean;
+import java.io.Serializable;
import java.util.Calendar;
import javax.validation.Valid;
@@ -11,7 +12,7 @@ import javax.validation.constraints.Past;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
-public class BeanToValidate {
+public class BeanToValidate implements Serializable {
@NotNull
@Size(min = 3, max = 16)
diff --git a/server/src/test/java/com/vaadin/tests/data/bean/BeanWithEnums.java b/server/src/test/java/com/vaadin/tests/data/bean/BeanWithEnums.java
index b0b612c200..7aa09f0830 100644
--- a/server/src/test/java/com/vaadin/tests/data/bean/BeanWithEnums.java
+++ b/server/src/test/java/com/vaadin/tests/data/bean/BeanWithEnums.java
@@ -15,10 +15,11 @@
*/
package com.vaadin.tests.data.bean;
+import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
-public class BeanWithEnums {
+public class BeanWithEnums implements Serializable{
private Set<TestEnum> enums = new HashSet<>();
public Set<TestEnum> getEnums() {
diff --git a/server/src/test/java/com/vaadin/tests/data/bean/Person.java b/server/src/test/java/com/vaadin/tests/data/bean/Person.java
index 861006318f..b3615b819d 100644
--- a/server/src/test/java/com/vaadin/tests/data/bean/Person.java
+++ b/server/src/test/java/com/vaadin/tests/data/bean/Person.java
@@ -1,9 +1,10 @@
package com.vaadin.tests.data.bean;
+import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDate;
-public class Person {
+public class Person implements Serializable {
private String firstName;
private String lastName;
private String email;