webtier@glassfish.java.net

JSP Compilation error (Mixing Generics and Autoboxing) - Glassfish 2.1

From: <webtier_at_javadesktop.org>
Date: Mon, 07 Dec 2009 16:43:18 PST

In Glassfish 2.1, when we mix Generics with auto-boxing of variables, the JSP compiler throws an exception. To reproduce the problem you can do the following. <br/> <br/>

In a web application create a java class say, "Macros.java" with the following method : <br/><br/>

package com.tests; <br/><br/>

import javax.servlet.http.HttpServletRequest; <br/><br/>

public class Macros { <br/><br/>
  
  @SuppressWarnings("unchecked")<br/>
  public static <T> T attr(String attribute, HttpServletRequest request){<br/>
    return (T)request.getAttribute(attribute);<br/>
  }<br/><br/>
  
}<br/>

<br/><br/>

The JSP is:<br/><br/>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <br/>
<%_at_page contentType="text/html;charset=utf-8" session="false"%> <br/><br/>

<%_at_page import="com.tests.Macros"%><br/><br/>

&lt;%<br/><br/>
  request.setAttribute("integer", 24);<br/>
  [b]int a = RestMacros.attr("integer", request);<br/><br/>[/b]
%&gt;<br/><br/>

<html><br/>
<head><br/>
<title>GlassFish Test Page</title><br/>
</head><br/>
<body><br/>
  &lt;h2&gt;Test Attributes&lt;/h2&gt;<br/>
  &lt;ul>&lt;br/>
    &lt;li>&lt;%=a %>&lt;/li&gt;<br/>
  &lt;/ul><br/>
</body><br/>
</html><br/><br/>

You get a compilation error : <br/>

PWC6033: Unable to compile class for JSP PWC6197<br/>
type parameters of T cannot be determined; no unique maximal instance exists for type <br/>variable T with upper bounds int,java.lang.Object <br/><br/>

However, when you change <br/><br/>

int a = RestMacros.attr("integer", request); --> Integer a = RestMacros.attr("integer", request); <br/><br/>

there is no error. This is a bug in Glassfish 2.1. Are they any known workarounds / patches? <br/><br/>

Thanks, <br/>
Rahul
[Message sent by forum member 'tikurahul' ]

http://forums.java.net/jive/thread.jspa?messageID=375259