Home » GWT JSNI

GWT JavaScript Native Interface (JSNI)

GWT JSNI is used to solve problems such as, when we need to integrate GWT with existing handwritten JavaScript or with a third-party JavaScript library. Occasionally, we need to access low-level browser functionality which is not exposed by the GWT class API’s.

JSNI is web equivalent of inline assembly code and can use in many ways such as:

  • Implement a Java method directly in JavaScript.
  • Wrap type-safe Java method signatures around existing JavaScript.
  • Call from JavaScript code into Java code and vice-versa.
  • Throw exceptions across Java/JavaScript boundaries.
  • Read and write Java fields from JavaScript.
  • Use development mode to debug both Java source (with a Java debugger) and JavaScript (with a script debugger).

Declaring a native method

Syntax

Calling JSNI from Java

Calling a JSNI method from Java is no different than calling a regular Java method.

Example

The caller can’t really tell if the method is native or not. This gives some flexibility in changing about how the method is implemented.

Calling Java from JSNI

It is more complex than calling JSNI from Java. For example, suppose we pass an object to a JSNI method and need to access a field or call a method in that object. We need to know how the GWT compiler manages the Java field and method names. It helps to access fields from your own JavaScript code.

Accessing Java fields

Syntax

where:

obj: It  is the object instance being referenced. For static variables, leave off the instance expression and the trailing period.
class : It is the fully-qualified name of the class in which the field is declared (or a subclass thereof).
field: It is the name of the field being accessed.

Invoking Java methods

Calling methods uses syntax similar to accessing fields, except also supply the signature of the method calling. Java methods can be overloaded, i.e., two methods can have the same name but take different parameters.

Syntax

where:

obj : It is the object instance being referenced. For static methods,omit the instance expression and the trailing period.
class: In this the method is declared (or a subclass thereof).
method : It is the name of the method being called.

GWT JSNI Method signatures

JSNI method signatures are exactly the same as JNI method signatures except that the method return type is left off. The following table shows type signatures:

Type Signature Java Type
Z Boolean
B Byte
C Char
S Short
I Int
J Long
F Float
D Double

GWT JSNI Example

 It demonstrates passing numbers, strings, booleans, and Java objects into JavaScript. It also shows how a JavaScript method can make a method call on a Java object that was passed in.

The following table contains datatypes and representation of type in JSNI code.

Java Type JavaScript Code Display
Java Numeric JavaScript Numeric Value, int var x= 5.
String JavaScript String Value var x=”tutoraspire”.
Boolean JavaScript Boolean Value var x= true.
JavaScriptObject JavaScriptObject which must have originated from JavaScript code. It should be the return value of some other JSNI method.
Java array An opaque value that can only be passed back into Java Code.
Java Object An opaque value that can be accessed through special syntax.
Next Topic#

You may also like