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.component.StateHolder;
25  import javax.faces.context.FacesContext;
26  import javax.faces.el.EvaluationException;
27  import javax.faces.el.MethodBinding;
28  import javax.faces.el.MethodNotFoundException;
29  
30  /**
31   * Converts a MethodExpression to a MethodBinding.  
32   * See JSF 1.2 spec section 5.8.4
33   *
34   * ATTENTION: If you make changes to this class, treat 
35   * org.apache.myfaces.el.convert.MethodExpressionToMethodBinding
36   * accordingly.
37   *
38   * @author Stan Silvert
39   * @see org.apache.myfaces.el.convert.MethodExpressionToMethodBinding
40   */
41  class _MethodExpressionToMethodBinding extends MethodBinding implements StateHolder {
42      
43      private MethodExpression methodExpression;
44      
45      private boolean isTransient = false;
46      
47      public _MethodExpressionToMethodBinding() {
48          methodExpression = null;
49      }
50  
51      /** Creates a new instance of MethodExpressionToMethodBinding */
52      public _MethodExpressionToMethodBinding(MethodExpression methodExpression) {
53          this.methodExpression = methodExpression;
54      }
55      
56      @Override
57      public String getExpressionString()
58      {
59          return methodExpression.getExpressionString();
60      }
61  
62      public Class getType(FacesContext facesContext) 
63          throws MethodNotFoundException {
64          
65          try {
66              return methodExpression.getMethodInfo(facesContext.getELContext()).getReturnType();
67          } catch (javax.el.MethodNotFoundException e) {
68              throw new javax.faces.el.MethodNotFoundException(e);
69          } catch (ELException e) {
70              throw new EvaluationException(e);
71          }
72      }
73  
74      public Object invoke(FacesContext facesContext, Object[] params) 
75          throws EvaluationException, MethodNotFoundException {
76          
77          try {
78              return methodExpression.invoke(facesContext.getELContext(), params);
79          } catch (javax.el.MethodNotFoundException e) {
80              throw new javax.faces.el.MethodNotFoundException(e);
81          } catch (ELException e) {
82              throw new EvaluationException(e);
83          }
84      }
85  
86  // -------- StateHolder methods -------------------------------------------    
87      
88      public void restoreState(FacesContext context, Object state) {
89          if(state != null)
90              methodExpression = (MethodExpression)state;
91      }
92  
93      public Object saveState(FacesContext context) {
94          if(!isTransient)
95              return methodExpression;
96          return null;
97      }
98  
99      public void setTransient(boolean newTransientValue) {
100         isTransient = newTransientValue;
101     }
102 
103     public boolean isTransient() {
104         return isTransient;
105     }
106     
107 }