Make forced coercion of null and "" to zero optional. It is enabled by default, as...
authormarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Mon, 9 Jun 2008 16:41:58 +0000 (16:41 +0000)
committermarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Mon, 9 Jun 2008 16:41:58 +0000 (16:41 +0000)
Patch by Nils Eckert.

git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@665756 13f79535-47bb-0310-9956-ffa450edef68

java/org/apache/el/parser/AstValue.java
webapps/docs/config/systemprops.xml

index 3cf27cd..0a53675 100644 (file)
@@ -38,6 +38,10 @@ import org.apache.el.util.ReflectionUtil;
  */
 public final class AstValue extends SimpleNode {
 
+    protected static final boolean COERCE_TO_ZERO =
+        Boolean.valueOf(System.getProperty(
+                "org.apache.el.parser.COERCE_TO_ZERO", "true")).booleanValue();
+    
     protected static class Target {
         protected Object base;
 
@@ -129,12 +133,28 @@ public final class AstValue extends SimpleNode {
         Target t = getTarget(ctx);
         ctx.setPropertyResolved(false);
         ELResolver resolver = ctx.getELResolver();
-        resolver.setValue(ctx, t.base, t.property, 
-                       // coerce to the expected type
-                       ELSupport.coerceToType(value, 
-                                       resolver.getType(ctx, t.base, t.property)));
+
+        // coerce to the expected type
+        Class<?> targetClass = resolver.getType(ctx, t.base, t.property);
+        if (COERCE_TO_ZERO == true
+                || !isAssignable(value, targetClass)) {
+            value = ELSupport.coerceToType(value, targetClass);
+        }
+        resolver.setValue(ctx, t.base, t.property, value);
+    }
+
+    private boolean isAssignable(Object value, Class<?> targetClass) {
+        if (targetClass == null) {
+            return false;
+        } else if (value != null && targetClass.isPrimitive()) {
+            return false;
+        } else if (value != null && !targetClass.isInstance(value)) {
+            return false;
+        }
+        return true;
     }
 
+
     public MethodInfo getMethodInfo(EvaluationContext ctx, Class[] paramTypes)
             throws ELException {
         Target t = getTarget(ctx);
index 3eea6ff..10464c9 100644 (file)
 
 </section>
 
+<section name="Expression Language">
+  <properties>
+
+    <property name="org.apache.el.parser.COERCE_TO_ZERO">
+      <p>If <code>true</code>, when coercing expressions to numbers
+      <code>""</code> and <code>null</code> will be coerced to zero as required
+      by the specification. If not specified, the default value of
+      <code>true</code> will be used.</p>
+    </property>
+
+  </properties>
+</section>
 
 <section name="Jasper">
   <properties>