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

convert org.mozilla.javascript.NativeArray to java.lang.reflect.Array

979 views
Skip to first unread message

Rhino user

unread,
Sep 18, 2007, 12:45:18 AM9/18/07
to
Hi,
I am getting an array as input to one of the functions in a subclass
of ScriptableObject.
If I try to pass Object[] as an argument , I get exception.
If I give it as Object and then try to see if it is native array and
process it , somehow I cant get the actual values out pf it.
example of javascript is --
var arr = new Array();
var f = this.getField("lsBox1");
var l = this.getField("txt0");
l.value = "Orig List:";

for (var i = 0; i < f.numItems; i++){
l.value = l.value +"\n"+f.getItemAt(i, false);
arr[i] = [f.getItemAt(i,false), f.getItemAt(i)];}
arr.sort(compare);
f.setItems(arr);

The function set Items is in the ScriptableObject subclass . Current
implementation is like this - which adds native array object to my
text.
public void jsFunction_setItems(Object result)
{
if (result instanceof NativeArray) {
// Convert JavaScript array to Collection
NativeArray arr = (NativeArray) result;
int len = (int) arr.getLength();
List list = new ArrayList(len);
for (int i = 0; i < len; i++) {
Object obj = arr.get(i, arr);
Context.toObject(obj, arr);
if (obj == Context.getUndefinedValue()) {
obj = null;
}
list.add(obj);
}
Object[] arr1 = list.toArray();

for (int i = 0; i < arr1.length; i++) {
if (arr1[i] instanceof Object[]) {
Object[] arr2 = (Object[]) arr1[i];
jsFunction_insertItemAt(arr2[0].toString(), arr2[1]
.toString(), i);
} else {
jsFunction_insertItemAt(arr1[i].toString(), arr1[i]
.toString(), i);
}
}
}
}

Norris Boyd

unread,
Sep 18, 2007, 11:24:46 AM9/18/07
to

Have you tried using Context.getElements?

--N

Rhino user

unread,
Sep 18, 2007, 2:15:10 PM9/18/07
to

It still has array of nativeArray objects. I am not able to to the
actual data in it.
Thanks

Norris Boyd

unread,
Sep 18, 2007, 5:04:47 PM9/18/07
to

It doesn't do a deep convert; you'll have to pass each element to
Context.getElements as well. Could those elements also be arrays? If
so, you'll have to take a different approach.

What are you trying to do?

--N

Rhino user

unread,
Sep 19, 2007, 11:47:04 AM9/19/07
to

I can get an array of array from javascript. I have to get each
element at feed it to a listbox or combobox. That is why I need each
entry in the array. This array comes from an API called setItems on a
combobox and then I iterate thr' all items and insert them one by one.
Thanks

0 new messages