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