1 Reply Latest reply on Sep 6, 2007 1:37 AM by Stuart Douglas

    does seam ignore generics in lists?

    Simon Bailey Newbie

      hi all,

      following _very_ strange behaviour:

      in a SFSB i have 3 lists defined as follows:


      [...]
      @Out (required=false)
      private List<TimetableItem> bscLectures;
      @Out (required=false)
      private List<TimetableItem> bscPracticals;
      private List<TimetableItem> selectedBscLectures;
      [...]
      


      in a .xhtml file:
      <h:selectManyCheckbox id="lectureBscList"
       value="#{timetable.selectedBscLectures}" layout="pageDirection">
       <s:selectItems value="#{timetable.bscLectures}" var="lecture"
       label="#{lecture.title} (#{lecture.type}): #{lecture.times}" />
      </h:selectManyCheckbox>
      [...]
      <h:dataTable id="bscPracticalList" var="bscPr"
       value="#{timetable.bscPracticals}"
       rendered="#{not empty timetable.bscPracticals}">
       <h:column>
       #{bscPr.title}
       </h:column>
       <h:column>
       <h:selectOneMenu value="#{bscPr.selected}">
       <s:selectItems value="#{bscPr.times}" var="n"
       label="#{n.toShortString()}"
       noSelectionLabel="Please select..."/>
       </h:selectOneMenu>
       </h:column>
      </h:dataTable>
      


      TimetableItem is not an Entity Bean, it's a helper class i've mashed together, with the following fields:
      private String title;
      private String type;
      private List<TimetableItemTime> times;
      private Object selected; // this should be a TimetableItem as well
      


      i realised that i've got some problems somewhere when i tried the following:

      for (TimetableItem t : this.selectedBscLectures)
       System.out.println(t.toString())); // changed for ease of reading
      


      and it was giving me ClassCastExceptions. The SelectOneMenu's were doing the same thing on TimetableItem.selected, which is why i changed that to an Object. After investigating further, i found out that my List selectedBscLectures is actually populated with Strings.

      2 questions:
      1.) why strings?
      2.) why isn't the JVM screaming loudly about violations of the Generics?

      [i may have an answer for 2.):]
      constructs like this aren't caught by the java compiler, but do cause ClassCastExceptions at runtime.
      import java.util.*;
      
      public class Test {
       public static void main(String args[]) {
       ArrayList<Integer> integerList = new ArrayList<Integer>();
       integerList.add(1);
       Object o = new String("Simon!");
       integerList.add((Integer)o);
       }
      }
      


      now the interesting question is, where are the ClassCastExceptions which should be happening going? they're not in my server log...

      [possible answer to 1.): i'm assuming because my beans aren't @Entity's, they have no @Id, which means that the JVM is using the toString() to identify them (the value of checkbox is a string representation of the object), and passing this into the Lists. but how do i avoid this?]

      regards,
      sb

        • 1. Re: does seam ignore generics in lists?
          Stuart Douglas Master

          You need to use a converter. If you are using entities you can use <s:convertEntity/> like so:

           <h:selectOneMenu value="#{bscPr.selected}">
           <s:selectItems value="#{bscPr.times}" var="n"
           label="#{n.toShortString()}"
           noSelectionLabel="Please select..."/>
           <s:convertEntity/>
           </h:selectOneMenu>
          


          otherwise you have to write your own, I think seam comes with an example of how to do this.