2009/04/15 - Apache HiveMind has been retired.

For more information, please explore the Attic.

Clover coverage report - Code Coverage for hivemind release 1.2.1
Coverage timestamp: Fri Feb 10 2006 16:33:43 PST
file stats: LOC: 220   Methods: 14
NCLOC: 97   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
PropertyUtils.java 100% 100% 92.9% 98%
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  8369 public static void write(Object target, String propertyName, Object value)
 51    {
 52  8369 ClassAdaptor a = getAdaptor(target);
 53   
 54  8371 a.write(target, propertyName, value);
 55    }
 56   
 57    /**
 58    * An improved version of {@link #write(Object, String, Object)} where the value starts as a
 59    * string and is converted to the correct property type before being assigned.
 60    *
 61    * @since 1.1
 62    */
 63  4 public static void smartWrite(Object target, String propertyName, String value)
 64    {
 65  4 ClassAdaptor a = getAdaptor(target);
 66   
 67  4 a.smartWrite(target, propertyName, value);
 68    }
 69   
 70    /**
 71    * Initializes the properties of an object from a string. The string is a comma-seperated
 72    * sequence of property names and values. Property names are seperated from values be an equals
 73    * sign. Spaces before and after the property names are trimmed.
 74    * For boolean properties, the equals sign and value may be omitted (a value of true is
 75    * assumed), or the property name may be prefixed with an exclamation point to indicated false
 76    * value. Example: <code>validate,maxLength=10,displayName=User Id</code>.
 77    *
 78    * @param target
 79    * the object to be configured
 80    * @param initializer
 81    * the string encoding the properties and values to be configured in the target
 82    * object
 83    * @since 1.1
 84    */
 85   
 86  10 public static void configureProperties(Object target, String initializer)
 87    {
 88  10 ClassAdaptor a = getAdaptor(target);
 89   
 90  10 a.configureProperties(target, initializer);
 91    }
 92   
 93    /**
 94    * Returns true of the instance contains a writable property of the given type.
 95    *
 96    * @param target
 97    * the object to inspect
 98    * @param propertyName
 99    * the name of the property to check
 100    */
 101   
 102  4683 public static boolean isWritable(Object target, String propertyName)
 103    {
 104  4683 return getAdaptor(target).isWritable(propertyName);
 105    }
 106   
 107  4 public static boolean isReadable(Object target, String propertyName)
 108    {
 109  4 return getAdaptor(target).isReadable(propertyName);
 110    }
 111   
 112    /**
 113    * Updates the property of the target object.
 114    *
 115    * @param target
 116    * the object to update
 117    * @param propertyName
 118    * the name of a property toread
 119    */
 120   
 121  11 public static Object read(Object target, String propertyName)
 122    {
 123  11 ClassAdaptor a = getAdaptor(target);
 124   
 125  9 return a.read(target, propertyName);
 126    }
 127   
 128    /**
 129    * Returns the type of the named property.
 130    *
 131    * @param target
 132    * the object to examine
 133    * @param propertyName
 134    * the name of the property to check
 135    */
 136  8520 public static Class getPropertyType(Object target, String propertyName)
 137    {
 138  8520 ClassAdaptor a = getAdaptor(target);
 139   
 140  8520 return a.getPropertyType(target, propertyName);
 141    }
 142   
 143    /**
 144    * Returns the {@link PropertyAdaptor} for the given target object and property name.
 145    *
 146    * @throws ApplicationRuntimeException
 147    * if the property does not exist.
 148    */
 149  2 public static PropertyAdaptor getPropertyAdaptor(Object target, String propertyName)
 150    {
 151  2 ClassAdaptor a = getAdaptor(target);
 152   
 153  2 return a.getPropertyAdaptor(target, propertyName);
 154    }
 155   
 156    /**
 157    * Returns an unordered List of the names of all readable properties of the target.
 158    */
 159  1 public static List getReadableProperties(Object target)
 160    {
 161  1 return getAdaptor(target).getReadableProperties();
 162    }
 163   
 164    /**
 165    * Returns an unordered List of the names of all writable properties of the target.
 166    */
 167  790 public static List getWriteableProperties(Object target)
 168    {
 169  790 return getAdaptor(target).getWriteableProperties();
 170    }
 171   
 172  22394 private static ClassAdaptor getAdaptor(Object target)
 173    {
 174  22394 if (target == null)
 175  1 throw new ApplicationRuntimeException(UtilMessages.nullObject());
 176   
 177  22392 Class targetClass = target.getClass();
 178   
 179  22388 synchronized (HiveMind.INTROSPECTOR_MUTEX)
 180    {
 181  22399 ClassAdaptor result = (ClassAdaptor) _classAdaptors.get(targetClass);
 182   
 183  22399 if (result == null)
 184    {
 185  1059 result = buildClassAdaptor(target, targetClass);
 186  1058 _classAdaptors.put(targetClass, result);
 187    }
 188   
 189  22398 return result;
 190    }
 191    }
 192   
 193  1059 private static ClassAdaptor buildClassAdaptor(Object target, Class targetClass)
 194    {
 195  1059 try
 196    {
 197  1059 BeanInfo info = Introspector.getBeanInfo(targetClass);
 198   
 199  1058 return new ClassAdaptor(info.getPropertyDescriptors());
 200    }
 201    catch (Exception ex)
 202    {
 203  1 throw new ApplicationRuntimeException(UtilMessages.unableToIntrospect(targetClass, ex),
 204    target, null, ex);
 205    }
 206    }
 207   
 208    /**
 209    * Clears all cached information. Invokes {@link Introspector#flushCaches()}.
 210    */
 211  644 public static void clearCache()
 212    {
 213  644 synchronized (HiveMind.INTROSPECTOR_MUTEX)
 214    {
 215  644 _classAdaptors.clear();
 216  644 Introspector.flushCaches();
 217    }
 218    }
 219   
 220    }