From: markt Date: Mon, 9 Jun 2008 16:41:58 +0000 (+0000) Subject: Make forced coercion of null and "" to zero optional. It is enabled by default, as... X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=6058b9c9fc19b0cecd58f89c11c108bfc795737b;p=tomcat7.0 Make forced coercion of null and "" to zero optional. It is enabled by default, as per the spec. Patch by Nils Eckert. git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@665756 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/java/org/apache/el/parser/AstValue.java b/java/org/apache/el/parser/AstValue.java index 3cf27cdd4..0a5367544 100644 --- a/java/org/apache/el/parser/AstValue.java +++ b/java/org/apache/el/parser/AstValue.java @@ -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); diff --git a/webapps/docs/config/systemprops.xml b/webapps/docs/config/systemprops.xml index 3eea6ffe5..10464c9c6 100644 --- a/webapps/docs/config/systemprops.xml +++ b/webapps/docs/config/systemprops.xml @@ -48,6 +48,18 @@ +
+ + + +

If true, when coercing expressions to numbers + "" and null will be coerced to zero as required + by the specification. If not specified, the default value of + true will be used.

+
+ +
+