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

New instances of Java objects in JavaScript

4 views
Skip to first unread message

Jason Kridner

unread,
Apr 24, 2006, 7:48:38 AM4/24/06
to
I'm using Rhino and JavaAdapter to create Java objects. One application
I have requires me to pass the string of a class name to a Java
function. That Java function will then create a new instance of that
class. I need to create the class using JavaScript.

Here is my simplified example and the results:

js> js_o = new Object;
[object Object]
js> js_o = { run: function() { print("got here"); } };
[object Object]
js> js_o.run();
got here
js> java_o1 = new JavaAdapter(java.lang.Runnable, js_o);
adapter1@1ff436
js> java_o1.run();
got here
js> java_c = java_o1.getClass();
class adapter1
js> java_c.getName();
adapter1
js> java_o2 = java_c.newInstance();
org.mozilla.javascript.WrappedException: Wrapped
java.lang.InstantiationException: adapter1 (<stdin>#10)
at
org.mozilla.javascript.Context.throwAsScriptRuntimeEx(Context.java:1705)
at org.mozilla.javascript.MemberBox.invoke(MemberBox.java:157)
...
at org.mozilla.javascript.tools.shell.Main.main(Main.java:137)
Caused by: java.lang.InstantiationException: adapter1
at java.lang.Class.newInstance0(Class.java:335)
at java.lang.Class.newInstance(Class.java:303)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.mozilla.javascript.MemberBox.invoke(MemberBox.java:142)
... 15 more

What is this telling me about the 'adapter1' class that was created by
Rhino?

Is there a way to create a Java class in Rhino that you can use to allow
Java to create new instances?

Thanks in advance!

-Jason

jason-usenet at hangerhead.com

unread,
Apr 27, 2006, 11:49:04 PM4/27/06
to
I discovered a clue to why NewInstance doesn't work by performing the
following command after the ones I gave previously.

js> for (i in java_c.constructors) { print("java_c.constructors[" + i +
"] = " + java_c.constructors[i]); }
java_c.constructors[0] = public
adapter1(org.mozilla.javascript.ContextFactory,org.mozilla.javascript.Scriptable)
java_c.constructors[1] = public
adapter1(org.mozilla.javascript.ContextFactory,org.mozilla.javascript.Scriptable,org.mozilla.javascript.Scriptable)

That's interesting, because there isn't a constructor for no arguments!

Does anyone have an idea how to create a Java class from JavaScript
that doesn't require arguments to the constructor?

-Jason

jason-usenet at hangerhead.com

unread,
Apr 28, 2006, 12:22:40 AM4/28/06
to
Just to give further examples and try to summarize the problem
different ways...

This works...

java_o1 = new java.lang.Thread;
java_c = java_o1.getClass();
java_o2 = java_c.newInstance();

This does not...

js_o = new Object;


js_o = { run: function() { print("got here"); } };

js_o.run();


java_o1 = new JavaAdapter(java.lang.Runnable, js_o);

// or java_o1 = new java.lang.Runnable(js_o);
java_o1.run();
java_c = java_o1.getClass();
java_o2 = java_c.newInstance();

Fundamentally, I believe this is because the JavaAdapter created class
in the Java side of things requires arguments in its constructor.
There doesn't seem to be much documentation on JavaAdapter and the
class is final. I think the solution will be for me to create a class
wrapper that generates Java classes that use my global script context
to call the JavaAdapter constructors. I can't extend the JavaAdapter
class itself because it is final.

I'm still happy if anyone would like to chime in on how I can solve
this issue.

-Jason

jason-usenet at hangerhead.com

unread,
May 9, 2006, 6:12:37 AM5/9/06
to
Is there somewhere else I can ask this question? I tried Google
Answers and got no response their either. I think this is a very
fundamental question that some architect of Rhino must already know the
answer.

Dirk Haun

unread,
May 9, 2006, 7:10:57 AM5/9/06
to
> Is there somewhere else I can ask this question?

Yes, over in mozilla.dev.tech.js-engine

This list was closed back in January and the people who can actually
provide answers have moved to mozilla.dev.tech.js-engine

Also see
http://groups.google.com/group/netscape.public.mozilla.jseng/browse_thread/thread/5a0d643dd2c18f40/97266d1aa4b508e5

0 new messages