My favorites | Sign in
Google
                
Search
for
Updated Sep 09, 2008 by scottb@google.com
DevGuideMarshaling  
How Java objects appear to JavaScript code and vice-versa.

Sharing objects between Java source and JavaScript

Parameters and return types in JSNI methods are declared as Java types. There are very specific rules for how values passing in and out of JavaScript code must be treated. These rules must be followed whether the values enter and leave through normal method call semantics, or through the special syntax.

Passing Java values into JavaScript

Incoming Java type How it appears to JavaScript code
String JavaScript string, as in var s = "my string";
boolean JavaScript boolean value, as in var b = true;
long disallowed (see notes)
other numeric primitives JavaScript numeric value, as in var x = 42;
JavaScriptObject JavaScriptObject that must have originated from JavaScript code, typically as the return value of some other JSNI method (see notes)
Java array opaque value that can only be passed back into Java code
any other Java Object opaque value accessible through special syntax

Passing JavaScript values into Java code

Outgoing Java type What must be passed
String JavaScript string, as in return "boo";
boolean JavaScript boolean value, as in return false;
long disallowed (see notes)
Java numeric primitive JavaScript numeric value, as in return 19;
JavaScriptObject native JavaScript object, as in return document.createElement("div") (see notes)
any other Java Object (including arrays) Java Object of the correct type that must have originated in Java code; Java objects cannot be constructed from "thin air" in JavaScript

Important Notes

  • Violating any of these marshaling rules in hosted mode will generate a com.google.gwt.dev.shell.DevGuideHostedModeException detailing the problem. This exception is not translatable and never thrown in web mode.
  • JavaScriptObject gets special treatment from the GWT compiler and hosted browser. Its purpose is to provide an opaque representation of native JavaScript objects to Java code.

  • Java null and JavaScript null are identical and always legal values for any non-primitive Java type. JavaScript undefined is also considered equal to null when passed into Java code (the rules of JavaScript dictate that in JavaScript code, null == undefined is true but null === undefined is false). In previous versions of GWT, undefined was not a legal value to pass into Java.

Sign in to add a comment