How can I specify my own Rhino context in Java?

徘徊边缘 提交于 2019-12-20 03:32:09

问题


I'm trying to ensure that my Rhino scripts (running under Java 6) are strict so that if a script developer misspells an expression I want an exception to be thrown. Currently what happens is the expression simply evaluates to "undefined".

Now according to Mozilla org https://developer.mozilla.org/en/New_in_Rhino_1.6R6 there are features to enable strict checking in the context. I cannot find a working example of this.

What I did so far was write a class to extend ContextFactory and then override the hasFeature method.

public class ScriptContextFactory extends ContextFactory {

    protected boolean hasFeature(Context context, int featureIndex) {

        switch (featureIndex) {
            case Context.FEATURE_STRICT_EVAL:
                return true;

            case Context.FEATURE_STRICT_VARS:
                return true;
        }

        return super.hasFeature(context, featureIndex);
    }
  }

Then in the Main I set mine to the default.

ContextFactory.initGlobal(new ScriptContextFactory());

and I get an illegal state exception. :(

Any ideas or samples on how this works?

TIA


回答1:


If you are doing Context.enter() before calling initGlobal() try reversing the order.



来源:https://stackoverflow.com/questions/2740031/how-can-i-specify-my-own-rhino-context-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!