Sunday, July 17, 2011

GWT JSNI Reference

I've recently had to go through the GWT JSNI interface, and I found the documentation a little lacking.  While the official JSNI page has nice examples and clearly shows how things integrate, it leaves out some reference information that would be helpful.  My guess is that they didn't want to duplicate the JNI reference, but its still a little frustrating to have to look up the type codes in a separate area.  And the type codes aren't exactly intuitive, I mean, who would guess that 'Z' is the representation for boolean?

So, here is a little cheat sheet for those of you looking for the GWT JSNI type codes.  These are, of course, the same codes that can be found in the JNI reference.

Native Type Java Language Type Description Type signature
unsigned char jboolean unsigned 8 bits Z
signed char jbyte signed 8 bits B
unsigned short jchar unsigned 16 bits C
short jshort signed 16 bits S
long jint signed 32 bits I

long long

__int64
jlong signed 64 bits J
float jfloat 32 bits F
double jdouble 64 bits D

Table taken from Wikipedia, 5/7/2011


In addition to the primitive types, any object can be referenced using its fully qualified name like this:

LFully.Qualified.Name;

For example, to access a string, you use:  Ljava.lang.String;

To reference an array of the same type, prepend [ like this:

[Ljava.langString;

So that's my quick cheat sheet.  Here are a couple of additional references that help:

Coding Basics - Javascript Native Interface (JSNI) 

JDK 6 Java Native Interface-related APIs & Developer Guides