From: Edwin C. <com...@gm...> - 2009-08-17 10:33:32
|
Hi Ying Xia, Sorry I misspelled your name on a previous post. I have deleted that post. Here it is the content again for the developer list: I have been playing a bit with implemeting one or more invoke methods. When functions are invoked it is on OpenLayers objects (The getProperty/setProperty methods are for the config objects, and it's never necessary to invoke functions on config objects). Often these functions return values and I think it may get messy to get the return values by their correct type. Maybe you have some suggestions when you looking at what I have written below, since it looks like you were already using code to invoke functions (or did you create that after seeing getProperty/setProperty?). What did you do if a function returned a String, floating point number, or integer? Please read on to see what I mean. I found that I needed the following to code to invoke toString of Size and have it return a String (for an easy example and for briefness I made a variant to invoke functions that do not take arguments): in JSObjectHelper: public static native Object invoke(JSObject object, String name) /*-{ return object[name].apply(object); }-*/; in JSObject: public final Object invoke(String name) { return JSObjectHelper.invoke(this, name); }; invoking toString of Size: public String toString(){ return (String) getJSObject().invoke("toString"); } As you might have notice an Object is returned.by the JSObjectHelper/JSObject return methods. Returning a JSObject did not work, because that cannot be cast to a String and I also could not get a working narrowToString function on JSObject. It is a bit vague to my why returning Object did work. As an implementation of narrowToString I tried: public static native String narrowToString(JSObject object) /*-{ return object; //Gives a ClassCast exception }-*/; Although this works, and I can image it works with your way to pass arguments, I can foresee some uglyness when an integer or float is returned from the invoked function. Right now a lot of the API works with primitives and there is a mismatch between Object and primitives. I'd rather avoid autoboxing so then you may get things like: Float f = (Float) getJSObject().invoke("getHeight"); return f.floatValue(); An uglier problem is how to combine this with the possibility that a complex Javascript object is returned, because that would have to be returned as a JSObject. In Java functions with the same signature but different return tyehpes are not allowed, so we cannot have both: public final Object invoke(String name){ ... }; public final JSObject invoke(String name){ ... }; |