需要用el表达式取值的数据,除了对象,最好都是String类型, (二)

2014-11-24 09:53:49 · 作者: · 浏览: 2
a luationContext ctx)
throws ELException
{
Object obj0 = this.children[0].getValue(ctx);
Object obj1 = this.children[1].getValue(ctx);
return ELArithmetic.add(obj0, obj1);
}
}

其委托给ELArithmetic.add:

Java代码 public static final DoubleDelegate DOUBLE = new DoubleDelegate();

public static final LongDelegate LONG = new LongDelegate();

private static final Long ZERO = Long.valueOf(0L);

public static final Number add(Object obj0, Object obj1) {
if ((obj0 == null) && (obj1 == null))
return Long.valueOf(0L);
ELArithmetic delegate;
if (BIGDECIMAL.matches(obj0, obj1))
delegate = BIGDECIMAL;
else if (DOUBLE.matches(obj0, obj1))
if (BIGINTEGER.matches(obj0, obj1))
delegate = BIGDECIMAL;
else
delegate = DOUBLE;
else if (BIGINTEGER.matches(obj0, obj1))
delegate = BIGINTEGER;
else {
delegate = LONG;
}
Number num0 = delegate.coerce(obj0);
Number num1 = delegate.coerce(obj1);

return delegate.add(num0, num1);
}

此处委托给了各种delegate计算,其+的实现:

Java代码 public static final class LongDelegate extends ELArithmetic
{
protected Number add(Number num0, Number num1)
{
return Long.valueOf(num0.longValue() + num1.longValue());
}

从这里我们可以看出其实现。

而且其规范中都规定了具体字面量的东西:

写道
1.3 Literals
There are literals for boolean, integer, floating point, string, and null in an eva l-
expression.
■ Boolean - true and false
■ Integer - As defined by the IntegerLiteral construct in Section 1.19
■ Floating point - As defined by the FloatingPointLiteral construct in
Section 1.19
■ String - With single and double quotes - " is escaped as \", ' is escaped as \',
and \ is escaped as \\. Quotes only need to be escaped in a string value enclosed
in the same type of quote
■ Null - null

也规定了操作符的运算规则,如+ - *:

Java代码 1.3 Literals
There are literals for boolean, integer, floating point, string, and null in an eva l-expression.
■ Boolean - true and false
■ Integer - As defined by the IntegerLiteral construct in Section 1.19
■ Floating point - As defined by the FloatingPointLiteral construct in
Section 1.19
■ String - With single and double quotes - " is escaped as \", ' is escaped as \',and \ is escaped as \\. Quotes only need to be escaped in a string value enclosed in the same type of quote
■ Null - null
■ If operator is -, return A.subtract(B)
■ If operator is *, return A.multiply(B)
■ If A or B is a Float, Double,or String containing ., e,or E:
■ If A or B is BigInteger, coerce both A and B to BigDecimal and apply operator.
■ Otherwise, coerce both A and B to Double and apply operator
■ If A or B is BigInteger, coerce both to BigInteger and then:
■ If operator is +, return A.add(B)
■ If operator is -, return A.subtract(B)
■ If operator is *, return A.multiply(B)
■ Otherwise coerce both A and B to Long and apply operator
■ If operator results in exception, error

如Integer型,直接交给前边介绍的IntegerLiteral。

即规范中其实已经规范了这些,但是就像java里的一些东西,虽然规范规定了(如排序时 很多人有时候使用 return a-b; 如果a是负数则可能溢出),但是还是很容易出错。