diff options
author | Artur <artur@vaadin.com> | 2017-07-26 13:17:44 +0300 |
---|---|---|
committer | Henri Sara <henri.sara@gmail.com> | 2017-07-26 13:17:44 +0300 |
commit | f38da154c1e6f5706c95e7bbc1468d47a0a65104 (patch) | |
tree | 054b5e3ea9491e5540a7233e49f334098c45aede | |
parent | eee5d3a25891579a1b2013a101ca7d185bc174ac (diff) | |
download | vaadin-framework-f38da154c1e6f5706c95e7bbc1468d47a0a65104.tar.gz vaadin-framework-f38da154c1e6f5706c95e7bbc1468d47a0a65104.zip |
Make JavaScriptExtension serializable (#9721)
Fixes #9705
-rw-r--r-- | compatibility-server/src/test/java/com/vaadin/tests/server/SerializationTest.java | 62 | ||||
-rw-r--r-- | server/src/main/java/com/vaadin/server/JavaScriptCallbackHelper.java | 17 |
2 files changed, 73 insertions, 6 deletions
diff --git a/compatibility-server/src/test/java/com/vaadin/tests/server/SerializationTest.java b/compatibility-server/src/test/java/com/vaadin/tests/server/SerializationTest.java new file mode 100644 index 0000000000..eb852974fc --- /dev/null +++ b/compatibility-server/src/test/java/com/vaadin/tests/server/SerializationTest.java @@ -0,0 +1,62 @@ +package com.vaadin.tests.server; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.Serializable; + +import org.junit.Test; + +import com.vaadin.server.AbstractJavaScriptExtension; +import com.vaadin.ui.JavaScriptFunction; + +import elemental.json.JsonArray; + +public class SerializationTest { + + public static class JSE extends AbstractJavaScriptExtension { + { + addFunction("foo", new JavaScriptFunction() { + @Override + public void call(JsonArray arguments) { + System.out.println("Foo called"); + } + }); + } + + } + + @Test + public void testJSExtension() throws Exception { + serializeAndDeserialize(new JSE()); + } + + private static <S extends Serializable> S serializeAndDeserialize(S s) + throws IOException, ClassNotFoundException { + // Serialize and deserialize + + ByteArrayOutputStream bs = new ByteArrayOutputStream(); + ObjectOutputStream out = new ObjectOutputStream(bs); + out.writeObject(s); + byte[] data = bs.toByteArray(); + ObjectInputStream in = new ObjectInputStream( + new ByteArrayInputStream(data)); + @SuppressWarnings("unchecked") + S s2 = (S) in.readObject(); + + if (s.equals(s2)) { + System.out.println(s + " equals " + s2); + } else { + System.out.println(s + " does NOT equal " + s2); + } + + return s2; + } + + private static String toString(Object o) { + return String.valueOf(o); + } + +} diff --git a/server/src/main/java/com/vaadin/server/JavaScriptCallbackHelper.java b/server/src/main/java/com/vaadin/server/JavaScriptCallbackHelper.java index 7e228bb7ca..7765764b47 100644 --- a/server/src/main/java/com/vaadin/server/JavaScriptCallbackHelper.java +++ b/server/src/main/java/com/vaadin/server/JavaScriptCallbackHelper.java @@ -72,12 +72,17 @@ public class JavaScriptCallbackHelper implements Serializable { private void ensureRpc() { if (javascriptCallbackRpc == null) { - javascriptCallbackRpc = (String name, JsonArray arguments) -> { - JavaScriptFunction callback = callbacks.get(name); - try { - callback.call(arguments); - } catch (JsonException e) { - throw new IllegalArgumentException(e); + // Note that javascriptCallbackRpc is not a lambda to make sure it + // can be serialized properly + javascriptCallbackRpc = new JavaScriptCallbackRpc() { + @Override + public void call(String name, JsonArray arguments) { + JavaScriptFunction callback = callbacks.get(name); + try { + callback.call(arguments); + } catch (JsonException e) { + throw new IllegalArgumentException(e); + } } }; connector.registerRpc(javascriptCallbackRpc); |