title: How to evaluate MethodExpressions in JSF composite components
body: <p>I am not sure about the "correct" way to deal with method expressions in composite components.</p>
  
  <p>My composite uses a backing class with action methods. Theses perform some default actions or delegate to an action method passed by the composite user as an attribute:</p>
  
  <p>In using page:</p>
  
  <pre><code>&lt;my:component action="#{myBean.actionMethod}" /&gt;
  </code></pre>
  
  <p>Composite:</p>
  
  <pre><code>&lt;cc:interface componentType="mycomponentType"&gt;
    &lt;cc:attribute name="action" method-signature="java.lang.String action()" required="false" /&gt;
  &lt;/cc:interface&gt;
  
  &lt;cc:implementation&gt;
    &lt;h:commandButton value="submit" action="#{cc.componentAction}" /&gt;
  &lt;/cc:implementation&gt;
  </code></pre>
  
  <p>Backing class:</p>
  
  <pre><code>@FacesComponent("mycomponentType")
  public class UIMyComponent extends UINamingContainer {   
  
  public String action() {
      String outcome = "";
  
      ValueExpression ve = getValueExpression("action");
      String expression = ve.getExpressionString();
  
      FacesContext facesContext = FacesContext.getCurrentInstance();
      Application application = facesContext.getApplication();
      ELContext elContext = facesContext.getELContext();
      ExpressionFactory expressionFactory = application .getExpressionFactory();
  
      MethodExpression methodExpression = expressionFactory.createMethodExpression(elContext, expression, String.class, new Class[0]);
  
      outcome = (String) methodExpression.invoke(elContext, new Object[0]);
  
      if (outcome.equals("whatever")) {
          // set another outcome
      }
  
  
      return outcome;
  
  }
  
  }
  </code></pre>
  
  <p>The code above is working as expected, but I find it rather bulky and it creates a ValueExpression to retrieve the method-expression from the declared "action" attribute.</p>
  
  <p>UIComponentBase offers <code>getValueExpression("attributeName")</code> but there is nothing similar for MethodExpressions. </p>
  
  <p>So my Question is if there is a better way to evaluate <code>MethodExpressions</code> declared as attributes in composite components than the code above.</p>
  
  <p>Thx  </p>