JavaAdapter - extend class?

Attila Szegedi attila.szegedi at oracle.com
Fri Dec 21 13:28:42 PST 2012


On Dec 21, 2012, at 10:18 PM, Bruno Borges <bruno.borges at oracle.com> wrote:

> So, like I said: Nashorn does not support extending a class.

That is not true. You can extend both abstract and concrete classes in Nashorn. Currently, we only support extension of a single type - i.e. you can't implement multiple interfaces at once or extend a class and slap another interface on it. Do you have access to Nashorn JavaDoc? If you do, the top-level overview page together with the the documentation for NativeJava.asType() and NativeJava.extend() form an exhaustive description of all the ways you can implement interfaces and extend classes.

I can tackle extending/implementing multiple types (several interfaces or a class + several interfaces) if there's a user need. There are some complications to getting it right, related to storing such generated classes (i.e. right now we assign the single-type proxy with a ClassValue for the Class object it extends/implements, and for multi-type proxies it gets more complicated). Anyway, as I said, I can do it if there's a need.

But right now, extending either an abstract class or even a concrete class, as well as implementing a single interface all work.

Attila.

> 
> This is bad because sometimes we have abstract classes that need to be implemented on the Javascript side.
> 
> Is there any plan to provide this feature?
> 
> On Fri 21 Dec 2012 07:16:05 PM BRST, Jim Laskey (Oracle) wrote:
>> Absolutely.  Here is the comment from the source.
>> 
>>  This class implements Rhino-like JavaAdapter to help implement a Java
>>  interface in JavaScript. We support this using Invocable.getInterface.
>>  Using this JavaAdapter, script author could write:
>> 
>>     var r = new java.lang.Runnable() {
>>                 run: function() { script... }
>>             };
>> 
>>     r.run();
>>     new java.lang.Thread(r).start();
>> 
>>  Note that Rhino's JavaAdapter support allows extending a Java class and/or
>>  implementing one or more interfaces. This JavaAdapter implementation does
>>  not support these.
>> 
>> If you looked at the twitter example on the blog you'll see another application.
>> 
>> 	https://blogs.oracle.com/nashorn/entry/nashorn_in_the_twitterverse_continued
>> 
>> Cheers,
>> 
>> -- Jim
>> 
>> 
>> 
>> 
>> On 2012-12-21, at 4:56 PM, Bruno Borges <bruno.borges at oracle.com> wrote:
>> 
>>> On Mozilla Rhino, the JavaAdapter offers the capability of extending from concrete classes, and also implementing more than one interface.
>>> 
>>> Will Nashorn offer this same capability?
>>> 
>>> I ask this because the modified version of Rhino that comes with JRE does *not* offer this.
>>> 
>>> Thanks
>>> 
>>> --
>>> Bruno Borges
>>> Principal Product Manager | JavaEE WebLogic GlassFish
>>> Oracle LAD PM Team        | Cloud Application Foundation
>>> +55 11 5187 6514 (Work)   | +55 11 99564 9058 (Mobi)
>>> 
>> 
> 
> --
> --
> Bruno Borges
> Principal Product Manager | JavaEE WebLogic GlassFish
> Oracle LAD PM Team        | Cloud Application Foundation
> +55 11 5187 6514 (Work)   | +55 11 99564 9058 (Mobi)



More information about the nashorn-dev mailing list