【问题标题】:Getting "argument type mismatch" during MVEL expression evaluation在 MVEL 表达式评估期间获取“参数类型不匹配”
【发布时间】:2016-10-23 11:15:38
【问题描述】:

对于以下代码 sn-p,我收到错误“参数类型不匹配”。 如果我使用注释行,它工作正常。

有人可以解释这种行为的原因吗?

有没有办法让我自己评估第一个表达式?

VariableResolverFactory functionFactory = new MapVariableResolverFactory();    
Object value = MVEL.eval("def StringValueWithLength(str) { int myLen = str.toString().length(); return String.valueOf(str) + \":\" + myLen; }; StringValueWithLength(\"qwert\");", functionFactory);
//Object value = MVEL.eval("def StringValueWithLength(str) { return String.valueOf(str) + \":\" + str.toString().length(); }; StringValueWithLength(\"qwert\");", functionFactory);
System.out.println("Value : " + value);

【问题讨论】:

  • 最新版本的 mvel 出现编译错误,您使用的是哪个版本? MapVariableResolverFactory 需要 Map 参数
  • JDK 1.8 和 mvel2-2.3.1.Final.

标签: java function evaluation mvel


【解决方案1】:

这似乎是 MVEL 中的错误/限制,我调试了 MVEL 代码。

return 语句中的参数类型被 MVEL 错误检测到,实际上是 String,但检测为 int

public static java.lang.String java.lang.String.valueOf(int)

对于表达式

Object value = MVEL.eval("def StringValueWithLength(str) { int myLen = str.toString().length(); return String.valueOf(str) + \":\" + myLen; }; StringValueWithLength(\"qwert\");", functionFactory);

然后扔了

Caused by: java.lang.IllegalArgumentException: argument type mismatch.

以下表达式按预期工作

String expr = "def StringValueWithLength(str) { int myLen = str.length(); return String.valueOf(str) + \":\" + myLen; }; StringValueWithLength(\"qwert\");"; // removed toString

String expr = "def StringValueWithLength(str) { int myLen = str.toString().length(); return str + \":\" + myLen; }; StringValueWithLength(\"qwert\");"; // removed String.valueOf

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-26
    相关资源
    最近更新 更多