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   * See javax.faces.component._MethodExpressionToMethodBinding
36   * 
37   * @author Stan Silvert
38   */
39  public final class MethodExpressionToMethodBinding extends MethodBinding implements StateHolder
40  {
41  
42      private MethodExpression methodExpression;
43  
44      private boolean isTransient = false;
45  
46      public MethodExpressionToMethodBinding()
47      {
48          methodExpression = null;
49      }
50  
51      /** Creates a new instance of MethodExpressionToMethodBinding */
52      public MethodExpressionToMethodBinding(final MethodExpression methodExpression)
53      {
54          this.methodExpression = methodExpression;
55      }
56  
57      @Override
58      public String getExpressionString()
59      {
60          return methodExpression.getExpressionString();
61      }
62  
63      @Override
64      public Class getType(FacesContext facesContext) throws MethodNotFoundException
65      {
66  
67          try
68          {
69              return methodExpression.getMethodInfo(facesContext.getELContext()).getReturnType();
70          }
71          catch (javax.el.MethodNotFoundException e)
72          {
73              throw new javax.faces.el.MethodNotFoundException(e);
74          }
75      }
76  
77      @Override
78      public Object invoke(final FacesContext facesContext, final Object[] params) throws EvaluationException,
79                                                                                  MethodNotFoundException
80      {
81  
82          try
83          {
84              return methodExpression.invoke(facesContext.getELContext(), params);
85          }
86          catch (javax.el.MethodNotFoundException e)
87          {
88              throw new javax.faces.el.MethodNotFoundException(e);
89          }
90          catch (ELException e)
91          {
92              throw new EvaluationException(e.getCause());
93          }
94      }
95  
96      // -------- StateHolder methods -------------------------------------------
97  
98      public void restoreState(final FacesContext context, final Object state)
99      {
100         methodExpression = (MethodExpression) state;
101     }
102 
103     public Object saveState(final FacesContext context)
104     {
105         return methodExpression;
106     }
107 
108     public void setTransient(final boolean newTransientValue)
109     {
110         isTransient = newTransientValue;
111     }
112 
113     public boolean isTransient()
114     {
115         return isTransient;
116     }
117 
118 }