Register a custom type converter (is it possible)?

Sundararajan Athijegannathan sundararajan.athijegannathan at oracle.com
Mon Jun 20 13:45:52 UTC 2016


It may be simpler to provider conversion functions to your users. Like
"makeBar" functions - you could load those conversion script functions
before evaluating other scripts.

Keeps the java side typesafe and provide arbitrary mapping from script
side...

-Sundar


On 6/20/2016 7:09 PM, Paulo Lopes wrote:
> That is what I was trying to avoid. In the java side I'd like to have
> specific types `Bar` so i get type safety, compile time validation,
> etc... all the things you expect from a strongly typed language, and
> on Nashorn side I'd like to have the freedom of dynamic typed
> languages, also if my contract is already deployed I'd break the whole
> compatibility/contract by changing the method signatures to be Map.
>
> I was trying to get my head around the dynalink stuff but sadly it
> does not apply to this problem.
>
>
>
> On Mon, Jun 20, 2016 at 2:58 PM, Sundararajan Athijegannathan
> <sundararajan.athijegannathan at oracle.com
> <mailto:sundararajan.athijegannathan at oracle.com>> wrote:
>
>     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
>
>
>
>
> -- 
> Paulo Lopes
> www.jetdrone.com <http://www.jetdrone.com>



More information about the nashorn-dev mailing list