Extending a Java class

Sundararajan Athijegannathan sundararajan.athijegannathan at oracle.com
Mon Oct 1 03:00:08 UTC 2018


Hi,

In your example, it seems you're trying to add a new method to the 
subtype created in script. That is not supported. You can override 
existing methods of superclass  - but not add a new method to the Java 
type. Note that all super methods (including protected ones) are 
available via Java.super. For example, you can invoke 
Java.super(filter).fireMatchNode(). So the workaround for this case 
would be add utility function outside for "setEnabled".

Hope this helps,
-Sundar

On 28/09/18, 3:19 PM, Axel Dörfler wrote:
> Am 28/09/2018 um 11:34 schrieb Sundararajan Athijegannathan:
>> It is hard to say what went wrong without looking at your full 
>> sample/test.
>>
>> Openjdk wiki page explains Java.extend function is here:
>> http://hg.openjdk.java.net/jdk/jdk/file/7bd8d6b011c9/src/sample/nashorn/resourcetrysuggester.js 
>
>
> Thanks! This usage suggests that it should actually work.
>
> Here is my full example stripped down to the bare minimum:
>
> ----------------8<----------------
> var MatcherEditor = 
> Java.type("ca.odell.glazedlists.matchers.AbstractMatcherEditor");
>
> var filter = new (Java.extend(MatcherEditor)) {
>         setEnabled: function(enabled) {
>             if (enabled) {
>                 fireMatchNone();
>             } else {
>                 fireMatchAll();
>             }
>         }
>     };
> filter.setEnabled(true);
> ----------------8<----------------
>
> The error is:
> TypeError: filter.setEnabled is not a function in <eval> at line 
> number 13
>
> For the Glazedlists class used, see here:
> http://static.javadoc.io/net.java.dev.glazedlists/glazedlists_java16/1.10.0/ca/odell/glazedlists/matchers/AbstractMatcherEditor.html 
>
>
> Source:
> https://github.com/glazedlists/glazedlists/blob/master/core/src/main/java/ca/odell/glazedlists/matchers/AbstractMatcherEditor.java 
>
>
> That looks like a bug to me. But if there's a workaround, I'll take it 
> :-)
>
> Kind regards,
>    Axel Dörfler.


More information about the nashorn-dev mailing list