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