Clover coverage report - Code Coverage for hivemind release 1.1-alpha-1
Coverage timestamp: Tue Jan 18 2005 07:55:08 EST
file stats: LOC: 184   Methods: 12
NCLOC: 87   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
PropertyUtils.java 100% 100% 91.7% 97.7%
coverage coverage
 1   
 // Copyright 2004, 2005 The Apache Software Foundation
 2   
 //
 3   
 // Licensed under the Apache License, Version 2.0 (the "License");
 4   
 // you may not use this file except in compliance with the License.
 5   
 // You may obtain a copy of the License at
 6   
 //
 7   
 //     http://www.apache.org/licenses/LICENSE-2.0
 8   
 //
 9   
 // Unless required by applicable law or agreed to in writing, software
 10   
 // distributed under the License is distributed on an "AS IS" BASIS,
 11   
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12   
 // See the License for the specific language governing permissions and
 13   
 // limitations under the License.
 14   
 
 15   
 package org.apache.hivemind.util;
 16   
 
 17   
 import java.beans.BeanInfo;
 18   
 import java.beans.Introspector;
 19   
 import java.util.HashMap;
 20   
 import java.util.List;
 21   
 import java.util.Map;
 22   
 
 23   
 import org.apache.hivemind.ApplicationRuntimeException;
 24   
 import org.apache.hivemind.HiveMind;
 25   
 
 26   
 /**
 27   
  * A collection of static methods used to perform property-level access on arbitrary objects.
 28   
  * 
 29   
  * @author Howard Lewis Ship
 30   
  */
 31   
 public class PropertyUtils
 32   
 {
 33   
     private static final Map _classAdaptors = new HashMap();
 34   
 
 35   
     // Prevent instantiation
 36  0
     private PropertyUtils()
 37   
     {
 38   
     }
 39   
 
 40   
     /**
 41   
      * Updates the property of the target object.
 42   
      * 
 43   
      * @param target
 44   
      *            the object to update
 45   
      * @param propertyName
 46   
      *            the name of the property to be updated
 47   
      * @param value
 48   
      *            the value to be stored into the target object property
 49   
      */
 50  5756
     public static void write(Object target, String propertyName, Object value)
 51   
     {
 52  5756
         ClassAdaptor a = getAdaptor(target);
 53   
 
 54  5756
         a.write(target, propertyName, value);
 55   
     }
 56   
 
 57   
     /**
 58   
      * Returns true of the instance contains a writable property of the given type.
 59   
      * 
 60   
      * @param target
 61   
      *            the object to inspect
 62   
      * @param propertyName
 63   
      *            the name of the property to check
 64   
      */
 65   
 
 66  2569
     public static boolean isWritable(Object target, String propertyName)
 67   
     {
 68  2569
         return getAdaptor(target).isWritable(propertyName);
 69   
     }
 70   
 
 71  4
     public static boolean isReadable(Object target, String propertyName)
 72   
     {
 73  4
         return getAdaptor(target).isReadable(propertyName);
 74   
     }
 75   
 
 76   
     /**
 77   
      * Updates the property of the target object.
 78   
      * 
 79   
      * @param target
 80   
      *            the object to update
 81   
      * @param propertyName
 82   
      *            the name of a property toread
 83   
      */
 84   
 
 85  11
     public static Object read(Object target, String propertyName)
 86   
     {
 87  11
         ClassAdaptor a = getAdaptor(target);
 88   
 
 89  9
         return a.read(target, propertyName);
 90   
     }
 91   
 
 92   
     /**
 93   
      * Returns the type of the named property.
 94   
      * 
 95   
      * @param target
 96   
      *            the object to examine
 97   
      * @param propertyName
 98   
      *            the name of the property to check
 99   
      */
 100  5881
     public static Class getPropertyType(Object target, String propertyName)
 101   
     {
 102  5881
         ClassAdaptor a = getAdaptor(target);
 103   
 
 104  5881
         return a.getPropertyType(target, propertyName);
 105   
     }
 106   
 
 107   
     /**
 108   
      * Returns the {@link PropertyAdaptor}for the given target object and property name.
 109   
      * 
 110   
      * @throws ApplicationRuntimeException
 111   
      *             if the property does not exist.
 112   
      */
 113  2
     public static PropertyAdaptor getPropertyAdaptor(Object target, String propertyName)
 114   
     {
 115  2
         ClassAdaptor a = getAdaptor(target);
 116   
 
 117  2
         return a.getPropertyAdaptor(target, propertyName);
 118   
     }
 119   
 
 120   
     /**
 121   
      * Returns an unordered List of the names of all readable properties of the target.
 122   
      */
 123  1
     public static List getReadableProperties(Object target)
 124   
     {
 125  1
         return getAdaptor(target).getReadableProperties();
 126   
     }
 127   
 
 128   
     /**
 129   
      * Returns an unordered List of the names of all writable properties of the target.
 130   
      */
 131  437
     public static List getWriteableProperties(Object target)
 132   
     {
 133  437
         return getAdaptor(target).getWriteableProperties();
 134   
     }
 135   
 
 136  14661
     private static ClassAdaptor getAdaptor(Object target)
 137   
     {
 138  14661
         if (target == null)
 139  1
             throw new ApplicationRuntimeException(UtilMessages.nullObject());
 140   
 
 141  14660
         Class targetClass = target.getClass();
 142   
 
 143  14660
         synchronized (HiveMind.INTROSPECTOR_MUTEX)
 144   
         {
 145  14660
             ClassAdaptor result = (ClassAdaptor) _classAdaptors.get(targetClass);
 146   
 
 147  14660
             if (result == null)
 148   
             {
 149  873
                 result = buildClassAdaptor(target, targetClass);
 150  872
                 _classAdaptors.put(targetClass, result);
 151   
             }
 152   
 
 153  14659
             return result;
 154   
         }
 155   
     }
 156   
 
 157  873
     private static ClassAdaptor buildClassAdaptor(Object target, Class targetClass)
 158   
     {
 159  873
         try
 160   
         {
 161  873
             BeanInfo info = Introspector.getBeanInfo(targetClass);
 162   
 
 163  872
             return new ClassAdaptor(info.getPropertyDescriptors());
 164   
         }
 165   
         catch (Exception ex)
 166   
         {
 167  1
             throw new ApplicationRuntimeException(UtilMessages.unableToIntrospect(targetClass, ex),
 168   
                     target, null, ex);
 169   
         }
 170   
     }
 171   
 
 172   
     /**
 173   
      * Clears all cached information. Invokes {@link Introspector#flushCaches()}.
 174   
      */
 175  569
     public static void clearCache()
 176   
     {
 177  569
         synchronized (HiveMind.INTROSPECTOR_MUTEX)
 178   
         {
 179  569
             _classAdaptors.clear();
 180  569
             Introspector.flushCaches();
 181   
         }
 182   
     }
 183   
 
 184   
 }