| 1 | /* |
| 2 | * Copyright 2007 brunella ltd |
| 3 | * |
| 4 | * Licensed under the LGPL Version 3 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * |
| 7 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 8 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 9 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 10 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 11 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 12 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 13 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 14 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 15 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 16 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 17 | * THE POSSIBILITY OF SUCH DAMAGE. |
| 18 | */ |
| 19 | package sf.qof.util; |
| 20 | |
| 21 | import java.lang.reflect.Constructor; |
| 22 | import java.lang.reflect.InvocationTargetException; |
| 23 | |
| 24 | /** |
| 25 | * Helper class to instantiate an object. |
| 26 | */ |
| 27 | public final class ObjectInstantiator { |
| 28 | |
| 29 | private ObjectInstantiator() { } |
| 30 | |
| 31 | /** |
| 32 | * Creates a new instance of <code>clazz</code> using the given parameters. |
| 33 | * If no constructor for the given parameters can be found in |
| 34 | * <code>clazz</code> an exception is thrown. |
| 35 | * |
| 36 | * @param <T> object type |
| 37 | * @param clazz class type |
| 38 | * @param initArgs constructor arguments |
| 39 | * @return an instance of clazz |
| 40 | * @throws RuntimeException instanziation failed |
| 41 | */ |
| 42 | @SuppressWarnings("unchecked") |
| 43 | public static <T> T newInstance(Class<T> clazz, Object[] initArgs) { |
| 44 | if (initArgs == null || initArgs.length == 0) { |
| 45 | try { |
| 46 | return clazz.newInstance(); |
| 47 | } catch (InstantiationException e) { |
| 48 | throw new RuntimeException(e); |
| 49 | } catch (IllegalAccessException e) { |
| 50 | throw new RuntimeException(e); |
| 51 | } |
| 52 | } else { |
| 53 | Constructor<?>[] constructors = clazz.getDeclaredConstructors(); |
| 54 | for (Constructor<T> constructor : (Constructor<T>[]) constructors) { |
| 55 | Class<?>[] constructorParams = constructor.getParameterTypes(); |
| 56 | if (constructorParams.length == initArgs.length) { |
| 57 | boolean match = true; |
| 58 | for (int i = 0; i < constructorParams.length && match; i++) { |
| 59 | if (initArgs[i] != null) { |
| 60 | Class<?> constructorParam = constructorParams[i]; |
| 61 | Class<?> initArg = initArgs[i].getClass(); |
| 62 | if (constructorParam.isPrimitive()) { |
| 63 | initArg = ReflectionUtils.unbox(initArg); |
| 64 | } |
| 65 | match = constructorParam.isAssignableFrom(initArg); |
| 66 | } |
| 67 | } |
| 68 | if (match) { |
| 69 | try { |
| 70 | return (T) constructor.newInstance(initArgs); |
| 71 | } catch (IllegalArgumentException e) { |
| 72 | throw new RuntimeException(e); |
| 73 | } catch (InstantiationException e) { |
| 74 | throw new RuntimeException(e); |
| 75 | } catch (IllegalAccessException e) { |
| 76 | throw new RuntimeException(e); |
| 77 | } catch (InvocationTargetException e) { |
| 78 | throw new RuntimeException(e); |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | throw new RuntimeException("Cannot find matching constructor"); |
| 85 | } |
| 86 | } |