Pages

Tuesday, April 7, 2009

Java Script Native Interface

Share it Please

Java Script Native Interface--

HI EveryOne ,

Today iam going to explain the basics of jsni [ java script native interface ] . JSNI is GWT’s mechanism to allow you as a programmer to embed JavaScript in the

Java code.As we know there are 2 sides of jsni , java and java Script .

Generally jsni may be used mainly when ,

To enable communication between a main application and component

applications using a JavaScript variable (although only when they can’t be

in the same variable scope)

■ To access browser functionality that hasn’t been included directly with GWT

■ To expose an API of your GWT application to other applications on your

web page that can execute JavaScript (useful if you’re replacing legacy

applications in an existing site)

■ To access useful JavaScript libraries that you don’t have either the patience

or the ability to translate into GWT (you may, for example, be restricted by a

license associated with the library)

Generally jsni code is to be used with atmost care.we need to deciede wheter we need to use jsni or not . because according to Gwt , code written In gwt makes it to work acorss many browsers. If the same thing is to be done with jsni for example consider if we need to get the chidCount ,we will be using DOM.getChildCount() which may work differently in different browsers. Most of this sort of functionality is provided in gwt if we look more carefully.

The GWT documentation notes that JSNI can be thought of as the “web equivalent of inline assembly code.

We can write the java script call by using a native keyword , and using a special syntax

“/*- -*/”.This gives us the way to write a java script function that will run by the browser.


Public static native void alert()

/*- {

Alert(“jagadesh”);

}-*/;

And more than that we can access the java variables from java script code . Methods of java can be accessed by using “@method Name” and variables can be accessed by “::variable Name”.

Let’s write a sample class which contains a Jsni method

public class Application implements EntryPoint {

//OnModuleLoad

public void onModuleLoad() {

RootPanel.get().add(new Button("Click Me", new ClickListener() {

public void onClick(Widget sender) {

alert();

}

}));

}//OnModuleLoad()

//Jsni Method which displays a Alert message


public static native void alert()/*-{

alert("jagadesh");

}-*/;

}


Now lets check another code snippet,a code to access instance variable , static variables that are defines in java.


public class Application implements EntryPoint {

public String myInstanceVariable="this is instance variable";

private static String myStaticvariable="this is static variable";

//OnModuleLoad

public void onModuleLoad() {

RootPanel.get().add(new Button("Click Me", new ClickListener() {

public void onClick(Widget sender) {

showInstanceData();

}

}));

}//OnModuleLoad()


public static native void alert()/*-{

alert("jagadesh");

}-*/;


public static native void showStaticData()/*-{

//To Access the static variable call ,

var obtainedStaticVariable=@com.example.client.Application::myStaticvariable;

alert("Static Variable is--"+obtainedStaticVariable);

}-*/;


public native void showInstanceData()/*-{

//To Access The Instance Variable

var obtainedInstanceVariable=this.@com.example.client.Application::myInstanceVariable;

alert("Instance Variable is--"+obtainedInstanceVariable);

}-*/;


}


Now lets write a code as how to access the static methods and instance methods that are defined in java to java script .


public class Application implements EntryPoint {


public static void myStaticMethod() {

Window.alert("We R In the Static Method");

}

public static void myInstanceMethod() {

Window.alert("We R In the Instance Method");

}

public static void myStaticMethodWithArgument(String s) {

Window.alert(s);

}

//OnModuleLoad

public void onModuleLoad() {

RootPanel.get().add(new Button("Click Me", new ClickListener() {

public void onClick(Widget sender) {

try {

showInstanceData();

showStaticData();

showStaticDataWithArgument("jagadesh");

}catch(Exception e) {

e.printStackTrace();

}

}

}));

}//OnModuleLoad()


public static native void showStaticData()/*-{

@com.example.client.Application::myStaticMethod()();

}-*/;


public native void showInstanceData()/*-{

this.@com.example.client.Application::myInstanceMethod()();

}-*/;


public static native void showStaticDataWithArgument(String s)/*-{

@com.example.client.Application::myStaticMethodWithArgument(Ljava/lang/String;)(s);

}-*/;

}


we worked with an example where we called a java method from java script

public static native void showStaticDataWithArgument(String s)/*-{

@com.example.client.Application::myStaticMethodWithArgument(Ljava/lang/String;)(s);

}-*/;


Here we can see that java method takes an argument as String , where from jsni method we sent (Ljava\lang\string;)(s) .

This is the basic mapping from java script to java . here are the other mappings ,

Boolean

Z

Byte

B

Char

C

Short

S

Int

I

Long

J

Float

F

Double

D

Any java class

L(fully qualified path) ex: Ljava/lang/string

arrays

[ followed by the JSNI token for the contained type]

Lets write a more advanced :


public class Data {

public String action="this is data class";

public int version=1;

}

And the onModuleLoad() looks like,

public class Application implements EntryPoint {

//OnModuleLoad

public void onModuleLoad() {

RootPanel.get().add(new Button("Click Me", new ClickListener() {

public void onClick(Widget sender) {

try {

showSomeData(new Data());

}catch(Exception e) {

e.printStackTrace();

}

}

}));

}//OnModuleLoad()

public static native void showSomeData(Data d) /*-{

if(d.@com.example.client.Data::action=="this is data class"){

alert("its Write");

}

}-*/;

}


we will discuss some more concepts of jsni in next part

please donot forget to post yout comments.


thank u,

jagadesh.

No comments :

Post a Comment