1 15 package org.apache.hivemind.util; 16 17 import java.lang.reflect.Constructor ; 18 import java.lang.reflect.InvocationTargetException ; 19 import java.lang.reflect.Modifier ; 20 import java.util.ArrayList ; 21 import java.util.HashMap ; 22 import java.util.List ; 23 import java.util.Map ; 24 25 import org.apache.hivemind.ApplicationRuntimeException; 26 27 32 public class ConstructorUtils 33 { 34 35 38 private static final Map _primitiveMap = new HashMap (); 39 40 static 41 { 42 _primitiveMap.put(boolean.class, Boolean .class); 43 _primitiveMap.put(byte.class, Byte .class); 44 _primitiveMap.put(char.class, Character .class); 45 _primitiveMap.put(short.class, Short .class); 46 _primitiveMap.put(int.class, Integer .class); 47 _primitiveMap.put(long.class, Long .class); 48 _primitiveMap.put(float.class, Float .class); 49 _primitiveMap.put(double.class, Double .class); 50 } 51 52 54 private ConstructorUtils() 55 { 56 } 57 58 69 public static Object invokeConstructor(Class targetClass, Object [] parameters) 70 { 71 if (parameters == null) 72 parameters = new Object [0]; 73 74 Class [] parameterTypes = new Class [parameters.length]; 75 76 for (int i = 0; i < parameters.length; i++) 77 parameterTypes[i] = parameters[i] == null ? null : parameters[i].getClass(); 78 79 return invokeMatchingConstructor(targetClass, parameterTypes, parameters); 80 } 81 82 private static Object invokeMatchingConstructor(Class targetClass, Class [] parameterTypes, 83 Object [] parameters) 84 { 85 Constructor [] constructors = targetClass.getConstructors(); 86 87 for (int i = 0; i < constructors.length; i++) 88 { 89 Constructor c = constructors[i]; 90 91 if (isMatch(c, parameterTypes)) 92 return invoke(c, parameters); 93 } 94 95 throw new ApplicationRuntimeException(UtilMessages.noMatchingConstructor(targetClass), null); 96 } 97 98 private static boolean isMatch(Constructor c, Class [] types) 99 { 100 Class [] actualTypes = c.getParameterTypes(); 101 102 if (actualTypes.length != types.length) 103 return false; 104 105 for (int i = 0; i < types.length; i++) 106 { 107 if (types[i] == null && !actualTypes[i].isPrimitive()) 108 continue; 109 110 if (!isCompatible(actualTypes[i], types[i])) 111 return false; 112 } 113 114 return true; 115 } 116 117 public static boolean isCompatible(Class actualType, Class parameterType) 118 { 119 if (actualType.isAssignableFrom(parameterType)) 120 return true; 121 122 125 if (actualType.isPrimitive()) 126 { 127 Class wrapperClass = (Class ) _primitiveMap.get(actualType); 128 129 return wrapperClass.isAssignableFrom(parameterType); 130 } 131 132 return false; 133 } 134 135 public static Object invoke(Constructor c, Object [] parameters) 136 { 137 try 138 { 139 return c.newInstance(parameters); 140 } 141 catch (InvocationTargetException ex) 142 { 143 Throwable cause = ex.getTargetException(); 144 145 throw new ApplicationRuntimeException(UtilMessages.invokeFailed(c, cause), null, cause); 146 } 147 catch (Exception ex) 148 { 149 throw new ApplicationRuntimeException(UtilMessages.invokeFailed(c, ex), null, ex); 150 } 151 } 152 153 public static List getConstructorsOfLength(final Class clazz, final int length) 154 { 155 List fixedLengthConstructors = new ArrayList (1); 156 157 Constructor [] constructors = clazz.getDeclaredConstructors(); 158 159 for (int i = 0; i < constructors.length; i++) 160 { 161 if (!Modifier.isPublic(constructors[i].getModifiers())) 162 continue; 163 164 Class [] parameterTypes = constructors[i].getParameterTypes(); 165 166 if (parameterTypes.length == length) 167 fixedLengthConstructors.add(constructors[i]); 168 } 169 170 return fixedLengthConstructors; 171 } 172 } | Popular Tags |