本文主要是介绍揭开GWT的JavaScriptObject对象的神秘面纱,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
GWT的JSNI提供了java代码和javascript代码之间的交互功能,其中最重要的一个类就是 JavaScriptObject 。这是一个非常特殊的类:
1.不可以直接new创建,必须通过JSNI的native方法来创建。这也是为什么它只有protected修饰符的构造函数的原因了。
2.java代码可以把JavaScriptObject的对象通过native再次传回给js使用。
3.从JavaScriptObject类继承的类其构造函数的修饰符必须是protected,其公共方法必须加上final修饰符。
先一个应用的小例子,希望对朋友们理解代码有所帮助。
现假设我们有一个外联的javascript文件中有这么一个类
function Person() {}Person.prototype.setName = function(name) {this.name = name;
}
我们就可以在java中构建一个JSPerson类对javascript的Person类进行封装
public class JSPerson extends JavaScriptObjectWrapper {protected JSPerson() {}public static native JSPerson createNew() /*-{return new $wnd.Person();}-*/;
}
然后使用这个类
JSPerson jsPerson = JSPerson.createNew();
jsPerson.invokeMethod("setName", "sbw");
有了这样一个JSNI得通用类后,和外联js交互起来竟然如此的简单。
下面详细的解说,我们开始对JavaScriptObject类进行继承和封装,实现对外联的javascript库的方便操作。
1.首先我们建立类JavaScriptObjectWrapper继承自JavaScriptObject
package com.sbw.client.common;import com.google.gwt.core.client.JavaScriptObject;public class JavaScriptObjectWrapper extends JavaScriptObject {protected JavaScriptObjectWrapper() {}}
2.然后我们建立JSArrayHelper类,实现将java的Object数组转换为javascript的数组。这里请注意数组中每一个元素的类型的转换。
package com.sbw.client.common;public class JSArrayHelper {/**** @param objs : must be primitive type or JavaScriptObject, now just support few you can extend* @return*/public static JavaScriptObjectWrapper toJSArray(Object... objs) {JavaScriptObjectWrapper jsArray = createJSArray();for(int i = 0; i < objs.length; i++) {setJSArray(jsArray, i, objs[i]);}return jsArray;}private static native JavaScriptObjectWrapper createJSArray() /*-{return [];}-*/;private static void setJSArray(JavaScriptObjectWrapper jsArray, int index, Object value) {if(value.getClass().equals(java.lang.Integer.class)) {setJSArray(jsArray, index, (int) Integer.valueOf(value.toString()));}if(value.getClass().equals(java.lang.Float.class)) {setJSArray(jsArray, index, (float) Float.valueOf(value.toString()));}if(value.getClass().equals(java.lang.Double.class)) {setJSArray(jsArray, index, (double) Double.valueOf(value.toString()));}if(value.getClass().equals(java.lang.Boolean.class)) {setJSArray(jsArray, index, (boolean) Boolean.valueOf(value.toString()));}if(value.getClass().equals(java.lang.String.class)) {setJSArray(jsArray, index, value.toString());}if(value.getClass().equals(JavaScriptObjectWrapper.class)) {setJSArray(jsArray, index, (JavaScriptObjectWrapper) value);}}private static native void setJSArray(JavaScriptObjectWrapper jsArray, int index, int value) /*-{jsArray[index] = value;}-*/;private static native void setJSArray(JavaScriptObjectWrapper jsArray, int index, float value) /*-{jsArray[index] = value;}-*/;private static native void setJSArray(JavaScriptObjectWrapper jsArray, int index, double value) /*-{jsArray[index] = value;}-*/;private static native void setJSArray(JavaScriptObjectWrapper jsArray, int index, boolean value) /*-{jsArray[index] = value;}-*/;private static native void setJSArray(JavaScriptObjectWrapper jsArray, int index, String value) /*-{jsArray[index] = value;}-*/;private static native void setJSArray(JavaScriptObjectWrapper jsArray, int index, JavaScriptObjectWrapper value) /*-{jsArray[index] = value;}-*/;
}
3.创建JavaScriptObjectWrapperImpl类,全是static native方法,实现类似于java反射的机制,可以调用方法,可以获取和设置属性
package com.sbw.client.common;public class JavaScriptObjectWrapperImpl {public static native JavaScriptObjectWrapper invokeJSMethod(JavaScriptObjectWrapper jsObject, String methodName, JavaScriptObjectWrapper arguments) /*-{return jsObject[methodName].apply(jsObject, arguments);}-*/;public static native void setProperty(JavaScriptObjectWrapper object, String name, int value) /*-{object[name] = value;}-*/;public static native int getPropertyAsInt(JavaScriptObjectWrapper object, String name) /*-{var ret = object[name];return (ret === undefined) ? 0 : ret;}-*/;public static native void setProperty(JavaScriptObjectWrapper object, String name, String value) /*-{object[name] = value;}-*/;public static native String getPropertyAsString(JavaScriptObjectWrapper object, String name) /*-{var ret = object[name];return (ret === undefined) ? null : ret;}-*/;public static native void setProperty(JavaScriptObjectWrapper object, String name, boolean value) /*-{object[name] = value;}-*/;public static native boolean getPropertyAsBoolean(JavaScriptObjectWrapper object, String name) /*-{var ret = object[name];return (ret === undefined) ? 0 : ret;}-*/;public static native void setProperty(JavaScriptObjectWrapper object, String name, float value) /*-{object[name] = value;}-*/;public static native float getPropertyAsFloat(JavaScriptObjectWrapper object, String name) /*-{var ret = object[name];return (ret === undefined) ? 0 : ret;}-*/;public static native void setProperty(JavaScriptObjectWrapper object, String name, double value) /*-{object[name] = value;}-*/;public static native double getPropertyAsDouble(JavaScriptObjectWrapper object, String name) /*-{var ret = object[name];return (ret === undefined) ? 0 : ret;}-*/;public static native void setProperty(JavaScriptObjectWrapper object, String name, JavaScriptObjectWrapper value) /*-{object[name] = value;}-*/;public static native JavaScriptObjectWrapper getProperty(JavaScriptObjectWrapper object, String name) /*-{var ret = object[name];return (ret === undefined) ? null : ret;}-*/;
}
4.在其它两个类的支持下完善JavaScriptObjectWrapper类
package com.sbw.client.common;import com.google.gwt.core.client.JavaScriptObject;public class JavaScriptObjectWrapper extends JavaScriptObject {protected JavaScriptObjectWrapper() {}public final JavaScriptObjectWrapper invokeJSMethod(String methodName, Object... objs) {JavaScriptObjectWrapper arguments = JSArrayHelper.toJSArray(objs);return JavaScriptObjectWrapperImpl.invokeJSMethod(this, methodName, arguments);}public final void setProperty(String name, int value){JavaScriptObjectWrapperImpl.setProperty(this, name, value);}public final int getPropertyAsInt(String name){return JavaScriptObjectWrapperImpl.getPropertyAsInt(this, name);}public final void setProperty(String name, String value){JavaScriptObjectWrapperImpl.setProperty(this, name, value);}public final String getPropertyAsString(String name){return JavaScriptObjectWrapperImpl.getPropertyAsString(this, name);}public final void setProperty(String name, boolean value){JavaScriptObjectWrapperImpl.setProperty(this, name, value);}public final boolean getPropertyAsBoolean(String name){return JavaScriptObjectWrapperImpl.getPropertyAsBoolean(this, name);}public final void setProperty(String name, float value){JavaScriptObjectWrapperImpl.setProperty(this, name, value);}public final float getPropertyAsFloat(String name){return JavaScriptObjectWrapperImpl.getPropertyAsFloat(this, name);}public final void setProperty(String name, double value){JavaScriptObjectWrapperImpl.setProperty(this, name, value);}public final double getPropertyAsDouble(String name){return JavaScriptObjectWrapperImpl.getPropertyAsDouble(this, name);}public final void setProperty(String name, JavaScriptObjectWrapper value){JavaScriptObjectWrapperImpl.setProperty(this, name, value);}public final JavaScriptObjectWrapper getProperty(String name){return JavaScriptObjectWrapperImpl.getProperty(this, name);}}
这样我们的封装就完成了。
这篇关于揭开GWT的JavaScriptObject对象的神秘面纱的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!