2009/05/20 - Apache Shale has been retired.

For more information, please explore the Attic.

Coverage Report - org.apache.shale.util.PropertyHelper
 
Classes in this File Line Coverage Branch Coverage Complexity
PropertyHelper
0% 
0% 
5.6
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to you under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 
 18  
 package org.apache.shale.util;
 19  
 
 20  
 import java.beans.BeanInfo;
 21  
 import java.beans.IntrospectionException;
 22  
 import java.beans.Introspector;
 23  
 import java.beans.PropertyDescriptor;
 24  
 import java.lang.reflect.InvocationTargetException;
 25  
 import java.lang.reflect.Method;
 26  
 import javax.faces.el.EvaluationException;
 27  
 import javax.faces.el.PropertyNotFoundException;
 28  
 
 29  
 /**
 30  
  * <p>Helper class to provide access to the properties of JavaBeans.  This
 31  
  * implementation is stateless and maintains no cached information, so
 32  
  * instances may be freely created and destroyed, with no side effects
 33  
  * (unless there are side effects from caching inside the JDK itself).</p>
 34  
  *
 35  
  * $Id: PropertyHelper.java 464373 2006-10-16 04:21:54Z rahul $
 36  
  *
 37  
  * @since 1.0.1
 38  
  */
 39  0
 public class PropertyHelper {
 40  
 
 41  
 
 42  
     // ---------------------------------------------------------- Public Methods
 43  
 
 44  
 
 45  
     /**
 46  
      * <p>Return the value of the specified property from the specified bean.</p>
 47  
      *
 48  
      * @param bean Bean whose property is to be retrieved
 49  
      * @param name Name of the property to get
 50  
      *
 51  
      * @exception PropertyNotFoundException if the bean class does not have
 52  
      *  a property with the specified name, or if the property is write only
 53  
      * @exception EvaluationException if an exception occurs while
 54  
      *  retrieving the property value
 55  
      */
 56  
     public Object getValue(Object bean, String name) {
 57  
 
 58  0
         PropertyDescriptor descriptor = descriptor(bean, name);
 59  0
         Method method = descriptor.getReadMethod();
 60  0
         if (method == null) {
 61  0
             throw new PropertyNotFoundException(name);
 62  
         }
 63  
 
 64  
         try {
 65  0
             return method.invoke(bean, new Object[0]);
 66  0
         } catch (InvocationTargetException e) {
 67  0
             throw new EvaluationException(e.getCause());
 68  0
         } catch (Exception e) {
 69  0
             throw new EvaluationException(e);
 70  
         }
 71  
 
 72  
     }
 73  
 
 74  
 
 75  
     /**
 76  
      * <p>Return <code>true</code> if the specified property is read only on
 77  
      * the underlying bean class.</p>
 78  
      *
 79  
      * @param bean Bean whose property is to be checked
 80  
      * @param name Name of the property to check
 81  
      *
 82  
      * @exception PropertyNotFoundException if the bean class does not have
 83  
      *  a property with the specified name, or if the property is write only
 84  
      * @exception EvaluationException if an exception occurs while
 85  
      *  checking the read only status
 86  
      */
 87  
     public boolean isReadOnly(Object bean, String name) {
 88  
 
 89  0
         PropertyDescriptor descriptor = descriptor(bean, name);
 90  0
         if (descriptor.getReadMethod() != null) {
 91  0
             return descriptor.getWriteMethod() == null;
 92  
         }
 93  0
         throw new PropertyNotFoundException(name);
 94  
 
 95  
     }
 96  
 
 97  
 
 98  
     /**
 99  
      * <p>Return the type of the specified property on the underlying bean
 100  
      * class.</p>
 101  
      *
 102  
      * @param bean Bean whose property is to be analyzed
 103  
      * @param name Name of the property to return the type of
 104  
      *
 105  
      * @exception PropertyNotFoundException if the bean class does not have
 106  
      *  a property with the specified name
 107  
      * @exception EvaluationException if an exception occurs while
 108  
      *  retrieving the property type
 109  
      */
 110  
     public Class getType(Object bean, String name) {
 111  
 
 112  0
         PropertyDescriptor descriptor = descriptor(bean, name);
 113  0
         return descriptor.getPropertyType();
 114  
 
 115  
     }
 116  
 
 117  
 
 118  
     /**
 119  
      * <p>Set the specified value on the specified property of the specified
 120  
      * bean.</p>
 121  
      *
 122  
      * @param bean Bean whose property is to be set
 123  
      * @param name Name of the property to be set
 124  
      * @param value New value for this property
 125  
      *
 126  
      * @exception PropertyNotFoundException if the bean class does not have
 127  
      *  a property with the specified name, or if the property is read only
 128  
      * @exception EvaluationException if an exception occurs while
 129  
      *  setting the property value
 130  
      */
 131  
     public void setValue(Object bean, String name, Object value) {
 132  
 
 133  0
         PropertyDescriptor descriptor = descriptor(bean, name);
 134  0
         Method method = descriptor.getWriteMethod();
 135  0
         if (method == null) {
 136  0
             throw new PropertyNotFoundException(name);
 137  
         }
 138  
 
 139  
         try {
 140  0
             method.invoke(bean, new Object[] { value });
 141  0
         } catch (InvocationTargetException e) {
 142  0
             throw new EvaluationException(e.getCause());
 143  0
         } catch (Exception e) {
 144  0
             throw new EvaluationException(e);
 145  0
         }
 146  
 
 147  0
     }
 148  
 
 149  
 
 150  
     // ------------------------------------------------------- Private Methods
 151  
 
 152  
 
 153  
     /**
 154  
      * <p>Return the <code>PropertyDescriptor</code> for the specified
 155  
      * property of the specified class.</p>
 156  
      *
 157  
      * @param bean Bean whose property descriptor is to be returned
 158  
      * @param name Name of the property to retrieve a descrpitor for
 159  
      *
 160  
      * @exception PropertyNotFoundException if the bean class does not have
 161  
      *  a property with the specified name
 162  
      * @exception EvaluationException if an exception occurs introspecting
 163  
      *  the underlying bean class
 164  
      */
 165  
     private PropertyDescriptor descriptor(Object bean, String name) {
 166  
 
 167  0
         BeanInfo beanInfo = null;
 168  
         try {
 169  0
             beanInfo = Introspector.getBeanInfo(bean.getClass());
 170  0
         } catch (IntrospectionException e) {
 171  0
             throw new EvaluationException(e);
 172  0
         }
 173  
 
 174  0
         PropertyDescriptor descriptors[] = beanInfo.getPropertyDescriptors();
 175  0
         if (descriptors == null) {
 176  0
             descriptors = new PropertyDescriptor[0];
 177  
         }
 178  0
         for (int i = 0; i < descriptors.length; i++) {
 179  0
             if (name.equals(descriptors[i].getName())) {
 180  0
                 return descriptors[i];
 181  
             }
 182  
         }
 183  
 
 184  0
         throw new PropertyNotFoundException(name);
 185  
 
 186  
     }
 187  
 
 188  
 
 189  
 }