Extending a Java class

Sundararajan Athijegannathan sundararajan.athijegannathan at oracle.com
Fri Sep 28 09:34:41 UTC 2018


It is hard to say what went wrong without looking at your full sample/test.

Openjdk wiki page explains Java.extend function is here:

https://wiki.openjdk.java.net/display/Nashorn/Nashorn%2Bextensions#Nashornextensions-java_extend

You may want to take a look at nashorn samples that use Java.extend such 
these ->

http://hg.openjdk.java.net/jdk/jdk/file/7bd8d6b011c9/src/sample/nashorn/flexijson.js
http://hg.openjdk.java.net/jdk/jdk/file/7bd8d6b011c9/src/sample/nashorn/resourcetrysuggester.js

Hope this helps,
-Sundar

On 27/09/18, 8:53 PM, Axel Dörfler wrote:
> Hi there,
>
> I'm trying to extend an Abstract java base class, and add a new method 
> to it. However, that doesn't seem to work.
>
> I've tried it like this:
>
> var object = Java.extend(MyType, {
>     newFunction: function(argument) {
>         ...
>     }
> });
>
> And that:
>
> MyType.newFunction = function(...) {...};
> var object = new MyType;
>
> And also:
>
> var object = new MyType({
>     newFunction: ...
> });
>
> While it will all happily accept those things, none of them worked.
> When I try to call the method, it'll say:
> TypeError: object.newFunction is not a function
>
> Do you have any idea how I could solve this? Overriding an existing 
> method works easily, adding a new method does not (at least not this 
> way).
>
> Kind regards,
>    Axel Dörfler.


More information about the nashorn-dev mailing list