1 /**
2 * Copyright 2008 Bryan Ray
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package uk.org.bryanray.testtoys.util;
17
18 import java.lang.reflect.Constructor;
19 import java.lang.reflect.InvocationTargetException;
20
21 /**
22 * <DL>
23 * <DT><B>Project:</B></DT>
24 * <DD>TestDataLoader</DD>
25 * <DT><B>Filename:</B></DT>
26 * <DD>BeanUtil.java</DD>
27 * <DT><B>Creation Date:</B></DT>
28 * <DD>26 May 2008</DD>
29 * </DL>
30 *
31 * @author Bryan Ray
32 */
33 public class BeanUtil {
34 /**
35 * Creates a new instance of a class, wrapping any thrown exceptions using
36 * {@link ExceptionUtil#convertException(Exception)}.
37 *
38 * @param className
39 * The fully qualified class name to instantiate.
40 * @param constructorTypes
41 * The types of the constructor arguments.
42 * @param constructorValues
43 * The values of the constructor arguments.
44 */
45 public static Object createInstance(String className,
46 Class<?>[] constructorTypes, Object[] constructorValues) {
47 try {
48 Class<?> clazz = Class.forName(className);
49 Constructor<?> constructor = clazz.getConstructor(constructorTypes);
50 return constructor.newInstance(constructorValues);
51 } catch (ClassNotFoundException cnfe) {
52 throw ExceptionUtil.convertException(cnfe);
53 } catch (NoSuchMethodException nsme) {
54 throw ExceptionUtil.convertException(nsme);
55 } catch (IllegalAccessException e) {
56 throw ExceptionUtil.convertException(e);
57 } catch (InstantiationException ie) {
58 throw ExceptionUtil.convertException(ie);
59 } catch (InvocationTargetException ite) {
60 throw ExceptionUtil.convertException(ite);
61 }
62 }
63 }