View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.el.convert;
20  
21  import javax.el.ELException;
22  import javax.el.MethodExpression;
23  import javax.faces.component.StateHolder;
24  import javax.faces.context.FacesContext;
25  import javax.faces.el.EvaluationException;
26  import javax.faces.el.MethodBinding;
27  import javax.faces.el.MethodNotFoundException;
28  
29  /**
30   * Converts a MethodExpression to a MethodBinding. See JSF 1.2 spec section 5.8.4
31   * 
32   * ATTENTION: If you make changes to this class, treat javax.faces.component._MethodExpressionToMethodBinding
33   * accordingly.
34   * 
35   * @author Stan Silvert
36   * @see javax.faces.component._MethodExpressionToMethodBinding
37   */
38  public final class MethodExpressionToMethodBinding extends MethodBinding implements StateHolder
39  {
40  
41      private MethodExpression methodExpression;
42  
43      private boolean isTransient = false;
44  
45      public MethodExpressionToMethodBinding()
46      {
47          methodExpression = null;
48      }
49  
50      /** Creates a new instance of MethodExpressionToMethodBinding */
51      public MethodExpressionToMethodBinding(final MethodExpression methodExpression)
52      {
53          this.methodExpression = methodExpression;
54      }
55  
56      @Override
57      public String getExpressionString()
58      {
59          return methodExpression.getExpressionString();
60      }
61  
62      @Override
63      public Class getType(FacesContext facesContext) throws MethodNotFoundException
64      {
65  
66          try
67          {
68              return methodExpression.getMethodInfo(facesContext.getELContext()).getReturnType();
69          }
70          catch (javax.el.MethodNotFoundException e)
71          {
72              throw new javax.faces.el.MethodNotFoundException(e);
73          }
74      }
75  
76      @Override
77      public Object invoke(final FacesContext facesContext, final Object[] params) throws EvaluationException,
78                                                                                  MethodNotFoundException
79      {
80  
81          try
82          {
83              return methodExpression.invoke(facesContext.getELContext(), params);
84          }
85          catch (javax.el.MethodNotFoundException e)
86          {
87              throw new javax.faces.el.MethodNotFoundException(e);
88          }
89          catch (ELException e)
90          {
91              throw new EvaluationException(e.getCause());
92          }
93      }
94  
95      // -------- StateHolder methods -------------------------------------------
96  
97      public void restoreState(final FacesContext context, final Object state)
98      {
99          methodExpression = (MethodExpression) state;
100     }
101 
102     public Object saveState(final FacesContext context)
103     {
104         return methodExpression;
105     }
106 
107     public void setTransient(final boolean newTransientValue)
108     {
109         isTransient = newTransientValue;
110     }
111 
112     public boolean isTransient()
113     {
114         return isTransient;
115     }
116 
117 }