jsfuicomponentsmethodexpression

Create MethodExpression containing a method with parameter of type Object generates parse exception


#{bean.method(TheObjectInstance)}
public class TheObject
{
   public String value0 = "value0";
}
    TheObject object = new TheObject();

    FacesContext context = FacesContext.getCurrentInstance();
    Application application = context.getApplication();
    ExpressionFactory factory = application.getExpressionFactory();

    //Create method expression
    MethodExpression methodExpression = factory.createMethodExpression(
       context.getELContext(), 
       "#{bean.method(" + object + ")}", 
       null, 
       new Class<?>[] {TheObject.class});
javax.servlet.ServletException: Encountered "@" at line 1, column 87.
Was expecting one of:
    "." ...
    "(" ...
    ")" ...
    "[" ...
    "," ...
    ";" ...
    ">" ...
    "gt" ...
    "<" ...
    "lt" ...
    ">=" ...
    "ge" ...
    "<=" ...
    "le" ...
    "==" ...
    "eq" ...
    "!=" ...
    "ne" ...
    "&&" ...
    "and" ...
    "||" ...
    "or" ...
    "*" ...
    "+" ...
    "-" ...
    "?" ...
    "/" ...
    "div" ...
    "%" ...
    "mod" ...
    "+=" ...
    "=" ...

Solution

  •     <h:form>
         <h:datatable var="object" value="#{bean.objects}">
          <h:commandbutton value="test" actionlistenner="#{bean.method(object)}"/>
        </h:datatable>
        </h:form>
    
        //Wrong implementation: the object is converted as object.getClass().toString()
        MethodExpression methodExpression = factory.createMethodExpression(
           context.getELContext(), 
           "#{bean.method(" + object + ")}", 
           null, 
           new Class<?>[] {TheObject.class});
    
        //Right implementation: we refer to object referenced by the datatable var. 
        MethodExpression methodExpression = factory.createMethodExpression(
           context.getELContext(), 
           "#{bean.method(object)}", 
           null, 
           new Class<?>[] {TheObject.class});