Replies: 4 comments 3 replies
-
This is a very generic question where the potential answers vary significantly based on the situation at hand, ranging from using the (to be removed) SecurityManager to disabling Java interop to using annotations Am converting this to a discussion (for now) Please provide more info |
Beta Was this translation helpful? Give feedback.
-
Hi, your comment is not very clear to me why this involves SecurityManager. I just want to control if the methods and fields in the Java object can be accessed in the script. My situation is basically providing some Java object as a variable for the script to execute and the script can access the methods and fields of this object, but there are some public methods and fields that I don't want the script to have access to. The annotations you mentioned seem to fit my situation, how do I add annotations to the methods and fields to prevent script access? |
Beta Was this translation helpful? Give feedback.
-
I use Rhino in this way (simplified code): public class MyObject {
public String method1() {
return "method1";
}
public String method2() {
return "method2";
}
public Object evalJS(String jsStr) {
try (var cx = Context.enter()) {
cx.setOptimizationLevel(-1);
var scope = cx.initStandardObjects();
scope.put("myObj", scope, Context.javaToJS(this, scope));
return cx.evaluateString(scope, jsStr, "unknown source", 0, null);
}
}
} public class Main {
public static void main(String[] args) {
var obj = new MyObject();
obj.method1();
obj.method2();
obj.evalJS("myObj.method1()");
}
} If that's still not clear enough, check out the full code here: |
Beta Was this translation helpful? Give feedback.
-
Have a look at https://rhino.github.io/tutorials/embedding_tutorial/#defining-host-objects, think that would get you started |
Beta Was this translation helpful? Give feedback.
-
I want some methods and fields of a Java object to be hidden in the script, but currently its visibility is controlled by a modifier, and once I use the private modifier this will cause my code to not be able to access this method as well.
ps: Using kotlin programming on android.
Beta Was this translation helpful? Give feedback.
All reactions