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