Search code examples
arraysjsfclasscastexceptionuirepeatselectmanymenu

UISelectMany in ui:repeat causes java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to java.util.List


I have used the HashMap method for binding a list of checkboxes to a Map<String, Boolean> with success. This is nice since it allows you to have a dynamic number of checkboxes.

I'm trying to extend that to a variable length list of selectManyMenu. Being that they are selectMany, I'd like to be able to bind to a Map<String, List<MyObject>>. I have a single example working where I can bind a single selectManyMenu to a List<MyObject> and everything works fine, but whey I put a dynamic number of selectManyMenus inside a ui:repeat and attempt to bind to the map, I end up with weird results. The values are stored correctly in the map, as verified by the debugger, and calling toString(), but the runtime thinks the map's values are of type Object and not List<MyObject> and throws ClassCastExceptions when I try to access the map's keys.

I'm guessing it has something to do with how JSF determines the runtime type of the target of your binding, and since I am binding to a value in a Map, it doesn't know to get the type from the value type parameter of the map. Is there any workaround to this, other than probably patching Mojarra?

In general, how can I have a page with a dynamic number of selectManyMenus? Without, of course using Primefaces' <p:solveThisProblemForMe> component. (In all seriousness, Primefaces is not an option here, due to factors outside of my control.)

The question UISelectMany on a List<T> causes java.lang.ClassCastException: java.lang.String cannot be cast to T had some good information that I wasn't aware of, but I'm still having issues with this SSCE:

JSF:

  <ui:define name="content">
    <h:form>
      <ui:repeat value="#{testBean.itemCategories}" var="category">
        <h:selectManyMenu value="#{testBean.selectedItemMap[category]}">
          <f:selectItems value="#{testBean.availableItems}" var="item" itemValue="#{item}" itemLabel="#{item.name}"></f:selectItems>
          <f:converter binding="#{itemConverter}"></f:converter>
          <f:validator validatorId="test.itemValidator"></f:validator>
        </h:selectManyMenu>
      </ui:repeat>
      <h:commandButton value="Submit">
        <f:ajax listener="#{testBean.submitSelections}" execute="@form"></f:ajax>
      </h:commandButton>
    </h:form>
  </ui:define>

Converter:

@Named
public class ItemConverter implements Converter {

  @Inject
  ItemStore itemStore;

  @Override
  public Object getAsObject(FacesContext context, UIComponent component, String value) {
    return itemStore.getById(value);
  }

  @Override
  public String getAsString(FacesContext context, UIComponent component, Object value) {
    return Optional.of(value)
                   .filter(v -> Item.class.isInstance(v))
                   .map(v -> ((Item) v).getId())
                   .orElse(null);
  }
}

Backing Bean:

@Data
@Slf4j
@Named
@ViewScoped
public class TestBean implements Serializable {

  private static final long serialVersionUID = 1L;

  @Inject
  ItemStore itemStore;

  List<Item> availableItems;

  List<String> itemCategories;

  Map<String, List<Item>> selectedItemMap = new HashMap<>();

  public void initialize() {
    log.debug("Initialized TestBean");

    availableItems = itemStore.getAllItems();

    itemCategories = new ArrayList<>();
    itemCategories.add("First Category");
    itemCategories.add("Second Category");
    itemCategories.add("Third Category");
  }

  public void submitSelections(AjaxBehaviorEvent event) {
    log.debug("Submitted Selections");

    selectedItemMap.entrySet().forEach(entry -> {
      String key = entry.getKey();
      List<Item> items = entry.getValue();

      log.debug("Key: {}", key);

      items.forEach(item -> {
        log.debug("   Value: {}", item);
      });

    });

  }

}

ItemStore just contains a HashMap and delegate methods to access Items by their ID field.

Item:

@Data
@Builder
public class Item {
  private String id;
  private String name;
  private String value;
}

ItemListValidator:

@FacesValidator("test.itemValidator")
public class ItemListValidator implements Validator {

  @Override
  public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
    if (List.class.isInstance(value)) {

      if (((List) value).size() < 1) {
        throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_FATAL, "You must select at least 1 Admin Area", "You must select at least 1 Admin Area"));
      }
    }
  }

}

Error:

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to java.util.List

Stacktrace snipped but occurs on this line:

List<Item> items = entry.getValue();

What am I missing here?


Solution

  • As hinted in the related question UISelectMany on a List<T> causes java.lang.ClassCastException: java.lang.String cannot be cast to T, generic type arguments are unavailable during runtime. In other words, EL doesn't know you have a Map<String, List<Item>>. All EL knows is that you have a Map, so unless you explicitly specify a converter for the selected values, and a collection type for the collection, JSF will default to String for selected values and an object array Object[] for the collection. Do note that the [ in [Ljava.lang.Object indicates an array.

    Given that you want the collection type to be an instance of java.util.List, you need to specify the collectionType attribute with the FQN of the desired concrete implementation.

    <h:selectManyMenu ... collectionType="java.util.ArrayList">
    

    JSF will then make sure that the right collection type is being instantiated in order to fill the selected items and put in the model. Here's a related question where such a solution is being used but then for a different reason: org.hibernate.LazyInitializationException at com.sun.faces.renderkit.html_basic.MenuRenderer.convertSelectManyValuesForModel.


    Update: I should have tested the above theory. This doesn't work in Mojarra when the collection behind collectionType is in turn wrapped in another generic collection/map. Mojarra only checks the collectionType if the UISelectMany value itself already represents an instance of java.util.Collection. However, due to it being wrapped in a Map, its (raw) type becomes java.lang.Object and then Mojarra will skip the check for any collectionType.

    MyFaces did a better job in this in its UISelectMany renderer, it works over there.

    As far as I inspected Mojarra's source code, there's no way to work around this other way than replacing Map<String, List<Long>> by a List<Category> where Category is a custom object having String name and List<MyObject> selectedItems properties. True, this really kills the advantage of Map of having dynamic keys in EL, but it is what it is.

    Here's a MCVE using Long as item type (just substitute it with your MyObject):

    private List<Category> categories;
    private List<Long> availableItems;
    
    @PostConstruct
    public void init() {
        categories = Arrays.asList(new Category("one"), new Category("two"), new Category("three"));
        availableItems = Arrays.asList(1L, 2L, 3L, 4L, 5L);
    }
    
    public void submit() {
        categories.forEach(c -> {
            System.out.println("Name: " + c.getName());
    
            for (Long selectedItem : c.getSelectedItems()) {
                System.out.println("Selected item: " + selectedItem);
            }
        });
    
        // ...
    }
    

    public class Category {
    
        private String name;
        private List<Long> selectedItems;
    
        public Category(String name) {
            this.name = name;
        }
    
        // ...
    }
    

    <h:form>
        <ui:repeat value="#{bean.categories}" var="category">
            <h:selectManyMenu value="#{category.selectedItems}" converter="javax.faces.Long">
                <f:selectItems value="#{bean.availableItems}" />
            </h:selectManyMenu>
        </ui:repeat>
        <h:commandButton value="submit" action="#{bean.submit}">
            <f:ajax execute="@form" />
        </h:commandButton>
    </h:form>
    

    Do note that collectionType is unnecessary here. Only the converter is still necessary.

    Unrelated to the concrete problem, I'd like to point out that selectedItemMap.entrySet().forEach(entry -> { String key ...; List<Item> items ...;}) can be simplified to selectedItemMap.forEach((key, items) -> {}) and that ItemListValidator is unnecessary if you just use required="true" on the input component.