我可以严格评估 Java 中存储为字符串的布尔表达式吗?
我希望能够评估存储为字符串的布尔表达式,如下所示:
"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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在 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.
尝试eval 项目
Try the eval project
使用贾尼诺! http://docs.codehaus.org/display/JANINO/Home
它就像 eval爪哇
Use Janino! http://docs.codehaus.org/display/JANINO/Home
Its like eval for java
MVEL 也可以用
http://mvel.codehaus.org/
一行代码来进行评估大多数情况下:
“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:
"rootObj" could be null, but if it's supplied you can refer to properties and methods on it without qualificiation. ie. "id" or "calculateSomething()".
您可以尝试使用 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.