From 96a0377cabada8eeed1ec157a11865ef6de4e0aa Mon Sep 17 00:00:00 2001 From: =?utf8?q?Manuel=20Carrasco=20Mo=C3=B1ino?= Date: Sun, 14 Apr 2013 20:06:49 +0200 Subject: [PATCH] Fix a NPE in certain callback cases. Add a couple of useful and predefined functions. --- .../client/plugins/deferred/Callbacks.java | 2 +- .../client/plugins/deferred/Deferred.java | 40 ++++++++++++++++++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/Callbacks.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/Callbacks.java index c6d36d72..23ee2254 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/Callbacks.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/Callbacks.java @@ -149,7 +149,7 @@ public class Callbacks { private boolean run(Object c, Object...o) { // Unbox array inside array when there is only an element. // It happens when running filters in Promise.then() - if (o.length == 1 && o[0].getClass().isArray()) { + if (o != null && o.length == 1 && o[0] != null && o[0].getClass().isArray()) { o = (Object[])o[0]; } if (c instanceof Callback) { diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/Deferred.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/Deferred.java index e0257426..c804784f 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/Deferred.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/Deferred.java @@ -87,10 +87,48 @@ public class Deferred implements Promise.Deferred { // run the function with the new args to resolve this deferred doIt.f(); } - } + } protected com.google.gwt.query.client.plugins.deferred.Deferred dfd; + /** + * Utility function which can be used inside classes extending this to + * resolve this deferred in a call to any other promise method. + * + * Example: + *
+     * new PromiseFunction() {
+     *   public void f(final Deferred dfd) {
+     *     anotherPromise.done( resolve );
+     *   }
+     * }
+     * 
+ */ + protected Function resolve = new Function() { + public void f() { + dfd.resolve(arguments); + }; + }; + + /** + * Utility function which can be used inside classes extending this to + * reject this deferred in a call to any other promise method. + * + * Example: + *
+     * new PromiseFunction() {
+     *   public void f(final Deferred dfd) {
+     *     anotherPromise.done( reject );
+     *   }
+     * }
+     * 
+ */ + protected Function reject = new Function() { + public void f() { + dfd.reject(arguments); + }; + }; + protected DeferredPromiseImpl() { dfd = new com.google.gwt.query.client.plugins.deferred.Deferred(); } -- 2.39.5