Search code examples
jsfhashmapelmanaged-bean

Setting a member of a domain model object derived from Map


I have domain model object with dynamic keys and values which is derived from a map:

public class MyDomainModelObject extends HashMap<String, SomeClass<?>>
{
    private String documentType;

    public MyDomainModelObject()
    {
         super();
    }

    public String getDocumentType()
    {
        return documentType;
    }

    public void setDocumentType(final String documentType)
    {
         this.documentType = documentType;
    }
}

The getting and setting of the values stored in the map are working. But the setting of the member documentType leads to a ClassCastException.

The documentType is edited in a primefaces datatable cell editor:

<p:dataTable ...>
    <p:column ... />
        ...
    </p:column>
    <p:column>
        <p:cellEditor>
            <f:facet name="output">
                <h:outputText value="#{curObj.getDocumentType()}" />
            </f:facet>
            <f:facet name="input">
                <p:selectOneMenu value="#{curObj.documentType}">
                    <f:selectItems value="#{someBean.determineDocumentTypes(curObj)}" var="curDocType" itemLabel="#{curDocType}"
                        itemValue="#{curDocType}" />
                </p:selectOneMenu>
            </f:facet>
        </p:cellEditor>
    </p:column>
</p:dataTable>

When I change the documentType inside the table I get the following exception:

10:08:09,581 ERROR [stderr] (http-/0.0.0.0:9090-3) java.lang.ClassCastException: java.lang.String cannot be cast to SomeObject
10:08:09,581 ERROR [stderr] (http-/0.0.0.0:9090-3)  at MyDomainModel.put(MyDomainModel.java:1)
10:08:09,582 ERROR [stderr] (http-/0.0.0.0:9090-3)  at javax.el.MapELResolver.setValue(MapELResolver.java:262)
10:08:09,582 ERROR [stderr] (http-/0.0.0.0:9090-3)  at com.sun.faces.el.DemuxCompositeELResolver._setValue(DemuxCompositeELResolver.java:255)
10:08:09,582 ERROR [stderr] (http-/0.0.0.0:9090-3)  at com.sun.faces.el.DemuxCompositeELResolver.setValue(DemuxCompositeELResolver.java:281)
10:08:09,582 ERROR [stderr] (http-/0.0.0.0:9090-3)  at de.odysseus.el.tree.impl.ast.AstProperty.setValue(AstProperty.java:156)
10:08:09,582 ERROR [stderr] (http-/0.0.0.0:9090-3)  at de.odysseus.el.tree.impl.ast.AstEval.setValue(AstEval.java:87)
10:08:09,582 ERROR [stderr] (http-/0.0.0.0:9090-3)  at de.odysseus.el.TreeValueExpression.setValue(TreeValueExpression.java:146)
10:08:09,582 ERROR [stderr] (http-/0.0.0.0:9090-3)  at org.jboss.weld.el.WeldValueExpression.setValue(WeldValueExpression.java:64)
10:08:09,582 ERROR [stderr] (http-/0.0.0.0:9090-3)  at com.sun.faces.facelets.el.TagValueExpression.setValue(TagValueExpression.java:131)
10:08:09,583 ERROR [stderr] (http-/0.0.0.0:9090-3)  at javax.faces.component.UIInput.updateModel(UIInput.java:832)
10:08:09,583 ERROR [stderr] (http-/0.0.0.0:9090-3)  at javax.faces.component.UIInput.processUpdates(UIInput.java:749)
10:08:09,583 ERROR [stderr] (http-/0.0.0.0:9090-3)  at javax.faces.component.UIComponentBase.processUpdates(UIComponentBase.java:1290)
10:08:09,583 ERROR [stderr] (http-/0.0.0.0:9090-3)  at org.primefaces.component.celleditor.CellEditor.processUpdates(CellEditor.java:89)
10:08:09,583 ERROR [stderr] (http-/0.0.0.0:9090-3)  at org.primefaces.component.api.UIData.process(UIData.java:379)
10:08:09,583 ERROR [stderr] (http-/0.0.0.0:9090-3)  at org.primefaces.component.api.UIData.processChildren(UIData.java:360)
10:08:09,583 ERROR [stderr] (http-/0.0.0.0:9090-3)  at org.primefaces.component.api.UIData.processPhase(UIData.java:322)
10:08:09,583 ERROR [stderr] (http-/0.0.0.0:9090-3)  at org.primefaces.component.api.UIData.processUpdates(UIData.java:308)
10:08:09,583 ERROR [stderr] (http-/0.0.0.0:9090-3)  at org.primefaces.component.datatable.DataTable.processUpdates(DataTable.java:722)
10:08:09,584 ERROR [stderr] (http-/0.0.0.0:9090-3)  at com.sun.faces.context.PartialViewContextImpl$PhaseAwareVisitCallback.visit(PartialViewContextImpl.java:577)
10:08:09,584 ERROR [stderr] (http-/0.0.0.0:9090-3)  at com.sun.faces.component.visit.PartialVisitContext.invokeVisitCallback(PartialVisitContext.java:183)

Why is the javax.el.MapELResolver is used to set the value of documentType? The setDocumentType method is not executed but getDocumentType. I am a little bit confused.


Solution

  • Why is the javax.el.MapELResolver is used to set the value of documentType?

    It's because the base is an instance of java.util.Map. Basically, EL inspects like below:

    if (curObj instanceof Map) {
        // Use MapELResolver.
    }
    else (...) {
        // ...
    }
    else {
        // Use BeanELResolver.
    }
    

    You have 2 options:

    1. Use composition instead of inheritance.

      public class MyDomainModelObject {
      
          private String documentType;
          private Map<String, Foo> properties;
      
          // ...
      }
      
    2. Stick to inheritance and delegate to it (hacky, raw types, etc).

      public String getDocumentType() {
          return (String) ((Map) this).get("documentType");
      }
      
      public void setDocumentType(final String documentType) {
          ((Map) this).put("documentType", documentType);
      }
      

      Note that EL won't use it directly. It'll still grab it by Map#get() via MapELResolver. Those methods will only be used by your domain code. It will only break in there when you attempt to loop over it.