首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MVEL没有按照空比较的要求工作

MVEL没有按照空比较的要求工作
EN

Stack Overflow用户
提问于 2014-12-20 20:51:38
回答 2查看 5.7K关注 0票数 0

根据[http://mvel.codehaus.org/Value+Emptiness ]中MVEL的文档

代码语言:javascript
复制
empty

如果上述条件为真,则应计算为真。

字符串的长度大于0,但仅由空格组成。

布尔值为false。

数值为0。

但它并没有达到预期的效果。

  1. 对于String case条件,当字符串length>0时,计算结果为false,但只包含空格(下面给出了使用的代码)。 字符串stringValue=“";Map contextMap=new HashMap();contextMap.put("stringValue",stringValue);System.out.println(MVEL.eval("stringValue ==空”,contextMap));
  2. 对于布尔值,它不管布尔值如何,都要计算为false (下面给出了所使用的代码); 布尔booleanValue=false;Map contextMap=new HashMap();contextMap.put("booleanValue",booleanValue);System.out.println(MVEL.eval("booleanValue ==空“,contextMap));
  3. 并且在比较整数时出现了误差。代码: 整数integerValue=0;Map contextMap=new HashMap();contextMap.put("integerValue",integerValue);System.out.println(MVEL.eval("integerValue ==空“,contextMap));

错误:

代码语言:javascript
复制
Exception in thread "main" [Error: failed to subEval expression]
[Near : {... integerValue == empty ....}]
                             ^
[Line: 1, Column: 17]
    at org.mvel2.compiler.AbstractParser.reduce(AbstractParser.java:2653)
    at org.mvel2.compiler.AbstractParser.arithmeticFunctionReduction(AbstractParser.java:2552)
    at org.mvel2.MVELInterpretedRuntime.parseAndExecuteInterpreted(MVELInterpretedRuntime.java:152)
    at org.mvel2.MVELInterpretedRuntime.parse(MVELInterpretedRuntime.java:49)
    at org.mvel2.MVEL.eval(MVEL.java:165)
    at com.Test1.main(Test1.java:15)
Caused by: java.lang.RuntimeException: cannot convert <> to a numeric type: class org.mvel2.compiler.BlankLiteral [200]
    at org.mvel2.math.MathProcessor.getNumber(MathProcessor.java:702)
    at org.mvel2.math.MathProcessor._doOperations(MathProcessor.java:214)
    at org.mvel2.math.MathProcessor.doOperations(MathProcessor.java:79)
    at org.mvel2.math.MathProcessor.doOperations(MathProcessor.java:48)
    at org.mvel2.util.ExecutionStack.op(ExecutionStack.java:178)
    at org.mvel2.compiler.AbstractParser.reduce(AbstractParser.java:2593)
    ... 5 more

为什么它不能按照文档的方式工作?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-12-22 07:21:06

这三个问题都与MVEL问题有关。

对于Q1 and Q2

对于empty运算符,MVEL有一个类BlankLiteral,并且它有一个方法

代码语言:javascript
复制
public boolean equals(Object obj) {
    if (obj == null || "".equals(valueOf(obj))) {
      return true;
    }
    else if (isNumeric(obj)) {
      return "0".equals(valueOf(obj));
    }
    else if (obj instanceof Collection) {
      return ((Collection) obj).size() == 0;
    }
    else if (obj.getClass().isArray()) {
      return getLength(obj) == 0;
    }
    return false;
  }

,它不处理您在中询问的案件

Q3,表达式integerValue == empty

MVEl尝试将empty转换为Number,而控件则进入此类MathProcessor.class

方法是

代码语言:javascript
复制
private static Double getNumber(Object in, int type) {
    if (in == null)
      return 0d;
    switch (type) {
      case BIG_DECIMAL:
        return ((Number) in).doubleValue();
      case DataTypes.BIG_INTEGER:
        return ((Number) in).doubleValue();
      case DataTypes.INTEGER:
      case DataTypes.W_INTEGER:
        return ((Number) in).doubleValue();
      case DataTypes.LONG:
      case DataTypes.W_LONG:
        return ((Number) in).doubleValue();
      case DataTypes.STRING:
        return Double.parseDouble((String) in);
      case DataTypes.FLOAT:
      case DataTypes.W_FLOAT:
        return ((Number) in).doubleValue();
      case DataTypes.DOUBLE:
      case DataTypes.W_DOUBLE:
        return (Double) in;
      case DataTypes.SHORT:
      case DataTypes.W_SHORT:
        return ((Number) in).doubleValue();
      case DataTypes.CHAR:
      case DataTypes.W_CHAR:
        return Double.parseDouble(String.valueOf((Character) in));
      case DataTypes.BOOLEAN:
      case DataTypes.W_BOOLEAN:
        return ((Boolean) in) ? 1d : 0d;
      case DataTypes.W_BYTE:
      case DataTypes.BYTE:
        return ((Byte) in).doubleValue();
    }

    throw new RuntimeException("cannot convert <" + in + "> to a numeric type: " + in.getClass() + " [" + type + "]");

}

观察调试模式下的值,

代码语言:javascript
复制
Object in - BlankLiteral
int type - 200

200只是DataTypes.EMPTY,它目前不被MVEL处理。因此,由于没有匹配的情况,它会抛出异常。

因此,还没有在MVEL中完全实现“空”

票数 2
EN

Stack Overflow用户

发布于 2015-01-02 12:45:24

我在以下提交中添加了空文本缺少的实现部分:https://github.com/mvel/mvel/commit/b44824e9ccbc565b6619714c92582014271e4bbb

下一个版本(2.2.3.Final)将提供该修补程序。谢谢你报告这件事。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27584219

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档