Clover coverage report - Code Coverage for hivemind release 1.0-beta-1
Coverage timestamp: Sat Jul 3 2004 09:41:37 EDT
file stats: LOC: 152   Methods: 6
NCLOC: 89   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
ConstructorUtils.java 90.9% 94.7% 83.3% 92.4%
coverage coverage
 1   
 //  Copyright 2004 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.lang.reflect.Constructor;
 18   
 import java.util.HashMap;
 19   
 import java.util.Map;
 20   
 
 21   
 import org.apache.hivemind.ApplicationRuntimeException;
 22   
 import org.apache.hivemind.HiveMind;
 23   
 
 24   
 /**
 25   
  * Static methods for invoking constructors.
 26   
  *
 27   
  * @author Howard Lewis Ship
 28   
  */
 29   
 public class ConstructorUtils
 30   
 {
 31   
 
 32   
     /**
 33   
      * Map from primitive type to wrapper type.
 34   
      */
 35   
     private static final Map _primitiveMap = new HashMap();
 36   
 
 37   
     static {
 38  19
         _primitiveMap.put(boolean.class, Boolean.class);
 39  19
         _primitiveMap.put(byte.class, Byte.class);
 40  19
         _primitiveMap.put(char.class, Character.class);
 41  19
         _primitiveMap.put(short.class, Short.class);
 42  19
         _primitiveMap.put(int.class, Integer.class);
 43  19
         _primitiveMap.put(long.class, Long.class);
 44  19
         _primitiveMap.put(float.class, Float.class);
 45  19
         _primitiveMap.put(double.class, Double.class);
 46   
     }
 47   
 
 48   
     // Prevent instantiation
 49   
 
 50  0
     private ConstructorUtils()
 51   
     {
 52   
     }
 53   
 
 54   
     /**
 55   
      * Searches for a constructor matching against the provided arguments.
 56   
      * 
 57   
      * @param targetClass the class to be instantiated
 58   
      * @param parameters the parameters to pass to the constructor (may be null or empty)
 59   
      * @param location the location to use in any reported errors (this will be passed
 60   
      * to {@link HiveMind#getLocationString(Object)})
 61   
      * @return the new instance
 62   
      * @throws ApplicationRuntimeException on any failure
 63   
      * 
 64   
      */
 65  399
     public static Object invokeConstructor(Class targetClass, Object[] parameters, Object location)
 66   
     {
 67  399
         if (parameters == null)
 68  383
             parameters = new Object[0];
 69   
 
 70  399
         Class[] parameterTypes = new Class[parameters.length];
 71   
 
 72  399
         for (int i = 0; i < parameters.length; i++)
 73  18
             parameterTypes[i] = parameters[i] == null ? null : parameters[i].getClass();
 74   
 
 75  399
         return invokeMatchingConstructor(targetClass, parameterTypes, parameters, location);
 76   
     }
 77   
 
 78  399
     private static Object invokeMatchingConstructor(
 79   
         Class targetClass,
 80   
         Class[] parameterTypes,
 81   
         Object[] parameters,
 82   
         Object location)
 83   
     {
 84  399
         Constructor[] constructors = targetClass.getConstructors();
 85   
 
 86  399
         for (int i = 0; i < constructors.length; i++)
 87   
         {
 88  416
             Constructor c = constructors[i];
 89   
 
 90  416
             if (isMatch(c, parameterTypes))
 91  398
                 return invoke(c, parameters, location);
 92   
         }
 93   
 
 94  1
         throw new ApplicationRuntimeException(
 95   
             UtilMessages.noMatchingConstructor(targetClass, location),
 96   
             HiveMind.getLocation(location),
 97   
             null);
 98   
     }
 99   
 
 100  416
     private static boolean isMatch(Constructor c, Class[] types)
 101   
     {
 102  416
         Class[] actualTypes = c.getParameterTypes();
 103   
 
 104  416
         if (actualTypes.length != types.length)
 105  15
             return false;
 106   
 
 107  401
         for (int i = 0; i < types.length; i++)
 108   
         {
 109  21
             if (types[i] == null && actualTypes[i].isPrimitive())
 110  0
                 continue;
 111   
 
 112  21
             if (!isCompatible(actualTypes[i], types[i]))
 113  3
                 return false;
 114   
         }
 115   
 
 116  398
         return true;
 117   
     }
 118   
 
 119  21
     private static boolean isCompatible(Class actualType, Class parameterType)
 120   
     {
 121  21
         if (actualType.isAssignableFrom(parameterType))
 122  17
             return true;
 123   
 
 124   
         // Reflection fudges the assignment of a wrapper class to a primitive
 125   
         // type ... we check for that the hard way.
 126   
 
 127  4
         if (actualType.isPrimitive())
 128   
         {
 129  2
             Class wrapperClass = (Class) _primitiveMap.get(actualType);
 130   
 
 131  2
             return wrapperClass.isAssignableFrom(parameterType);
 132   
         }
 133   
 
 134  2
         return false;
 135   
     }
 136   
 
 137  398
     private static Object invoke(Constructor c, Object[] parameters, Object location)
 138   
     {
 139  398
         try
 140   
         {
 141  398
             return c.newInstance(parameters);
 142   
         }
 143   
         catch (Exception ex)
 144   
         {
 145  0
             throw new ApplicationRuntimeException(
 146   
                 UtilMessages.invokeFailed(c, location, ex),
 147   
                 HiveMind.getLocation(location),
 148   
                 ex);
 149   
         }
 150   
     }
 151   
 }
 152