我可以严格评估 Java 中存储为字符串的布尔表达式吗?

发布于 2024-09-04 09:31:52 字数 1097 浏览 5 评论 0原文

我希望能够评估存储为字符串的布尔表达式,如下所示:

"hello" == "goodbye" && 100 < 101

我知道已经有很多这样的问题,但我问这个是因为我已经尝试了最常见的答案 评估这样的语句

"hello" == 100

对于这个问题,BeanShell,它允许毫无问题地 。有谁知道 FOSS 解析器会因操作数不匹配等问题而引发错误?或者 BeanShell 中是否有一个设置可以帮助我?我已经尝试过 Interpreter.setStrictJava(true) 。

为了完整起见,这是我当前使用的代码:

Interpreter interpreter = new Interpreter();
interpreter.setStrictJava(true);    
String testableCondition = "100 == \"hello\"";
try {
    interpreter.eval("boolean result = ("+ testableCondition + ")");
    System.out.println("result: "+interpreter.get("result"));
    if(interpreter.get("result") == null){
        throw new ValidationFailure("Result was null");
    }
} catch (EvalError e) {
    e.printStackTrace();
    throw new ValidationFailure("Eval error while parsing the condition");
}

编辑:

我当前的代码返回此输出,

result: false

没有错误。我希望它做的是抛出一个 EvalError 或某事让我知道存在不匹配的操作数。

I would like to be able to evaluate an boolean expression stored as a string, like the following:

"hello" == "goodbye" && 100 < 101

I know that there are tons of questions like this on SO already, but I'm asking this one because I've tried the most common answer to this question, BeanShell, and it allows for the evaluation of statements like this one

"hello" == 100

with no trouble at all. Does anyone know of a FOSS parser that throws errors for things like operand mismatch? Or is there a setting in BeanShell that will help me out? I've already tried Interpreter.setStrictJava(true).

For completeness sake, here's the code that I'm using currently:

Interpreter interpreter = new Interpreter();
interpreter.setStrictJava(true);    
String testableCondition = "100 == \"hello\"";
try {
    interpreter.eval("boolean result = ("+ testableCondition + ")");
    System.out.println("result: "+interpreter.get("result"));
    if(interpreter.get("result") == null){
        throw new ValidationFailure("Result was null");
    }
} catch (EvalError e) {
    e.printStackTrace();
    throw new ValidationFailure("Eval error while parsing the condition");
}

Edit:

The code I have currently returns this output

result: false

without error. What I would like it to do is throw an EvalError or something letting me know that there were mismatched operands.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

递刀给你 2024-09-11 09:31:52

在 Java 6 中,您可以动态调用编译器,如本文中所述:

http://www.ibm.com/developerworks/java/library/j-jcomp/index.html

您可以使用它来将表达式动态编译为 Java 类,如果出现以下情况,这将抛出类型错误:您尝试将字符串与数字进行比较。

In Java 6, you can dynamically invoke the compiler, as explained in this article:

http://www.ibm.com/developerworks/java/library/j-jcomp/index.html

You could use this to dynamically compile your expression into a Java class, which will throw type errors if you try to compare a string to a number.

我家小可爱 2024-09-11 09:31:52

使用贾尼诺! http://docs.codehaus.org/display/JANINO/Home

它就像 eval爪哇

Use Janino! http://docs.codehaus.org/display/JANINO/Home

Its like eval for java

北风几吹夏 2024-09-11 09:31:52

MVEL 也可以用

http://mvel.codehaus.org/

一行代码来进行评估大多数情况下:

Object result = MVEL.eval(expression, rootObj);

“rootObj”可能为 null,但如果提供了它,您可以无需限定即可引用其属性和方法。 IE。 “id”或“calculateSomething()”。

MVEL would also be useful

http://mvel.codehaus.org/

one line of code to do the evaluation in most cases:

Object result = MVEL.eval(expression, rootObj);

"rootObj" could be null, but if it's supplied you can refer to properties and methods on it without qualificiation. ie. "id" or "calculateSomething()".

浪漫之都 2024-09-11 09:31:52

您可以尝试使用 http://groovy.codehaus.org/api/groovy/util /Eval.html 如果 groovy 是一个选项。

You can try with http://groovy.codehaus.org/api/groovy/util/Eval.html if groovy is an option.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文