dev@javaserverfaces.java.net

Is JSF incompatible with generics?

From: Freire, Jose Luis \(PT - Lisbon\) <"Freire,>
Date: Tue, 9 Jun 2009 14:56:28 +0100

Using JSF 1.2.12, Facelets 1.1.14, Tomcat 6.0.18.

I'm not sure where to open this bug, since I'm not sure if it's JSF, Facelets, or EL related.

If we have this simple class:
public class GenericsTestController<E extends Object> {

       private E value;

       public E getValue() {
              return value;
       }

       public void setValue(E value) {
              this.value = value;
       }
}

And we have this class to use as a managed bean (integerTest):
public class IntegerTestController extends GenericsTestController<Integer> {

       public String add() {
              setValue(getValue()+1);
              return null;
       }
       public String subtract() {
              setValue(getValue()-1);
              return null;
       }
}

This JSF code will not work:
<h:form>
       Value:
       <h:inputText value="#{integerTest.value}" />
       <h:commandButton action="#{integerTest.add}" value="+" />
       <h:commandButton action="#{integerTest.subtract}" value="-" />
</h:form>

With this error:
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
       at genericstest.IntegerTestController.add(IntegerTestController.java:18)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
       at java.lang.reflect.Method.invoke(Method.java:597)
       at org.apache.el.parser.AstValue.invoke(AstValue.java:172)
       at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
       at com.sun.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:68)
       at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
       at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
       at javax.faces.component.UICommand.broadcast(UICommand.java:387)
       at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:475)
       at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:756)
       at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82)
       at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:100)
       at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
       at javax.faces.webapp.FacesServlet.service(FacesServlet.java:265)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
       at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
       at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
       at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
       at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
       at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
       at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
       at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
       at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
       at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
       at java.lang.Thread.run(Thread.java:619)

In fact, it fails with the same error even if we override getValue and setValue on IntegerTestController.

The only workaround is not to extend from GenericsTestController and implement the "value" property as Integer in the IntegerTestController.

Help anyone?

José Freire
Consulting - Financial Services Industry
Deloitte Consultores, S.A.
Deloitte | Edifício Atrium Saldanha, Praça Duque de Saldanha, 1 - 7º, 1050-094 Lisboa, Portugal
Tel/Direct: +(351) 210 422 500 | Fax: +(351) 210 422 950
jfreire_at_deloitte.pt | www.deloitte.pt

Please consider the environment before printing.




 
*Disclaimer:*
Deloitte refers to one or more of Deloitte Touche Tohmatsu, a Swiss Verein, its member firms, and their respective subsidiaries and affiliates. As a Swiss Verein (association), neither Deloitte Touche Tohmatsu nor any of its member firms has any liability for each other's acts or omissions. Each of the member firms is a separate and independent legal entity operating under the names "Deloitte," "Deloitte & Touche," "Deloitte Touche Tohmatsu," or other related names. Services are provided by the member firms or their subsidiaries or affiliates and not by the Deloitte Touche Tohmatsu Verein.
Privileged/Confidential Information may be contained in this message. If you are not the addressee indicated in this message (or responsible for delivery of the message to such person), you may not copy or deliver this message to anyone. In such case, you should destroy this message and kindly notify the sender by reply email. Please advise immediately if you or your employer do not consent to Internet email for messages of this kind. Opinions, conclusions and other information in this message that do not relate to the official business of my firm shall be understood as neither given nor endorsed by it.