如何在Java中指定自己的Rhino上下文? [英] How can I specify my own Rhino context in Java?

查看:99
本文介绍了如何在Java中指定自己的Rhino上下文?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力确保我的Rhino脚本(在Java 6下运行)是严格的,这样如果脚本开发人员拼错了表达式,我希望抛出异常。目前发生的情况是表达式只是评估为未定义。

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".

现在根据Mozilla组织 https://developer.mozilla.org/en/New_in_Rhino_1.6R6 有一些功能可以在上下文中启用严格检查。我找不到一个有效的例子。

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.

我到目前为止所做的是写一个类来扩展ContextFactory然后覆盖hasFeature方法。

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);
    }
  }

然后在Main我设置为默认值。

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

推荐答案

如果您在调用 initGlobal()之前正在执行 Context.enter() 尝试撤消订单。

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

这篇关于如何在Java中指定自己的Rhino上下文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆