From dc8e25cbe93579037affa9339c9d780a0148d9ff Mon Sep 17 00:00:00 2001 From: Manolo Carrasco Date: Mon, 13 Feb 2012 08:08:08 +0000 Subject: [PATCH] Throw a NullPointerException when casting null to Js objects. Fixes issue_122 --- .../com/google/gwt/query/client/Properties.java | 9 ++------- .../com/google/gwt/query/client/js/JsCache.java | 13 ++++++++++++- .../google/gwt/query/client/GQueryCoreTestGwt.java | 7 +++++++ 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/Properties.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/Properties.java index 6842e311..8b2793ff 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/Properties.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/Properties.java @@ -15,6 +15,7 @@ */ package com.google.gwt.query.client; +import com.google.gwt.core.client.GWT; import com.google.gwt.core.client.JavaScriptObject; import com.google.gwt.core.client.JsArrayMixed; import com.google.gwt.query.client.js.JsCache; @@ -137,10 +138,6 @@ public class Properties extends JavaScriptObject { } public final String toJsonString() { - // In dev-mode a null object casted to JavascriptObject does not throw a NPE - // e.g: System.out.println(((Properties)null).toJsonString()); - if (this == null) return "null"; - String ret = ""; for (String k : keys()){ String ky = k.matches("\\d+") ? k : "\"" + k + "\""; @@ -171,9 +168,6 @@ public class Properties extends JavaScriptObject { } public final String toQueryString() { - // In dev-mode a null object casted to JavascriptObject does not throw a NPE - if (this == null) return "null"; - String ret = ""; for (String k : keys()) { ret += ret.isEmpty() ? "" : "&"; @@ -206,4 +200,5 @@ public class Properties extends JavaScriptObject { public final boolean isEmpty(){ return c().length() == 0; } + } diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsCache.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsCache.java index bf64308d..ba7242a9 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsCache.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsCache.java @@ -15,6 +15,7 @@ */ package com.google.gwt.query.client.js; +import com.google.gwt.core.client.GWT; import com.google.gwt.core.client.JavaScriptObject; import com.google.gwt.core.client.JsArrayMixed; import com.google.gwt.core.client.JsArrayString; @@ -36,6 +37,7 @@ public class JsCache extends JavaScriptObject { }-*/; public final void pushAll(JavaScriptObject prevElem) { + checkNull(); JsCache c = prevElem.cast(); for (int i = 0, ilen = c.length(); i < ilen; i++) { put(length(), c.get(i)); @@ -101,7 +103,7 @@ public class JsCache extends JavaScriptObject { public final T getJavaScriptObject(Object name) { Object o = get(name); - return (o instanceof JavaScriptObject) ? ((JavaScriptObject)o).cast() : null; + return (o != null && o instanceof JavaScriptObject) ? ((JavaScriptObject)o).cast() : null; } public final native boolean isEmpty() /*-{ @@ -128,6 +130,7 @@ public class JsCache extends JavaScriptObject { }-*/; public final int[] indexes() { + checkNull(); JsArrayString a = keysImpl(); int[] ret = new int[a.length()]; for (int i = 0; i < a.length(); i++) { @@ -141,6 +144,7 @@ public class JsCache extends JavaScriptObject { } public final String[] keys() { + checkNull(); JsArrayString a = keysImpl(); String[] ret = new String[a.length()]; for (int i = 0; i < a.length(); i++) { @@ -167,6 +171,13 @@ public class JsCache extends JavaScriptObject { return ret + "}"; } + private void checkNull() { + // In dev-mode a null object casted to JavascriptObject does not throw a NPE + if (!GWT.isProdMode() && this == null) { + throw new NullPointerException(); + } + } + private final native JsArrayString keysImpl() /*-{ var key, keys=[]; // Chrome in DevMode injects a property to JS objects diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTestGwt.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTestGwt.java index 347adb1a..370e3a1a 100644 --- a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTestGwt.java +++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTestGwt.java @@ -547,6 +547,13 @@ public class GQueryCoreTestGwt extends GWTTestCase { p = $$("({border:'1px solid black'})"); assertEquals(1, p.keys().length); assertNotNull(p.getStr("border")); + + try { + // DevMode null casting return an object + ((Properties)null).toJsonString(); + fail("Executing methods of a null object should throw a NullPointerException"); + } catch (NullPointerException e) { + } } public void testRelativeMethods() { -- 2.39.5