Fix second part of Comment 8 in https://issues.apache.org/bugzilla/show_bug.cgi?id...
authorkkolinko <kkolinko@13f79535-47bb-0310-9956-ffa450edef68>
Mon, 21 Dec 2009 21:51:29 +0000 (21:51 +0000)
committerkkolinko <kkolinko@13f79535-47bb-0310-9956-ffa450edef68>
Mon, 21 Dec 2009 21:51:29 +0000 (21:51 +0000)
Coerce result of composite EL expression (${a}${b}) from String to the expected type.

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

java/org/apache/jasper/compiler/Generator.java

index 1930a51..a0e11aa 100644 (file)
@@ -889,6 +889,54 @@ class Generator {
                 }
                 output.append(quote(tx.substring(mark, i)));
             }
+            if (expectedType != type && !expectedType.isAssignableFrom(type)) {
+                // Composite expression was evaluated to String
+                // We must coerce it to the expected type.
+                String className = JspUtil.getCanonicalName(expectedType);
+                String methodName = null;
+                if (expectedType.isPrimitive()) {
+                    if (expectedType == Boolean.TYPE) {
+                        className = "Boolean";
+                        methodName = ".booleanValue()";
+                    }
+                    else if (expectedType == Character.TYPE) {
+                        className = "Character";
+                        methodName = ".charValue()";
+                    }
+                    else if (expectedType == Byte.TYPE) {
+                        className = "Byte";
+                        methodName = ".byteValue()";
+                    }
+                    else if (expectedType == Short.TYPE) {
+                        className = "Short";
+                        methodName = ".shortValue()";
+                    }
+                    else if (expectedType == Integer.TYPE) {
+                        className = "Integer";
+                        methodName = ".intValue()";
+                    }
+                    else if (expectedType == Long.TYPE) {
+                        className = "Long";
+                        methodName = ".longValue()";
+                    }
+                    else if (expectedType == Float.TYPE) {
+                        className = "Float";
+                        methodName = ".floatValue()";
+                    }
+                    else if (expectedType == Double.TYPE) {
+                        className = "Double";
+                        methodName = ".doubleValue()";
+                    }
+                }
+                output.insert(0, "(("
+                        + className
+                        + ")org.apache.el.lang.ELSupport.coerceToType(");
+                output.append(",").append(className).append(".class))");
+                if (methodName != null) {
+                    output.insert(0, '(');
+                    output.append(methodName).append(')');
+                }
+            }
             return output.toString();
         }