Author: markt Date: Mon Jun 9 09:41:58 2008 New Revision: 665756 URL: http://svn.apache.org/viewvc?rev=665756&view=rev Log: Make forced coercion of null and "" to zero optional. It is enabled by default, as per the spec. Patch by Nils Eckert.
Modified: tomcat/trunk/java/org/apache/el/parser/AstValue.java tomcat/trunk/webapps/docs/config/systemprops.xml Modified: tomcat/trunk/java/org/apache/el/parser/AstValue.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/el/parser/AstValue.java?rev=665756&r1=665755&r2=665756&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/el/parser/AstValue.java (original) +++ tomcat/trunk/java/org/apache/el/parser/AstValue.java Mon Jun 9 09:41:58 2008 @@ -38,6 +38,10 @@ */ 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 @@ 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); Modified: tomcat/trunk/webapps/docs/config/systemprops.xml URL: http://svn.apache.org/viewvc/tomcat/trunk/webapps/docs/config/systemprops.xml?rev=665756&r1=665755&r2=665756&view=diff ============================================================================== --- tomcat/trunk/webapps/docs/config/systemprops.xml (original) +++ tomcat/trunk/webapps/docs/config/systemprops.xml Mon Jun 9 09:41:58 2008 @@ -48,6 +48,18 @@ </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> --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]