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