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