1 16 17 package org.apache.commons.beanutils; 18 19 import java.lang.reflect.Constructor ; 20 import java.lang.reflect.InvocationTargetException ; 21 import java.lang.reflect.Modifier ; 22 23 48 public class ConstructorUtils { 49 50 52 private static final Class [] emptyClassArray = new Class [0]; 53 54 private static final Object [] emptyObjectArray = new Object [0]; 55 56 58 71 public static Object invokeConstructor(Class klass, Object arg) 72 throws 73 NoSuchMethodException , 74 IllegalAccessException , 75 InvocationTargetException , 76 InstantiationException { 77 78 Object [] args = { arg }; 79 return invokeConstructor(klass, args); 80 81 } 82 83 96 public static Object invokeConstructor(Class klass, Object [] args) 97 throws 98 NoSuchMethodException , 99 IllegalAccessException , 100 InvocationTargetException , 101 InstantiationException { 102 103 if (null == args) { 104 args = emptyObjectArray; 105 } 106 int arguments = args.length; 107 Class parameterTypes[] = new Class [arguments]; 108 for (int i = 0; i < arguments; i++) { 109 parameterTypes[i] = args[i].getClass(); 110 } 111 return invokeConstructor(klass, args, parameterTypes); 112 113 } 114 115 132 public static Object invokeConstructor( 133 Class klass, 134 Object [] args, 135 Class [] parameterTypes) 136 throws 137 NoSuchMethodException , 138 IllegalAccessException , 139 InvocationTargetException , 140 InstantiationException { 141 142 if (parameterTypes == null) { 143 parameterTypes = emptyClassArray; 144 } 145 if (args == null) { 146 args = emptyObjectArray; 147 } 148 149 Constructor ctor = 150 getMatchingAccessibleConstructor(klass, parameterTypes); 151 if (null == ctor) { 152 throw new NoSuchMethodException ( 153 "No such accessible constructor on object: " + klass.getName()); 154 } 155 return ctor.newInstance(args); 156 } 157 158 159 172 public static Object invokeExactConstructor(Class klass, Object arg) 173 throws 174 NoSuchMethodException , 175 IllegalAccessException , 176 InvocationTargetException , 177 InstantiationException { 178 179 Object [] args = { arg }; 180 return invokeExactConstructor(klass, args); 181 182 } 183 184 197 public static Object invokeExactConstructor(Class klass, Object [] args) 198 throws 199 NoSuchMethodException , 200 IllegalAccessException , 201 InvocationTargetException , 202 InstantiationException { 203 if (null == args) { 204 args = emptyObjectArray; 205 } 206 int arguments = args.length; 207 Class parameterTypes[] = new Class [arguments]; 208 for (int i = 0; i < arguments; i++) { 209 parameterTypes[i] = args[i].getClass(); 210 } 211 return invokeExactConstructor(klass, args, parameterTypes); 212 213 } 214 215 233 public static Object invokeExactConstructor( 234 Class klass, 235 Object [] args, 236 Class [] parameterTypes) 237 throws 238 NoSuchMethodException , 239 IllegalAccessException , 240 InvocationTargetException , 241 InstantiationException { 242 243 if (args == null) { 244 args = emptyObjectArray; 245 } 246 247 if (parameterTypes == null) { 248 parameterTypes = emptyClassArray; 249 } 250 251 Constructor ctor = getAccessibleConstructor(klass, parameterTypes); 252 if (null == ctor) { 253 throw new NoSuchMethodException ( 254 "No such accessible constructor on object: " + klass.getName()); 255 } 256 return ctor.newInstance(args); 257 258 } 259 260 267 public static Constructor getAccessibleConstructor( 268 Class klass, 269 Class parameterType) { 270 271 Class [] parameterTypes = { parameterType }; 272 return getAccessibleConstructor(klass, parameterTypes); 273 274 } 275 276 284 public static Constructor getAccessibleConstructor( 285 Class klass, 286 Class [] parameterTypes) { 287 288 try { 289 return getAccessibleConstructor( 290 klass.getConstructor(parameterTypes)); 291 } catch (NoSuchMethodException e) { 292 return (null); 293 } 294 295 } 296 297 303 public static Constructor getAccessibleConstructor(Constructor ctor) { 304 305 if (ctor == null) { 307 return (null); 308 } 309 310 if (!Modifier.isPublic(ctor.getModifiers())) { 312 return (null); 313 } 314 315 Class clazz = ctor.getDeclaringClass(); 317 if (Modifier.isPublic(clazz.getModifiers())) { 318 return (ctor); 319 } 320 321 return null; 323 324 } 325 326 342 private static Constructor getMatchingAccessibleConstructor( 343 Class clazz, 344 Class [] parameterTypes) { 345 try { 348 Constructor ctor = clazz.getConstructor(parameterTypes); 349 try { 350 ctor.setAccessible(true); 367 } catch (SecurityException se) {} 368 return ctor; 369 370 } catch (NoSuchMethodException e) { 371 } 372 373 int paramSize = parameterTypes.length; 375 Constructor [] ctors = clazz.getConstructors(); 376 for (int i = 0, size = ctors.length; i < size; i++) { 377 Class [] ctorParams = ctors[i].getParameterTypes(); 379 int ctorParamSize = ctorParams.length; 380 if (ctorParamSize == paramSize) { 381 boolean match = true; 382 for (int n = 0; n < ctorParamSize; n++) { 383 if (!MethodUtils 384 .isAssignmentCompatible( 385 ctorParams[n], 386 parameterTypes[n])) { 387 match = false; 388 break; 389 } 390 } 391 392 if (match) { 393 Constructor ctor = getAccessibleConstructor(ctors[i]); 395 if (ctor != null) { 396 try { 397 ctor.setAccessible(true); 398 } catch (SecurityException se) {} 399 return ctor; 400 } 401 } 402 } 403 } 404 405 return null; 406 } 407 408 } 409 | Popular Tags |