Search code examples
javawrappernashorn

How to hide methods/fields from Nashorn


is it possible to hide methods/fields from the NashornScriptEngine for example it can only call methods/fields if a particular annotation is associated with it.

I did find another question similar to this but unfortunately it was for Rhino how do I hide a class method from rhino script engine.

I did attempt though to extends some of the classes but they are all final so no luck there.


Solution

  • No, nashorn does not support hiding specific public method(s) or public field(s) of a Java class or object. But, you can wrap a Java object by a JSObject implementation (https://docs.oracle.com/javase/8/docs/jdk/api/nashorn/jdk/nashorn/api/scripting/JSObject.html) and filter out specific methods and/or fields. But note that this scheme does not work for Java classes - unless you override Java.type function in global scope and return wrapped objects for specific classes.