开发者

How can I specify my own Rhino context in Java?

开发者 https://www.devze.com 2022-12-28 10:41 出处:网络
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 expre

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:
        开发者_Go百科        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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消