Search code examples
javanashorn

Java class functions and members


I am trying to improve my scripts by altering some code. What I want is to make use of JS members. I know I can do this with AbstractScriptObject by overriding getMember.

Now comes the problem, I also have methods in this class that I want to call, but apparently I cannot call them anymore because it acts like a JS object now and it won't recgonize my methods anymore.

Is there any way so I can have members and java methods that I can call within a script? (Maybe something that converts my java methods to js functions?)

I want to do something like this in scripts: if (dog.name == "") and dog.doSomething(args)

Thanks in advance, Kevin


Solution

  • You've to expose your "methods" as script "functions". i.e., getMember should return further JSObject implementations that are "callable" (isFunction returns true and you override "call").