Author: remy.maucherat(a)jboss.com
Date: 2008-04-15 10:05:49 -0400 (Tue, 15 Apr 2008)
New Revision: 584
Modified:
trunk/java/org/apache/el/lang/ELArithmetic.java
trunk/java/org/apache/el/lang/ELSupport.java
trunk/java/org/apache/el/parser/AstNegative.java
trunk/webapps/docs/changelog.xml
Log:
- Improve number types handling in EL.
Modified: trunk/java/org/apache/el/lang/ELArithmetic.java
===================================================================
--- trunk/java/org/apache/el/lang/ELArithmetic.java 2008-04-15 13:54:17 UTC (rev 583)
+++ trunk/java/org/apache/el/lang/ELArithmetic.java 2008-04-15 14:05:49 UTC (rev 584)
@@ -110,12 +110,12 @@
public final static class DoubleDelegate extends ELArithmetic {
protected Number add(Number num0, Number num1) {
- // could only be one of these
- if (num0 instanceof BigDecimal) {
- return ((BigDecimal) num0).add(new BigDecimal(num1.doubleValue()));
- } else if (num1 instanceof BigDecimal) {
- return ((new BigDecimal(num0.doubleValue()).add((BigDecimal) num1)));
- }
+ // could only be one of these
+ if (num0 instanceof BigDecimal) {
+ return ((BigDecimal) num0).add(new BigDecimal(num1.doubleValue()));
+ } else if (num1 instanceof BigDecimal) {
+ return ((new BigDecimal(num0.doubleValue()).add((BigDecimal) num1)));
+ }
return new Double(num0.doubleValue() + num1.doubleValue());
}
@@ -123,7 +123,7 @@
if (num instanceof Double)
return num;
if (num instanceof BigInteger)
- return new BigDecimal((BigInteger) num);
+ return new BigDecimal((BigInteger) num);
return new Double(num.doubleValue());
}
@@ -140,22 +140,22 @@
}
protected Number subtract(Number num0, Number num1) {
- // could only be one of these
- if (num0 instanceof BigDecimal) {
- return ((BigDecimal) num0).subtract(new BigDecimal(num1.doubleValue()));
- } else if (num1 instanceof BigDecimal) {
- return ((new BigDecimal(num0.doubleValue()).subtract((BigDecimal) num1)));
- }
+ // could only be one of these
+ if (num0 instanceof BigDecimal) {
+ return ((BigDecimal) num0).subtract(new BigDecimal(num1.doubleValue()));
+ } else if (num1 instanceof BigDecimal) {
+ return ((new BigDecimal(num0.doubleValue()).subtract((BigDecimal)
num1)));
+ }
return new Double(num0.doubleValue() - num1.doubleValue());
}
protected Number multiply(Number num0, Number num1) {
- // could only be one of these
- if (num0 instanceof BigDecimal) {
- return ((BigDecimal) num0).multiply(new BigDecimal(num1.doubleValue()));
- } else if (num1 instanceof BigDecimal) {
- return ((new BigDecimal(num0.doubleValue()).multiply((BigDecimal) num1)));
- }
+ // could only be one of these
+ if (num0 instanceof BigDecimal) {
+ return ((BigDecimal) num0).multiply(new BigDecimal(num1.doubleValue()));
+ } else if (num1 instanceof BigDecimal) {
+ return ((new BigDecimal(num0.doubleValue()).multiply((BigDecimal)
num1)));
+ }
return new Double(num0.doubleValue() * num1.doubleValue());
}
@@ -164,8 +164,6 @@
|| obj1 instanceof Double
|| obj0 instanceof Float
|| obj1 instanceof Float
- || (obj0 != null && (Double.TYPE == obj0.getClass() ||
Float.TYPE == obj0.getClass()))
- || (obj1 != null && (Double.TYPE == obj1.getClass() ||
Float.TYPE == obj1.getClass()))
|| (obj0 instanceof String && ELSupport
.isStringFloat((String) obj0)) || (obj1 instanceof String
&& ELSupport
.isStringFloat((String) obj1)));
@@ -326,8 +324,11 @@
return (obj != null && isNumberType(obj.getClass()));
}
- public final static boolean isNumberType(final Class type) {
- return type == (java.lang.Long.class) || type == Long.TYPE || type ==
(java.lang.Double.class) || type == Double.TYPE || type == (java.lang.Byte.class) || type
== Byte.TYPE || type == (java.lang.Short.class) || type == Short.TYPE || type ==
(java.lang.Integer.class) || type == Integer.TYPE || type == (java.lang.Float.class) ||
type == Float.TYPE || type == (java.math.BigInteger.class) || type ==
(java.math.BigDecimal.class);
+ public final static boolean isNumberType(final Class<?> type) {
+ return type == Long.TYPE || type == Double.TYPE ||
+ type == Byte.TYPE || type == Short.TYPE ||
+ type == Integer.TYPE || type == Float.TYPE ||
+ Number.class.isAssignableFrom(type);
}
/**
@@ -359,13 +360,12 @@
return coerce(ZERO);
}
- Class objType = obj.getClass();
- if (Character.class.equals(objType) || Character.TYPE == objType) {
+ if (obj instanceof Character) {
return coerce(new Short((short) ((Character) obj).charValue()));
}
throw new IllegalArgumentException(MessageFactory.get("error.convert",
- obj, objType, "Number"));
+ obj, obj.getClass(), "Number"));
}
protected abstract Number coerce(final String str);
Modified: trunk/java/org/apache/el/lang/ELSupport.java
===================================================================
--- trunk/java/org/apache/el/lang/ELSupport.java 2008-04-15 13:54:17 UTC (rev 583)
+++ trunk/java/org/apache/el/lang/ELSupport.java 2008-04-15 14:05:49 UTC (rev 584)
@@ -164,7 +164,7 @@
if (obj == null || "".equals(obj)) {
return Boolean.FALSE;
}
- if (obj instanceof Boolean || obj.getClass() == Boolean.TYPE) {
+ if (obj instanceof Boolean) {
return (Boolean) obj;
}
if (obj instanceof String) {
@@ -187,7 +187,7 @@
return new Character((char) ((Number) obj).shortValue());
}
Class objType = obj.getClass();
- if (obj instanceof Character || objType == Character.TYPE) {
+ if (obj instanceof Character) {
return (Character) obj;
}
@@ -259,14 +259,13 @@
return coerceToNumber((Number) obj, type);
}
- Class objType = obj.getClass();
- if (Character.class.equals(objType) || Character.TYPE == objType) {
+ if (obj instanceof Character) {
return coerceToNumber(new Short((short) ((Character) obj)
.charValue()), type);
}
throw new IllegalArgumentException(MessageFactory.get("error.convert",
- obj, objType, type));
+ obj, obj.getClass(), type));
}
protected final static Number coerceToNumber(final String val,
@@ -402,10 +401,7 @@
return (obj0 instanceof Double
|| obj1 instanceof Double
|| obj0 instanceof Float
- || obj1 instanceof Float
- || (obj0 != null && (Double.TYPE == obj0.getClass() || Float.TYPE
== obj0
- .getClass())) || (obj1 != null && (Double.TYPE == obj1
- .getClass() || Float.TYPE == obj1.getClass())));
+ || obj1 instanceof Float);
}
public final static boolean isDoubleStringOp(final Object obj0,
@@ -424,17 +420,7 @@
|| obj0 instanceof Short
|| obj1 instanceof Short
|| obj0 instanceof Byte
- || obj1 instanceof Byte
- || (obj0 != null && (Long.TYPE == obj0.getClass()
- || Integer.TYPE == obj0.getClass()
- || Character.TYPE == obj0.getClass()
- || Short.TYPE == obj0.getClass() || Byte.TYPE == obj0
- .getClass())) || (obj0 != null && (Long.TYPE == obj0
- .getClass()
- || Integer.TYPE == obj0.getClass()
- || Character.TYPE == obj0.getClass()
- || Short.TYPE == obj0.getClass() || Byte.TYPE == obj0
- .getClass())));
+ || obj1 instanceof Byte);
}
public final static boolean isStringFloat(final String str) {
Modified: trunk/java/org/apache/el/parser/AstNegative.java
===================================================================
--- trunk/java/org/apache/el/parser/AstNegative.java 2008-04-15 13:54:17 UTC (rev 583)
+++ trunk/java/org/apache/el/parser/AstNegative.java 2008-04-15 14:05:49 UTC (rev 584)
@@ -43,23 +43,22 @@
}
return new Long(-Long.parseLong((String) obj));
}
- Class type = obj.getClass();
- if (obj instanceof Long || Long.TYPE == type) {
+ if (obj instanceof Long) {
return new Long(-((Long) obj).longValue());
}
- if (obj instanceof Double || Double.TYPE == type) {
+ if (obj instanceof Double) {
return new Double(-((Double) obj).doubleValue());
}
- if (obj instanceof Integer || Integer.TYPE == type) {
+ if (obj instanceof Integer) {
return new Integer(-((Integer) obj).intValue());
}
- if (obj instanceof Float || Float.TYPE == type) {
+ if (obj instanceof Float) {
return new Float(-((Float) obj).floatValue());
}
- if (obj instanceof Short || Short.TYPE == type) {
+ if (obj instanceof Short) {
return new Short((short) -((Short) obj).shortValue());
}
- if (obj instanceof Byte || Byte.TYPE == type) {
+ if (obj instanceof Byte) {
return new Byte((byte) -((Byte) obj).byteValue());
}
Long num = (Long) coerceToNumber(obj, Long.class);
Modified: trunk/webapps/docs/changelog.xml
===================================================================
--- trunk/webapps/docs/changelog.xml 2008-04-15 13:54:17 UTC (rev 583)
+++ trunk/webapps/docs/changelog.xml 2008-04-15 14:05:49 UTC (rev 584)
@@ -112,6 +112,9 @@
<fix>
<bug>44428</bug>: NPE in function mapper. (markt)
</fix>
+ <fix>
+ Make number types handling more flexible in EL. (markt)
+ </fix>
</changelog>
</subsection>
</section>