Bug 58110 - JSP compiler points error to wrong line number when there are two same return statements.
Summary: JSP compiler points error to wrong line number when there are two same return...
Status: RESOLVED FIXED
Alias: None
Product: Tomcat 7
Classification: Unclassified
Component: Jasper (show other bugs)
Version: unspecified
Hardware: PC Linux
: P2 normal (vote)
Target Milestone: ---
Assignee: Tomcat Developers Mailing List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-07-07 10:30 UTC by LiuYan 刘研
Modified: 2015-07-07 17:47 UTC (History)
0 users



Attachments
Apache Tomcat_7.0.61 - Error report.html (2.52 KB, text/html)
2015-07-07 10:30 UTC, LiuYan 刘研
Details

Note You need to log in before you can comment on or make changes to this bug.
Description LiuYan 刘研 2015-07-07 10:30:54 UTC
Created attachment 32888 [details]
Apache Tomcat_7.0.61 - Error report.html

I forgot to delete the last return "" statement, and JSP compile point error to line number #8 which is same as the last return statement.

Code:
----------------------------------------------------------------------------------------------------
<%@ page pageEncoding='UTF-8' contentType='text/html'%>
<%@ page import = 'java.util.*' %>
<%!
String test ()
{
	List<Object> listObjectClasses = null;
	if (listObjectClasses==null || listObjectClasses.size()==0)
		return "";

	String sImageIconFile = null;
	for (Object objectClass : listObjectClasses)
	{
		//sImageIconFile = "active-directory-objectClass-" + (String)objectClass + ".png";
	}
	boolean isImageIconFileExist = false;
	if (! (sImageIconFile==null || sImageIconFile.isEmpty()))
	{
		sImageIconFile = "images/" + sImageIconFile;
		//File fileImageIcon = new File (application.getRealPath (sImageIconFile));
		//isImageIconFileExist = fileImageIcon.exists ();
	}
	return (sImageIconFile==null || sImageIconFile.isEmpty()) ? "" : "<img src='" + sImageIconFile + "'/>";
	return "";
		// This statement above will cause tomcat JDT compiler point error to line #8。 But if move this line into the above line, then compiler will point error to the right line number #23
}
%>
----------------------------------------------------------------------------------------------------

JSP compile exception
----------------------------------------------------------------------------------------------------
org.apache.jasper.JasperException: Unable to compile class for JSP: 

An error occurred at line: 8 in the jsp file: /test.jsp
Unreachable code
5: {
6: 	List<Object> listObjectClasses = null;
7: 	if (listObjectClasses==null || listObjectClasses.size()==0)
8: 		return "1";
9: 
10: 	String sImageIconFile = null;
11: 	for (Object objectClass : listObjectClasses)


Stacktrace:
	org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:103)
	org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:366)
	org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:485)
	org.apache.jasper.compiler.Compiler.compile(Compiler.java:379)
	org.apache.jasper.compiler.Compiler.compile(Compiler.java:354)
	org.apache.jasper.compiler.Compiler.compile(Compiler.java:341)
	org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:657)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
	org.apache.catalina.filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:108)
----------------------------------------------------------------------------------------------------

If I put the test() function into a .java file and use javac to compile it, javac will point error to right line number.
----------------------------------------------------------------------------------------------------
$ javac Test.java 
Picked up _JAVA_OPTIONS: -Dawt.useSystemAAFontSettings=lcd
Test.java:24: 错误: 无法访问的语句
		return "";
		^
1 个错误
----------------------------------------------------------------------------------------------------
Comment 1 Mark Thomas 2015-07-07 11:26:30 UTC
I've confirmed this error doesn't occur if you do something similar directly in Eclipse and I've been able to repeat this with trunk (9.0.x). I just need to check if JDT is returning the wrong line number or if Tomcat is making some invalid assumptions.
Comment 2 Mark Thomas 2015-07-07 11:48:38 UTC
I can confirm that this is a Tomcat bug in o.a.jasper.compiler.JavacErrorDetail

I'll investigate a fix...
Comment 3 Mark Thomas 2015-07-07 13:22:39 UTC
Fixed in trunk (for 9.0.x), 8.0.x (for 8.0.25) and 7.0.x (for 7.0.64).

Thanks for the report.
Comment 4 LiuYan 刘研 2015-07-07 17:02:16 UTC
Just downloaded https://svn.apache.org/repos/asf/tomcat/tc7.0.x/trunk/java/org/apache/jasper/compiler/ErrorDispatcher.java, compiled, put it into jasper.jar, restart tomcat 7.0.63, it works! 

Thanks for fixing!
Comment 5 Mark Thomas 2015-07-07 17:47:39 UTC
Great. Thanks for confirming the fix.

For future reference there is an easier way to apply this sort of patch than unpacking and repacking the JAR.

Just put the class into the right structure under $CATALINA_BASE/lib. In this case that would be $CATALINA_BASE/lib/org/apache/jasper/compiler/ErrorDispatcher.class

Just like WEB-INF/classes takes priority over WEB-INF/lib, classes in $CATALINA_BASE/lib take priority over JAR files.