Coverage Report - javax.faces.component._MethodBindingToMethodExpression
 
Classes in this File Line Coverage Branch Coverage Complexity
_MethodBindingToMethodExpression
0%
0/77
0%
0/38
0
_MethodBindingToMethodExpression$1
0%
0/2
N/A
0
_MethodBindingToMethodExpression$2
0%
0/2
N/A
0
_MethodBindingToMethodExpression$Invoker
N/A
N/A
0
 
 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.ELContext;
 23  
 import javax.el.ELException;
 24  
 import javax.el.MethodExpression;
 25  
 import javax.el.MethodInfo;
 26  
 import javax.el.MethodNotFoundException;
 27  
 import javax.el.PropertyNotFoundException;
 28  
 import javax.faces.context.FacesContext;
 29  
 import javax.faces.el.EvaluationException;
 30  
 import javax.faces.el.MethodBinding;
 31  
 
 32  
 /**
 33  
  * Converts a MethodBinding to a MethodExpression
 34  
  * 
 35  
  * TODO: find a way to share the implementation of class with impl.
 36  
  * 
 37  
  * @author Stan Silvert
 38  
  */
 39  
 @SuppressWarnings("deprecation")
 40  0
 class _MethodBindingToMethodExpression extends MethodExpression implements StateHolder
 41  
 {
 42  0
     private static final Class[] EXPECTED_TYPES = new Class[] { MethodBinding.class, StateHolder.class };
 43  
 
 44  
     private MethodBinding methodBinding;
 45  
 
 46  
     private boolean _transientFlag;
 47  
 
 48  
     private transient MethodInfo methodInfo;
 49  
 
 50  
     /**
 51  
      * No-arg constructor used during restoreState
 52  
      */
 53  
     protected _MethodBindingToMethodExpression()
 54  0
     {
 55  0
     }
 56  
 
 57  
     /** Creates a new instance of MethodBindingToMethodExpression */
 58  
     public _MethodBindingToMethodExpression(MethodBinding methodBinding)
 59  0
     {
 60  0
         checkNullArgument(methodBinding, "methodBinding");
 61  0
         this.methodBinding = methodBinding;
 62  0
     }
 63  
 
 64  
     /**
 65  
      * Return the wrapped MethodBinding.
 66  
      */
 67  
     public MethodBinding getMethodBinding()
 68  
     {
 69  0
         return methodBinding;
 70  
     }
 71  
     
 72  
     void setMethodBinding(MethodBinding methodBinding)
 73  
     {
 74  0
         this.methodBinding = methodBinding;
 75  0
     }
 76  
 
 77  
     /**
 78  
      * Note: MethodInfo.getParamTypes() may incorrectly return an empty class array if invoke() has not been called.
 79  
      * 
 80  
      * @throws IllegalStateException
 81  
      *             if expected params types have not been determined.
 82  
      */
 83  
     public MethodInfo getMethodInfo(ELContext context) throws PropertyNotFoundException, MethodNotFoundException,
 84  
             ELException
 85  
     {
 86  0
         checkNullArgument(context, "elcontext");
 87  0
         checkNullState(methodBinding, "methodBinding");
 88  
 
 89  0
         if (methodInfo == null)
 90  
         {
 91  0
             final FacesContext facesContext = (FacesContext) context.getContext(FacesContext.class);
 92  0
             if (facesContext != null)
 93  
             {
 94  0
                 methodInfo = invoke(new Invoker<MethodInfo>()
 95  0
                 {
 96  
                     public MethodInfo invoke()
 97  
                     {
 98  0
                         return new MethodInfo(null, methodBinding.getType(facesContext), null);
 99  
                     }
 100  
                 });
 101  
             }
 102  
         }
 103  0
         return methodInfo;
 104  
     }
 105  
 
 106  
     public Object invoke(ELContext context, final Object[] params) throws PropertyNotFoundException,
 107  
             MethodNotFoundException, ELException
 108  
     {
 109  0
         checkNullArgument(context, "elcontext");
 110  0
         checkNullState(methodBinding, "methodBinding");
 111  0
         final FacesContext facesContext = (FacesContext) context.getContext(FacesContext.class);
 112  0
         if (facesContext != null)
 113  
         {
 114  0
             return invoke(new Invoker<Object>()
 115  0
             {
 116  
                 public Object invoke()
 117  
                 {
 118  0
                     return methodBinding.invoke(facesContext, params);
 119  
                 }
 120  
             });
 121  
         }
 122  0
         return null;
 123  
     }
 124  
 
 125  
     public boolean isLiteralText()
 126  
     {
 127  0
         if (methodBinding == null)
 128  0
             throw new IllegalStateException("methodBinding is null");
 129  0
         String expr = methodBinding.getExpressionString();
 130  0
         return !(expr.startsWith("#{") && expr.endsWith("}"));
 131  
     }
 132  
 
 133  
     public String getExpressionString()
 134  
     {
 135  0
         return methodBinding.getExpressionString();
 136  
     }
 137  
 
 138  
     public Object saveState(FacesContext context)
 139  
     {
 140  0
         if (!isTransient())
 141  
         {
 142  0
             if (methodBinding instanceof StateHolder)
 143  
             {
 144  0
                 Object[] state = new Object[2];
 145  0
                 state[0] = methodBinding.getClass().getName();
 146  0
                 state[1] = ((StateHolder) methodBinding).saveState(context);
 147  0
                 return state;
 148  
             }
 149  
             else
 150  
             {
 151  0
                 return methodBinding;
 152  
             }
 153  
         }
 154  0
         return null;
 155  
     }
 156  
 
 157  
     public void restoreState(FacesContext context, Object state)
 158  
     {
 159  0
         if (state instanceof MethodBinding)
 160  
         {
 161  0
             methodBinding = (MethodBinding) state;
 162  0
             methodInfo = null;
 163  
         }
 164  0
         else if (state != null)
 165  
         {
 166  0
             Object[] values = (Object[]) state;
 167  0
             methodBinding = (MethodBinding) _ClassUtils.newInstance(values[0].toString(), EXPECTED_TYPES);
 168  0
             ((StateHolder) methodBinding).restoreState(context, values[1]);
 169  0
             methodInfo = null;
 170  
         }
 171  0
     }
 172  
 
 173  
     public void setTransient(boolean transientFlag)
 174  
     {
 175  0
         _transientFlag = transientFlag;
 176  0
     }
 177  
 
 178  
     public boolean isTransient()
 179  
     {
 180  0
         return _transientFlag;
 181  
     }
 182  
 
 183  
     @Override
 184  
     public int hashCode()
 185  
     {
 186  0
         final int PRIME = 31;
 187  0
         int result = 1;
 188  0
         result = PRIME * result + ((methodBinding == null) ? 0 : methodBinding.hashCode());
 189  0
         return result;
 190  
     }
 191  
 
 192  
     @Override
 193  
     public boolean equals(Object obj)
 194  
     {
 195  0
         if (this == obj)
 196  0
             return true;
 197  0
         if (obj == null)
 198  0
             return false;
 199  0
         if (getClass() != obj.getClass())
 200  0
             return false;
 201  0
         final _MethodBindingToMethodExpression other = (_MethodBindingToMethodExpression) obj;
 202  0
         if (methodBinding == null)
 203  
         {
 204  0
             if (other.methodBinding != null)
 205  0
                 return false;
 206  
         }
 207  0
         else if (!methodBinding.equals(other.methodBinding))
 208  0
             return false;
 209  0
         return true;
 210  
     }
 211  
 
 212  
     private void checkNullState(Object notNullInstance, String instanceName)
 213  
     {
 214  0
         if (notNullInstance == null)
 215  0
             throw new IllegalStateException(instanceName + " is null");
 216  0
     }
 217  
 
 218  
     private void checkNullArgument(Object notNullInstance, String instanceName)
 219  
     {
 220  0
         if (notNullInstance == null)
 221  0
             throw new IllegalArgumentException(instanceName + " is null");
 222  0
     }
 223  
 
 224  
     private <T> T invoke(Invoker<T> invoker)
 225  
     {
 226  
         try
 227  
         {
 228  0
             return invoker.invoke();
 229  
         }
 230  0
         catch (javax.faces.el.MethodNotFoundException e)
 231  
         {
 232  0
             throw new MethodNotFoundException(e.getMessage(), e);
 233  
         }
 234  0
         catch (EvaluationException e)
 235  
         {
 236  0
             throw new ELException(e.getMessage(), e);
 237  
         }
 238  
     }
 239  
 
 240  
     private interface Invoker<T>
 241  
     {
 242  
         T invoke();
 243  
     }
 244  
 }