001    package org.apache.myfaces.tobago.component;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one or more
005     * contributor license agreements.  See the NOTICE file distributed with
006     * this work for additional information regarding copyright ownership.
007     * The ASF licenses this file to You under the Apache License, Version 2.0
008     * (the "License"); you may not use this file except in compliance with
009     * the License.  You may obtain a copy of the License at
010     *
011     *      http://www.apache.org/licenses/LICENSE-2.0
012     *
013     * Unless required by applicable law or agreed to in writing, software
014     * distributed under the License is distributed on an "AS IS" BASIS,
015     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016     * See the License for the specific language governing permissions and
017     * limitations under the License.
018     */
019    
020    import javax.el.ELException;
021    import javax.el.MethodExpression;
022    import javax.faces.component.StateHolder;
023    import javax.faces.context.FacesContext;
024    import javax.faces.el.EvaluationException;
025    import javax.faces.el.MethodBinding;
026    import javax.faces.el.MethodNotFoundException;
027    
028    @SuppressWarnings("deprecation")
029    public class MethodExpressionToMethodBinding extends MethodBinding implements StateHolder {
030    
031      private MethodExpression methodExpression;
032    
033      private boolean isTransient = false;
034    
035      public MethodExpressionToMethodBinding() {
036        methodExpression = null;
037      }
038    
039      /**
040       * Creates a new instance of MethodExpressionToMethodBinding
041       */
042      public MethodExpressionToMethodBinding(MethodExpression methodExpression) {
043        this.methodExpression = methodExpression;
044      }
045    
046      @Override
047      public String getExpressionString() {
048        return methodExpression.getExpressionString();
049      }
050    
051      public Class getType(FacesContext facesContext)
052          throws MethodNotFoundException {
053    
054        try {
055          return methodExpression.getMethodInfo(facesContext.getELContext()).getReturnType();
056        } catch (javax.el.MethodNotFoundException e) {
057          throw new javax.faces.el.MethodNotFoundException(e);
058        } catch (ELException e) {
059          throw new EvaluationException(e);
060        }
061      }
062    
063      public Object invoke(FacesContext facesContext, Object[] params)
064          throws EvaluationException {
065    
066        try {
067          return methodExpression.invoke(facesContext.getELContext(), params);
068        } catch (javax.el.MethodNotFoundException e) {
069          throw new javax.faces.el.MethodNotFoundException(e);
070        } catch (ELException e) {
071          throw new EvaluationException(e);
072        }
073      }
074    
075    
076      public void restoreState(FacesContext context, Object state) {
077        if (state != null) {
078          methodExpression = (MethodExpression) state;
079        }
080      }
081    
082      public Object saveState(FacesContext context) {
083        if (!isTransient) {
084          return methodExpression;
085        }
086        return null;
087      }
088    
089      public void setTransient(boolean newTransientValue) {
090        isTransient = newTransientValue;
091      }
092    
093      public boolean isTransient() {
094        return isTransient;
095      }
096    
097    }