Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Returning a Java Array as a native array

72 views
Skip to first unread message

MythMoth

unread,
Jul 30, 2008, 11:12:57 AM7/30/08
to
I'm calling into a native Java object's method from the Rhino
Javascript environment, but I can't for the life of me figure out how
to have the Java objects return native array objects.

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.

MythMoth

unread,
Jul 30, 2008, 11:33:17 AM7/30/08
to
On Jul 30, 4:12 pm, MythMoth <dave.min...@gmail.com> wrote:
> I'm calling into a native Java object's method from the Rhino
> Javascript environment, but I can't for the life of me figure out how
> to have the Java objects return native array objects.
>
> ...

>
> 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?!

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);
}
}

Duane Nickull

unread,
Jul 30, 2008, 12:17:56 PM7/30/08
to MythMoth, dev-tech-...@lists.mozilla.org
Have you tried explicitly casting the returned object into an array in
JavaScript?

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
**********************************************************************

Norris Boyd

unread,
Jul 30, 2008, 5:37:01 PM7/30/08
to

What about just returning the result of cx.newArray(scope,
myJavaArray)?

--Norris

0 new messages