简体   繁体   English

MVEL 验证没有值的算术表达式

[英]MVEL to validate arithmetic expression without values

I want to verify if given arithmetic expression (a+b) is valid without providing inputs or values.我想在不提供输入或值的情况下验证给定的算术表达式(a+b)是否有效。 I tried using ExpressionCompiler and MVEL.compileExpression() as below,我尝试使用ExpressionCompilerMVEL.compileExpression()如下,

String expression = "a+b";
ExpressionCompiler c = new ExpressionCompiler(formula,ctx );
//c.setVerifyOnly(true); // tried this but didn't help
c.compile() // this will throw exception if expression is invalid

This works for most of the cases like a+b* , but when expression is a+b) this is compiled as valid expression, the compiler is not complaining the extra parenthesis.这适用于大多数情况,例如a+b* ,但是当表达式为a+b)时,它被编译为有效表达式,编译器不会抱怨额外的括号。

Is there any way to make MVEL to verify this a+b) kind of expressions?有没有办法让 MVEL 来验证这种 a+b) 类型的表达式?

Finally found out how to validate expression without providing values or inputs.终于找到了如何在不提供值或输入的情况下验证表达式。 The solution is very simple, just need to enable ParserContext.setStrictTypeEnforcement(true)解决方法很简单,只需要启用ParserContext.setStrictTypeEnforcement(true)

Below is the code,下面是代码,

ParserContext ctx = new ParserContext();
ctx.setStrictTypeEnforcement(true); // this made the trick
ExpressionCompiler c = new ExpressionCompiler(formula,ctx );
c.compile();// now this throws exception for a+b) -> unqualified type in strict mode for: )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM