Coverage Report - org.apache.myfaces.el.convert.ValueBindingToValueExpression
 
Classes in this File Line Coverage Branch Coverage Complexity
ValueBindingToValueExpression
0%
0/76
0%
0/38
2.583
ValueBindingToValueExpression$1
0%
0/2
N/A
2.583
ValueBindingToValueExpression$2
0%
0/2
N/A
2.583
ValueBindingToValueExpression$3
0%
0/2
N/A
2.583
ValueBindingToValueExpression$4
0%
0/3
N/A
2.583
ValueBindingToValueExpression$Invoker
N/A
N/A
2.583
 
 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.el.convert;
 20  
 
 21  
 import java.util.logging.Level;
 22  
 import java.util.logging.Logger;
 23  
 
 24  
 import javax.el.ELContext;
 25  
 import javax.el.ELException;
 26  
 import javax.el.PropertyNotFoundException;
 27  
 import javax.el.PropertyNotWritableException;
 28  
 import javax.el.ValueExpression;
 29  
 import javax.faces.component.StateHolder;
 30  
 import javax.faces.context.FacesContext;
 31  
 import javax.faces.el.EvaluationException;
 32  
 import javax.faces.el.ValueBinding;
 33  
 
 34  
 import org.apache.myfaces.shared.util.ClassUtils;
 35  
 
 36  
 /**
 37  
  * Wraps a ValueBinding inside a ValueExpression. Also allows access to the original ValueBinding object.
 38  
  * 
 39  
  * Although ValueExpression implements Serializable, this class implements StateHolder instead.
 40  
  * 
 41  
  * ATTENTION: If you make changes to this class, treat {@link ValueBindingToValueExpression} accordingly.
 42  
  * 
 43  
  * @author Stan Silvert
 44  
  * @see javax.faces.component._ValueBindingToValueExpression
 45  
  */
 46  
 @SuppressWarnings("deprecation")
 47  0
 public class ValueBindingToValueExpression extends ValueExpression implements StateHolder
 48  
 {
 49  
     private static final long serialVersionUID = 8071429285360496554L;
 50  
 
 51  
     //private static final Log logger = LogFactory.getLog(ValueBindingToValueExpression.class);
 52  0
     private static final Logger log = Logger.getLogger(ValueBindingToValueExpression.class.getName());
 53  
 
 54  
     private ValueBinding _valueBinding;
 55  
 
 56  
     private boolean _transient;
 57  
 
 58  
     /**
 59  
      * No-arg constructor used during restoreState
 60  
      */
 61  
     public ValueBindingToValueExpression()
 62  0
     {
 63  0
     }
 64  
 
 65  
     private ValueBinding getNotNullValueBinding()
 66  
     {
 67  0
         if (_valueBinding == null)
 68  
         {
 69  0
             throw new IllegalStateException("value binding is null");
 70  
         }
 71  0
         return _valueBinding;
 72  
     }
 73  
 
 74  
     /** Creates a new instance of ValueBindingToValueExpression */
 75  
     public ValueBindingToValueExpression(ValueBinding valueBinding)
 76  0
     {
 77  0
         if (valueBinding == null)
 78  
         {
 79  0
             throw new IllegalArgumentException("value binding must not be null");
 80  
         }
 81  0
         this._valueBinding = valueBinding;
 82  0
     }
 83  
 
 84  
     public ValueBinding getValueBinding()
 85  
     {
 86  0
         return _valueBinding;
 87  
     }
 88  
 
 89  
     @Override
 90  
     public boolean isReadOnly(final ELContext context) throws NullPointerException, PropertyNotFoundException,
 91  
             ELException
 92  
     {
 93  0
         return invoke(new Invoker<Boolean>()
 94  0
         {
 95  
             public Boolean invoke()
 96  
             {
 97  0
                 return getNotNullValueBinding().isReadOnly(getFacesContext(context));
 98  
             }
 99  
         });
 100  
     }
 101  
 
 102  
     @Override
 103  
     public Object getValue(final ELContext context) throws NullPointerException, PropertyNotFoundException, ELException
 104  
     {
 105  0
         return invoke(new Invoker<Object>()
 106  0
         {
 107  
             public Object invoke()
 108  
             {
 109  0
                 return getNotNullValueBinding().getValue(getFacesContext(context));
 110  
             }
 111  
         });
 112  
     }
 113  
 
 114  
     @Override
 115  
     public Class<?> getType(final ELContext context) throws NullPointerException, PropertyNotFoundException,
 116  
             ELException
 117  
     {
 118  0
         return invoke(new Invoker<Class<?>>()
 119  0
         {
 120  
             public Class<?> invoke()
 121  
             {
 122  0
                 return getNotNullValueBinding().getType(getFacesContext(context));
 123  
             }
 124  
         });
 125  
     }
 126  
 
 127  
     @Override
 128  
     public void setValue(final ELContext context, final Object value) throws NullPointerException,
 129  
             PropertyNotFoundException, PropertyNotWritableException, ELException
 130  
     {
 131  0
         invoke(new Invoker<Object>()
 132  0
         {
 133  
             public Object invoke()
 134  
             {
 135  0
                 getNotNullValueBinding().setValue(getFacesContext(context), value);
 136  0
                 return null;
 137  
             }
 138  
         });
 139  0
     }
 140  
 
 141  
     @Override
 142  
     public int hashCode()
 143  
     {
 144  0
         int result = 1;
 145  0
         result = 31 * result + (_transient ? 1231 : 1237);
 146  0
         result = 31 * result + ((_valueBinding == null) ? 0 : _valueBinding.hashCode());
 147  0
         return result;
 148  
     }
 149  
 
 150  
     @Override
 151  
     public boolean equals(Object obj)
 152  
     {
 153  0
         if (this == obj)
 154  
         {
 155  0
             return true;
 156  
         }
 157  0
         if (obj == null)
 158  
         {
 159  0
             return false;
 160  
         }
 161  0
         if (getClass() != obj.getClass())
 162  
         {
 163  0
             return false;
 164  
         }
 165  0
         final ValueBindingToValueExpression other = (ValueBindingToValueExpression) obj;
 166  0
         if (_transient != other._transient)
 167  
         {
 168  0
             return false;
 169  
         }
 170  0
         if (_valueBinding == null)
 171  
         {
 172  0
             if (other._valueBinding != null)
 173  
             {
 174  0
                 return false;
 175  
             }
 176  
         }
 177  0
         else if (!_valueBinding.equals(other._valueBinding))
 178  
         {
 179  0
             return false;
 180  
         }
 181  0
         return true;
 182  
     }
 183  
 
 184  
     @Override
 185  
     public boolean isLiteralText()
 186  
     {
 187  0
         return false;
 188  
     }
 189  
 
 190  
     @Override
 191  
     public String getExpressionString()
 192  
     {
 193  0
         return getNotNullValueBinding().getExpressionString();
 194  
     }
 195  
 
 196  
     @Override
 197  
     public Class<?> getExpectedType()
 198  
     {
 199  0
         if (_valueBinding != null)
 200  
         {
 201  
             try
 202  
             {
 203  0
                 Object value = getNotNullValueBinding().getValue(FacesContext.getCurrentInstance());
 204  0
                 if (value != null)
 205  
                 {
 206  0
                     return value.getClass();
 207  
                 }
 208  
             }
 209  0
             catch (Throwable e)
 210  
             {
 211  0
                 log.log(Level.WARNING, "Could not determine expected type for '"
 212  
                                        + _valueBinding.getExpressionString() + "': "
 213  
                                        + e.getMessage(), e);
 214  0
             }
 215  
         }
 216  0
         return null;
 217  
     }
 218  
 
 219  
     public void restoreState(FacesContext context, Object state)
 220  
     {
 221  0
         if (state instanceof ValueBinding)
 222  
         {
 223  0
             _valueBinding = (ValueBinding) state;
 224  
         }
 225  0
         else if (state != null)
 226  
         {
 227  0
             Object[] stateArray = (Object[]) state;
 228  0
             _valueBinding = (ValueBinding) ClassUtils.newInstance((String) stateArray[0], ValueBinding.class);
 229  0
             ((StateHolder) _valueBinding).restoreState(context, stateArray[1]);
 230  
         }
 231  0
     }
 232  
 
 233  
     public Object saveState(FacesContext context)
 234  
     {
 235  0
         if (!_transient)
 236  
         {
 237  0
             if (_valueBinding instanceof StateHolder)
 238  
             {
 239  0
                 Object[] state = new Object[2];
 240  0
                 state[0] = _valueBinding.getClass().getName();
 241  0
                 state[1] = ((StateHolder) _valueBinding).saveState(context);
 242  0
                 return state;
 243  
             }
 244  0
             return _valueBinding;
 245  
         }
 246  0
         return null;
 247  
     }
 248  
 
 249  
     public void setTransient(boolean newTransientValue)
 250  
     {
 251  0
         _transient = newTransientValue;
 252  0
     }
 253  
 
 254  
     public boolean isTransient()
 255  
     {
 256  0
         return _transient;
 257  
     }
 258  
 
 259  
     private FacesContext getFacesContext(ELContext context)
 260  
     {
 261  0
         if (context == null)
 262  
         {
 263  0
             throw new IllegalArgumentException("el context must not be null.");
 264  
         }
 265  0
         FacesContext facesContext = (FacesContext) context.getContext(FacesContext.class);
 266  0
         if (facesContext == null)
 267  
         {
 268  0
             throw new IllegalStateException("faces context not available in el context.");
 269  
         }
 270  0
         return facesContext;
 271  
     }
 272  
 
 273  
     private <T> T invoke(Invoker<T> invoker)
 274  
     {
 275  
         try
 276  
         {
 277  0
             return invoker.invoke();
 278  
         }
 279  0
         catch (javax.faces.el.PropertyNotFoundException e)
 280  
         {
 281  0
             throw new PropertyNotFoundException(e.getMessage(), e);
 282  
         }
 283  0
         catch (EvaluationException e)
 284  
         {
 285  0
             throw new ELException(e.getMessage(), e);
 286  
         }
 287  
     }
 288  
 
 289  
     private interface Invoker<T>
 290  
     {
 291  
         T invoke();
 292  
     }
 293  
 }