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