【问题标题】:MVEL to validate arithmetic expression without valuesMVEL 验证没有值的算术表达式
【发布时间】:2021-03-23 03:10:12
【问题描述】:

我想在不提供输入或值的情况下验证给定的算术表达式 (a+b) 是否有效。 我尝试使用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

这适用于大多数情况,例如 a+b*,但是当表达式为 a+b) 时,它被编译为有效表达式,编译器不会抱怨额外的括号。

有什么方法可以让 MVEL 来验证这种 a+b) 类型的表达式吗?

【问题讨论】:

    标签: java rules mvel


    【解决方案1】:

    终于找到了如何在不提供值或输入的情况下验证表达式。 解决方法很简单,只需要开启ParserContext.setStrictTypeEnforcement(true)

    下面是代码,

    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: )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-15
      • 2011-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-02
      • 1970-01-01
      相关资源
      最近更新 更多