Clover coverage report -
Coverage timestamp: Thu Jul 5 2007 20:02:32 EDT
file stats: LOC: 151   Methods: 5
NCLOC: 107   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ObjectUtil.java 73.3% 75.6% 80% 75%
coverage coverage
 1    /*
 2    * JBoss, the OpenSource J2EE webOS
 3    *
 4    * Distributable under LGPL license.
 5    * See terms of license at gnu.org.
 6    */
 7    package org.jboss.cache.pojo.util;
 8   
 9    import org.apache.commons.logging.Log;
 10    import org.apache.commons.logging.LogFactory;
 11    import org.jboss.aop.Advised;
 12    import org.jboss.cache.CacheException;
 13    import org.jboss.cache.Fqn;
 14    import org.jboss.cache.pojo.PojoCacheException;
 15    import org.jboss.cache.pojo.impl.CachedType;
 16    import org.jboss.cache.pojo.impl.PojoCacheImpl;
 17    import org.jboss.cache.pojo.memory.FieldPersistentReference;
 18   
 19    import java.lang.reflect.Field;
 20    import java.util.Collection;
 21    import java.util.HashSet;
 22    import java.util.Iterator;
 23    import java.util.Map;
 24    import java.util.Set;
 25   
 26    /**
 27    * Unitlity methods for pojo object.
 28    *
 29    * @author Ben Wang
 30    */
 31    public final class ObjectUtil
 32    {
 33    private static final Log log = LogFactory.getLog(ObjectUtil.class.getName());
 34   
 35    /**
 36    * Static methos to check if <code>thisObject</code> is reachable from <code>originalObject</code>.
 37    *
 38    * @param cache
 39    * @param originalObject
 40    * @param thisObject
 41    * @return
 42    * @throws CacheException
 43    */
 44  3 public static boolean isReachable(PojoCacheImpl cache, Object originalObject, Object thisObject)
 45    throws CacheException
 46    {
 47  3 HashSet objSet = new HashSet();
 48   
 49  3 return isReachableInner(cache, originalObject, thisObject, objSet);
 50    }
 51   
 52  16 private static boolean isReachableInner(PojoCacheImpl cache, Object originalObject,
 53    Object thisObject, Set objSet)
 54    throws CacheException
 55    {
 56    // Currently we don't support recursive Collection
 57  16 if (!(originalObject instanceof Advised))
 58  0 throw new PojoCacheException("ObjectUtil.isReachable(): originalObject is not Advised.");
 59   
 60  16 if (log.isTraceEnabled())
 61    {
 62  0 log.trace("isReachable(): current object: " + originalObject + " this object: " + thisObject);
 63    }
 64   
 65  16 if (originalObject.equals(thisObject))
 66    {
 67  2 if (log.isTraceEnabled())
 68    {
 69  0 log.trace("isReachable(): object found reachable.");
 70    }
 71   
 72  2 return true;
 73    }
 74   
 75  14 if (!objSet.contains(originalObject))
 76    {
 77  10 objSet.add(originalObject);
 78    } else
 79    { // We have been here before so let's return.
 80  4 return false;
 81    }
 82   
 83  10 CachedType type = cache.getCachedType(originalObject.getClass());
 84  10 for (Iterator i = type.getFields().iterator(); i.hasNext();)
 85    {
 86  36 Field field = (Field) (((FieldPersistentReference) i.next())).get();
 87  36 Object value = null;
 88  36 try
 89    {
 90  36 value = field.get(originalObject); // Reflection may not work here.
 91    }
 92    catch (IllegalAccessException e)
 93    {
 94  0 throw new CacheException("field access failed", e);
 95    }
 96  36 CachedType fieldType = cache.getCachedType(field.getType());
 97  36 if (fieldType.isImmediate())
 98    {
 99    } else
 100    {
 101  16 if (value instanceof Map)
 102    {
 103  0 Set set = ((Map) value).keySet();
 104  0 for (Iterator it = set.iterator(); it.hasNext();)
 105    {
 106  0 if (isReachableInner(cache, it.next(), thisObject, objSet))
 107  0 return true;
 108    }
 109   
 110  0 continue;
 111  16 } else if (value instanceof Collection)
 112    {
 113  10 for (Iterator it = ((Collection) value).iterator(); it.hasNext();)
 114    {
 115  8 if (isReachableInner(cache, it.next(), thisObject, objSet))
 116  4 return true;
 117    }
 118   
 119  6 continue;
 120    }
 121   
 122  6 if (!(value instanceof Advised))
 123  1 continue; // TODO We don't care about Collection now.
 124   
 125  5 if (isReachableInner(cache, value, thisObject, objSet))
 126  1 return true;
 127    }
 128    }
 129   
 130  5 return false;
 131    }
 132   
 133  0 public static String getIndirectFqn(Fqn fqn)
 134    {
 135    // TODO Need to generate a unique id here
 136    // Let's strip off the line separator and use underscoe instead.
 137  0 return getIndirectFqn(fqn.toString());
 138    }
 139   
 140  68 public static String getIndirectFqn(String fqn)
 141    {
 142    // TODO Need to generate a unique id here
 143    // Let's strip off the line separator and use underscoe instead.
 144  68 return fqn.replace('/', '_');
 145    }
 146   
 147  4 public static String identityString(Object object)
 148    {
 149  4 return object.getClass().getName() + '@' + Integer.toHexString(System.identityHashCode(object));
 150    }
 151    }