Example of what I'm after:
var javaObject = ...
var arr = javaObject.list();
for( foo in arr ) {
// Process element
}
The above doesn't work because the returned object is (for example) a
String[] instead of a Javascript array of strings.
I can hack around it, but would prefer it if my API can be seamless.
I'm dimly aware that it's probably something to do with the
WrapFactory, but pretty much run out of steam at that point. Help?!
Thanks,
Dave.
To answer my own question, the following seemed to do the trick. I'd
appreciate any suggestions if there's a better way to do this though:
public class MyWrapFactory extends WrapFactory {
private static final Logger log = Logger.getAnonymousLogger();
public MyWrapFactory() {
super();
}
@Override
public Object wrap(Context cx, Scriptable scope, Object obj, Class<?
> staticType) {
log.info("Wrap: " + obj);
if( obj != null &&
String[].class.isAssignableFrom(obj.getClass()) ) {
return wrapItUp(cx,scope,obj,staticType);
} else {
return super.wrap(cx, scope, obj, staticType);
}
}
private Scriptable wrapItUp(Context cx, Scriptable scope, Object
obj, Class<?> staticType) {
log.info("Spotted an array incoming...");
final String[] source = (String[])obj;
final Object[] target = new Object[source.length];
for( int i = 0; i < source.length; i++ ) {
target[i] =
super.wrapAsJavaObject(cx,scope,source[i],String.class);
}
log.info("Wrapped and returning");
return cx.newArray(scope, target);
}
}
Var javaObject = toArray(returnedObejct);
Or:
return (string[])arrayList.ToArray(typeof(string));
If you have an arrayList of Strings. I haven¹t worked with Rhino but would
suspect you can also do the cast in the POJO.
Duane
> _______________________________________________
> dev-tech-js-engine mailing list
> dev-tech-...@lists.mozilla.org
> https://lists.mozilla.org/listinfo/dev-tech-js-engine
>
--
**********************************************************************
Senior Technical Evangelist - Adobe Systems, Inc.
Duane's World TV Show - http://www.duanesworldtv.org/
Blog - http://technoracle.blogspot.com
Community Music - http://www.mix2r.com
My Band - http://www.myspace.com/22ndcentury
Adobe MAX 2008 - http://technoracle.blogspot.com/2007/08/adobe-max-2008.html
**********************************************************************
What about just returning the result of cx.newArray(scope,
myJavaArray)?
--Norris