Bug 47371 - EL expression parser error when getter methed return an empty string
Summary: EL expression parser error when getter methed return an empty string
Status: RESOLVED FIXED
Alias: None
Product: Tomcat 6
Classification: Unclassified
Component: Jasper (show other bugs)
Version: unspecified
Hardware: PC Windows XP
: P1 normal (vote)
Target Milestone: default
Assignee: Tomcat Developers Mailing List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-06-16 02:55 UTC by Wang
Modified: 2011-05-10 08:55 UTC (History)
1 user (show)



Attachments
Patch for ELArithmetic (729 bytes, patch)
2011-04-22 12:30 UTC, gbt
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Wang 2009-06-16 02:55:55 UTC
There is a jsp file called test.jsp:
<%@ page contentType="text/html; charset=UTF-8" %>
<jsp:directive.page import="java.util.Map"/>
<jsp:directive.page import="java.util.HashMap"/>
<%!public class CellAttributes{
	private String cellValue;

	public CellAttributes(String cellValue) {
		this.cellValue = cellValue;
	}
	public String getCellValue() {
		return cellValue;
	}
} %>
<%Map<String,CellAttributes> currentRow = new HashMap<String, CellAttributes>();

currentRow.put("a",new CellAttributes(""));
currentRow.put("b",new CellAttributes("1"));
pageContext.setAttribute("currentRow",currentRow);

%>
<Html>
<BODY>
${currentRow['a'].cellValue+currentRow['b'].cellValue}
</BODY>
</Html>

throws exception:
java.lang.NumberFormatException: For input string: ""
	java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
	java.lang.Long.parseLong(Long.java:424)
	java.lang.Long.<init>(Long.java:671)
	org.apache.el.lang.ELArithmetic$LongDelegate.coerce(ELArithmetic.java:186)
	org.apache.el.lang.ELArithmetic.coerce(ELArithmetic.java:357)
	org.apache.el.lang.ELArithmetic.add(ELArithmetic.java:235)
	org.apache.el.parser.AstPlus.getValue(AstPlus.java:40)
	org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:186)
	org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:935)
	org.apache.jsp.new_jsp._jspService(new_jsp.java:84)
	org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

but I think it should be "1"
Comment 1 Mark Thomas 2009-06-19 15:50:31 UTC
Thanks for the test case, it really helps speed up the process of evaluating bugs.

+ is not a String concatenation operator in EL. However, I think this will achieve the result you want:
${currentRow['a'].cellValue}${currentRow['b'].cellValue}
Comment 2 gbt 2011-04-22 10:04:48 UTC
Even without string concatenation, you receive the NumberFormat exception with the following expression : 

${4+currentRow['a'].cellValue}

With ${4+currentRow['b'].cellValue}, the result is OK.

Why the empty string test is placed after the String instance test inside the method org.apache.el.lang.ELArithmetic.coerce ?
Comment 3 Konstantin Kolinko 2011-04-22 11:23:11 UTC
Confirming in trunk, both the original issue and the one in Comment 2.

From expression_language-2_2-mrel-spec.pdf:

The '+' operator is defined in chapter 1.7.1. There is the following step there:
Otherwise coerce both A and B to Long and apply operator

Coercion to Long is covered by chapter 1.18.3.
If A is null or "", return 0.

Throwing a NumberFormatException for the empty string is a bug.
Comment 4 gbt 2011-04-22 12:30:59 UTC
Created attachment 26928 [details]
Patch for ELArithmetic
Comment 5 Mark Thomas 2011-05-05 13:40:56 UTC
Thanks for the patch. It has been applied (along with a test case) to 7.0.x and will be included in 7.0.13 onwards.

The patch has also been proposed for 6.0.x.
Comment 6 Mark Thomas 2011-05-09 12:47:03 UTC
Drop the severity.
Comment 7 Mark Thomas 2011-05-10 08:55:49 UTC
Fixed in 6.0.x and will be included in 6.0.33 onwards.