Register a custom type converter (is it possible)?

Sundararajan Athijegannathan sundararajan.athijegannathan at oracle.com
Mon Jun 20 12:58:34 UTC 2016


No, there are no such conversion hooks.

But, if any of your Java methods accept java.util.Maps, you can pass
script objects.  Also, if any of your Java methods accept List or Java
arrays, you can pass script arrays.

var m = new java.util.HashMap();

m.putAll({ foo: 44 });

var l = new java.util.ArrayList();

l.addAll([44, 546, 676]);

-Sundar

On 6/20/2016 5:43 PM, Paulo Lopes wrote:
> Hi,
>
>
> I've been reading the documentation, wiki and the examples from Nashorn and
> have a question that I cannot find a yes/no answer to. Say for example that
> I've an application that leverages on Nashorn for JavaScript execution but
> its core is in Java. In my Java code I've a interface like:
>
> interface Foo {
>
>   void doSomething(Bar bar);
>
> }
>
>
> Now from Nashorn I'd like to invoke the method "doSomething" like:
>
>
> foo.doSomethin({key: 'value'});
>
>
> As expected this won't work since Nashorn does not know how to cast from
> "JSObject" to "Bar" however I'd like to register somewhere/somehow (this is
> what I don't know) a helper class/method/something where I could do this. I
> also do not want to bring Nashorn API/types to Java to I'm not looking for
> something like:
>
>
> class Bar implements JSObject { ... }
>
>
> Is this possible? and it is possible with JDK8?
>
>
> Thanks!
> Paulo



More information about the nashorn-dev mailing list