Nashorn Roadmap & Rhino migration question
A. Sundararajan
sundararajan.athijegannathan at oracle.com
Thu Aug 7 09:31:42 UTC 2014
Hi Paul,
Yes, it should work on all places where Java class access is needed.
*) Reflectively load your java class using Class.forName.
*) access value of "static" property on it.
*) resulting object (instance of StaticClass) can be used anywhere Java
class is needed.
(superclass, interface, class-for-new, static method call, static field
access).
Hope this helps,
-Sundar
On Thursday 07 August 2014 12:36 AM, Paul Bakker wrote:
> Hi Sundar,
>
> Does the approach you suggested also work for implementing
> interfaces/abstract classes, like I did in my example?
>
> When we built this in Rhino, we had a hard time figuring out how to
> extend (Abstract) Java classes/implement Java interfaces through
> JavaScript that were loaded dynamically using a custom classloader at
> runtime in scripting.
>
> The only way that worked in Rhino was using a custom Packages instance
> created with new Packages(ClassLoader). Loading a class through
> reflection and then trying to provide an implementation through
> JavaScript didn't work.
>
> So, this is what we're doing in Rhino:
>
> try {
> //Create custom classloader that extends applicationclassloader
> and set it temporarily as applicationclassloader
> var cx = Packages.org.mozilla.javascript.Context.getCurrentContext()
> var savedCL = cx.getApplicationClassLoader()
> var mediaCL = java.net.URLClassLoader([new
> java.net.URL("mycustomprotocol:///bin/")], savedCL)
> cx.setApplicationClassLoader(mediaCL)
>
> //Create custom Packages, using a custom classloader that extends
> the applicationclassloader
> var customPackages = new Packages(mediaCL);
> //Seehttp://osdir.com/ml/mozilla.devel.jseng/2002-06/msg00037.html
>
> //Extend abstract class 'MyClass', providing the implementation
> through JavaScript
> var callBackClass = new customPackages.com.mycompany.MyClass({
> name: function(arg1, arg2) {
> //Some code here
> }
> })
> } catch (e) {
> log.error(e)
> } finally {
> cx.setApplicationClassLoader(savedCL);
> }
>
> Paul
>
> On 8/1/2014 8:49 PM, Jim Laskey (Oracle) wrote:
>> Hi,
>>
>> Nashorn engine picks up thread-context loader at the time of engine
>> creation or user supplied loader (with nashorn specific extension API).
>>
>> For the cases like you specify, you've to use reflection - but only to
>> get to the class. Something like
>>
>> // get java.lang.Class object of the desired class using the class
>> loader "mediaCL"
>> var myClass =
>> java.lang.Class.forName("customPackages.com.mycompany.MyClass", true,
>> mediaCL);
>> // make "static" reference to that class.
>> // MyClass is an instance of StaticClass in nashorn
>> var MyClass = myClass.static;
>>
>> // MyClass can be used just like you'd use other java classes from
>> script
>>
>> // new object
>> var obj = new MyClass();
>> // instance method
>> obj.mymethod("bar");
>> // static method
>> print(MyClass.myStaticBar());
>>
>> Hope this helps,
>> -Sundarl,
More information about the nashorn-dev
mailing list