Manuel Carrasco Moñino has uploaded a new change for review.
https://gwt-review.googlesource.com/2961
Change subject: RequestFactory: Enabling support for interfaces in
@ProxyFor.
......................................................................
RequestFactory: Enabling support for interfaces in @ProxyFor.
Fixes issues #7509 and #5762.
Patch moved from rietveld (http://gwt-code-reviews.appspot.com/1764804/)
RequestFactory should allow that client-proxies point to interfaces in
server side, facilitating different implementations of the object.
This feature allows as well that we could reuse proxy interfaces in both
sides, what reduces the code and allows to use the same value class in both
sides, example:
@ProxyFor(value = A.class, locator = ALocator.class)
public interface A {
String getSomething();
void setSomething(String s);
}
public class ALocator extends Locator<A, String> {
@Override
public A create(Class<? extends A> clazz) {
return new AImpl();
}
....
}
public class AImpl implements A {
String getSomething(){return null}
void setSomething(String s){}
}
Change-Id: I273ed2c403b275f4c9bec2b7e7ba72a3d05930b9
---
M requestfactory/build.xml
M
user/src/com/google/web/bindery/requestfactory/server/ResolverServiceLayer.java
M
user/test/com/google/web/bindery/requestfactory/gwt/RequestFactorySuite.java
A
user/test/com/google/web/bindery/requestfactory/gwt/client/ProxyForInterfacesTest.java
A
user/test/com/google/web/bindery/requestfactory/server/ProxyForInterfacesJreTest.java
M
user/test/com/google/web/bindery/requestfactory/vm/RequestFactoryJreSuite.java
6 files changed, 214 insertions(+), 15 deletions(-)
diff --git a/requestfactory/build.xml b/requestfactory/build.xml
index 9215bfe..25414ea 100755
--- a/requestfactory/build.xml
+++ b/requestfactory/build.xml
@@ -119,6 +119,7 @@
<arg path="${gwt.build.lib}/requestfactory-test-validated.jar" />
<arg
value="com.google.web.bindery.requestfactory.gwt.client.RequestFactoryChainedContextTest.Factory"
/>
<arg
value="com.google.web.bindery.requestfactory.gwt.client.RequestFactoryPolymorphicTest.Factory"
/>
+ <arg
value="com.google.web.bindery.requestfactory.gwt.client.ProxyForInterfacesTest.Factory"
/>
<arg
value="com.google.web.bindery.requestfactory.shared.BoxesAndPrimitivesTest.Factory"
/>
<arg
value="com.google.web.bindery.requestfactory.shared.ComplexKeysTest.Factory"
/>
<arg
value="com.google.web.bindery.requestfactory.shared.LocatorTest.Factory" />
diff --git
a/user/src/com/google/web/bindery/requestfactory/server/ResolverServiceLayer.java
b/user/src/com/google/web/bindery/requestfactory/server/ResolverServiceLayer.java
index 43b1b82..ac5a09a 100644
---
a/user/src/com/google/web/bindery/requestfactory/server/ResolverServiceLayer.java
+++
b/user/src/com/google/web/bindery/requestfactory/server/ResolverServiceLayer.java
@@ -74,23 +74,12 @@
if (TypeUtils.isValueType(domainClass)) {
return domainClass.asSubclass(clientClass);
}
- Class<?> toSearch = domainClass;
- while (toSearch != null) {
- List<String> clientTypes =
deobfuscator.getClientProxies(toSearch.getName());
- if (clientTypes != null) {
- for (String clientType : clientTypes) {
- Class<?> proxy = forName(clientType);
- if (clientClass.isAssignableFrom(proxy)) {
- return proxy.asSubclass(clientClass);
- }
- }
- }
- toSearch = toSearch.getSuperclass();
- }
- if (required) {
+
+ Class<? extends T> ret = resolveClientType(domainClass, clientClass);
+ if (ret == null && required) {
die(null, "The domain type %s cannot be sent to the client",
domainClass.getCanonicalName());
}
- return null;
+ return ret;
}
@Override
@@ -257,4 +246,29 @@
}
return params.toArray(new Class<?>[params.size()]);
}
+
+ private <T> Class<? extends T> resolveClientType(Class<?> domainClass,
Class<T> clientClass) {
+ if (domainClass == null) {
+ return null;
+ }
+
+ List<String> clientTypes =
deobfuscator.getClientProxies(domainClass.getName());
+ if (clientTypes != null) {
+ for (String clientType : clientTypes) {
+ Class<?> proxy = forName(clientType);
+ if (clientClass.isAssignableFrom(proxy)) {
+ return proxy.asSubclass(clientClass);
+ }
+ }
+ }
+
+ for (Class<?> toSearch : domainClass.getInterfaces()) {
+ Class<? extends T> ret = resolveClientType(toSearch, clientClass);
+ if (ret != null) {
+ return ret;
+ }
+ }
+
+ return resolveClientType(domainClass.getSuperclass(), clientClass);
+ }
}
diff --git
a/user/test/com/google/web/bindery/requestfactory/gwt/RequestFactorySuite.java
b/user/test/com/google/web/bindery/requestfactory/gwt/RequestFactorySuite.java
index 0d4db18..ae45216 100644
---
a/user/test/com/google/web/bindery/requestfactory/gwt/RequestFactorySuite.java
+++
b/user/test/com/google/web/bindery/requestfactory/gwt/RequestFactorySuite.java
@@ -24,6 +24,7 @@
import
com.google.web.bindery.requestfactory.gwt.client.RequestFactoryPolymorphicTest;
import com.google.web.bindery.requestfactory.gwt.client.RequestFactoryTest;
import
com.google.web.bindery.requestfactory.gwt.client.RequestFactoryUnicodeEscapingTest;
+import
com.google.web.bindery.requestfactory.gwt.client.ProxyForInterfacesTest;
import com.google.web.bindery.requestfactory.gwt.client.ui.EditorTest;
import com.google.web.bindery.requestfactory.shared.BoxesAndPrimitivesTest;
import com.google.web.bindery.requestfactory.shared.ComplexKeysTest;
@@ -58,6 +59,7 @@
suite.addTestSuite(RequestFactoryUnicodeEscapingTest.class);
suite.addTestSuite(RequestPayloadTest.class);
suite.addTestSuite(ServiceInheritanceTest.class);
+ suite.addTestSuite(ProxyForInterfacesTest.class);
return suite;
}
}
diff --git
a/user/test/com/google/web/bindery/requestfactory/gwt/client/ProxyForInterfacesTest.java
b/user/test/com/google/web/bindery/requestfactory/gwt/client/ProxyForInterfacesTest.java
new file mode 100644
index 0000000..b8dea3c
--- /dev/null
+++
b/user/test/com/google/web/bindery/requestfactory/gwt/client/ProxyForInterfacesTest.java
@@ -0,0 +1,147 @@
+/*
+ * Copyright 2012 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may
not use this file except
+ * in compliance with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
distributed under the License
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
ANY KIND, either express
+ * or implied. See the License for the specific language governing
permissions and limitations under
+ * the License.
+ */
+package com.google.web.bindery.requestfactory.gwt.client;
+
+import com.google.gwt.core.client.GWT;
+import com.google.gwt.junit.client.GWTTestCase;
+import com.google.web.bindery.event.shared.SimpleEventBus;
+import com.google.web.bindery.requestfactory.shared.ProxyFor;
+import com.google.web.bindery.requestfactory.shared.Receiver;
+import com.google.web.bindery.requestfactory.shared.Request;
+import com.google.web.bindery.requestfactory.shared.RequestContext;
+import com.google.web.bindery.requestfactory.shared.RequestFactory;
+import com.google.web.bindery.requestfactory.shared.Service;
+import com.google.web.bindery.requestfactory.shared.ValueProxy;
+
+/**
+ * Tests RequestFactory when using proxies for interfaces.
+ * @see http://code.google.com/p/google-web-toolkit/issues/detail?id=5762
+ * @see http://code.google.com/p/google-web-toolkit/issues/detail?id=7509
+ */
+public class ProxyForInterfacesTest extends GWTTestCase {
+
+ protected interface Factory extends RequestFactory {
+ Context ctx();
+ }
+
+ static class C0 implements I1 {
+ public String getName() {
+ return "C0";
+ }
+ }
+
+ static class C1 implements I2 {
+ public String getName() {
+ return "C1";
+ }
+ }
+
+ static class C2 extends C1 {
+ public String getName() {
+ return "C2";
+ }
+ }
+
+ @Service(ContextImpl.class)
+ static interface Context extends RequestContext {
+ Request<I1> getC0();
+
+ Request<I1> getC1_RetI1();
+
+ Request<I2> getC1_RetI2();
+
+ Request<I1> getC2_RetI1();
+
+ Request<I2> getC2_RetI2();
+ }
+
+ static class ContextImpl {
+ public static I1 getC0() {
+ return new C0();
+ }
+
+ public static I1 getC1_RetI1() {
+ return new C1();
+ }
+
+ public static I2 getC1_RetI2() {
+ return new C1();
+ }
+
+ public static I1 getC2_RetI1() {
+ return new C2();
+ }
+
+ public static I2 getC2_RetI2() {
+ return new C2();
+ }
+ }
+
+ @ProxyFor(I1.class)
+ static interface I1 extends ValueProxy {
+ String getName();
+ }
+
+ @ProxyFor(I2.class)
+ static interface I2 extends I1, ValueProxy {
+ }
+
+ class ReceiverAssert<T extends I1> extends Receiver<T> {
+ String name;
+
+ public ReceiverAssert(String name) {
+ this.name = name;
+ }
+
+ @Override
+ public void onSuccess(T response) {
+ assertEquals(name, response.getName());
+ }
+ }
+
+ private static final int TEST_DELAY = 5000;
+
+ protected Factory factory;
+
+ @Override
+ public String getModuleName() {
+ return "com.google.web.bindery.requestfactory.gwt.RequestFactorySuite";
+ }
+
+ public void testProxyForInterfaces() {
+ delayTestFinish(TEST_DELAY);
+ Context ctx = factory.ctx();
+ ctx.getC0().to(new ReceiverAssert<I1>("C0"));
+ ctx.getC1_RetI1().to(new ReceiverAssert<I1>("C1"));
+ ctx.getC2_RetI1().to(new ReceiverAssert<I1>("C2"));
+ ctx.getC1_RetI2().to(new ReceiverAssert<I2>("C1"));
+ ctx.getC2_RetI2().to(new ReceiverAssert<I2>("C2"));
+ ctx.fire(new Receiver<Void>() {
+ public void onSuccess(Void response) {
+ finishTest();
+ }
+ });
+ }
+
+ protected Factory createFactory() {
+ Factory f = GWT.create(Factory.class);
+ f.initialize(new SimpleEventBus());
+ return f;
+ }
+
+ @Override
+ protected void gwtSetUp() throws Exception {
+ factory = createFactory();
+ }
+}
diff --git
a/user/test/com/google/web/bindery/requestfactory/server/ProxyForInterfacesJreTest.java
b/user/test/com/google/web/bindery/requestfactory/server/ProxyForInterfacesJreTest.java
new file mode 100644
index 0000000..827eb23
--- /dev/null
+++
b/user/test/com/google/web/bindery/requestfactory/server/ProxyForInterfacesJreTest.java
@@ -0,0 +1,33 @@
+/*
+ * Copyright 2012 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may
not use this file except
+ * in compliance with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
distributed under the License
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
ANY KIND, either express
+ * or implied. See the License for the specific language governing
permissions and limitations under
+ * the License.
+ */
+package com.google.web.bindery.requestfactory.server;
+
+import
com.google.web.bindery.requestfactory.gwt.client.ProxyForInterfacesTest;
+
+/**
+ * JRE version of {@link ProxyForInterfacesTest}.
+ */
+public class ProxyForInterfacesJreTest extends ProxyForInterfacesTest {
+
+ @Override
+ public String getModuleName() {
+ return null;
+ }
+
+ @Override
+ protected Factory createFactory() {
+ return RequestFactoryJreTest.createInProcess(Factory.class);
+ }
+
+}
diff --git
a/user/test/com/google/web/bindery/requestfactory/vm/RequestFactoryJreSuite.java
b/user/test/com/google/web/bindery/requestfactory/vm/RequestFactoryJreSuite.java
index e928579..f4192ba 100644
---
a/user/test/com/google/web/bindery/requestfactory/vm/RequestFactoryJreSuite.java
+++
b/user/test/com/google/web/bindery/requestfactory/vm/RequestFactoryJreSuite.java
@@ -26,6 +26,7 @@
import com.google.web.bindery.requestfactory.server.RequestFactoryJreTest;
import
com.google.web.bindery.requestfactory.server.RequestFactoryPolymorphicJreTest;
import
com.google.web.bindery.requestfactory.server.RequestFactoryUnicodeEscapingJreTest;
+import
com.google.web.bindery.requestfactory.server.ProxyForInterfacesJreTest;
import com.google.web.bindery.requestfactory.server.RequestPayloadJreTest;
import
com.google.web.bindery.requestfactory.server.ServiceInheritanceJreTest;
import com.google.web.bindery.requestfactory.server.ServiceLocatorTest;
@@ -58,6 +59,7 @@
suite.addTestSuite(ServiceInheritanceJreTest.class);
suite.addTestSuite(ServiceLocatorTest.class);
suite.addTestSuite(SimpleEntityProxyIdTest.class);
+ suite.addTestSuite(ProxyForInterfacesJreTest.class);
return suite;
}
--
To view, visit https://gwt-review.googlesource.com/2961
To unsubscribe, visit https://gwt-review.googlesource.com/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I273ed2c403b275f4c9bec2b7e7ba72a3d05930b9
Gerrit-PatchSet: 1
Gerrit-Project: gwt
Gerrit-Branch: master
Gerrit-Owner: Manuel Carrasco Moñino <[email protected]>
--
http://groups.google.com/group/Google-Web-Toolkit-Contributors
---
You received this message because you are subscribed to the Google Groups "GWT Contributors" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.