KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mdarad > framework > util > ClassUtils


1 /*
2     Mdarad-Toolobox is a collection of tools for Architected RAD
3     (Rapid Application Development) based on an MDA approach.
4     The toolbox contains frameworks and generators for many environments
5     (JAVA, J2EE, Hibernate, .NET, C++, etc.) which allow to generate
6     applications from a design Model
7     Copyright (C) 2004-2005 Elapse Technologies Inc.
8
9     This library is free software; you can redistribute it and/or
10     modify it under the terms of the GNU General Public
11     License as published by the Free Software Foundation; either
12     version 2.1 of the License, or (at your option) any later version.
13
14     This library is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17     General Public License for more details.
18
19     You should have received a copy of the GNU General Public
20     License along with this library; if not, write to the Free Software
21     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */

23 package org.mdarad.framework.util;
24
25 import java.lang.reflect.Constructor JavaDoc;
26 import java.lang.reflect.InvocationTargetException JavaDoc;
27
28 /**
29  * This class contains static methods used to make some
30  * operation on or with classes such as dyanamically instanciate
31  * a <code>Class</code> object.
32  * @author Philippe Brouillette
33  * @version 1.0
34  * @since 1.4
35  */

36 public class ClassUtils {
37     
38     /**
39      * Method that dynamically instanciates a class using the
40      * constructor with the argument types specified by the
41      * argument <code>construcotrArgTypes</code>. The values for
42      * the types are passed by <code>constructorArgs</code>.
43      * @param type the class to be instanciated
44      * @param constructorArgTypes the argument types of the constructor to use
45      * @param constructorArgs the values for the arguments of the constructor.
46      * @return an instance of the class
47      */

48     public static Object JavaDoc getInstance(Class JavaDoc type, Class JavaDoc[] constructorArgTypes, Object JavaDoc[] constructorArgs)
49             throws ClassUtilsException {
50         if (type == null) {
51             throw new IllegalArgumentException JavaDoc("The type should never be null");
52         }
53         Object JavaDoc object = null;
54         try {
55             Constructor JavaDoc constructor =
56                 type.getConstructor(constructorArgTypes);
57             object = constructor.newInstance(constructorArgs);
58         } catch (NoSuchMethodException JavaDoc nsme) {
59             throw new ClassUtilsException("Constructor could not be found", nsme);
60         } catch (InstantiationException JavaDoc ie) {
61             throw new ClassUtilsException("Instance could not be created", ie);
62         } catch (IllegalAccessException JavaDoc ie) {
63             throw new ClassUtilsException("Instance could not be created", ie);
64         } catch (InvocationTargetException JavaDoc ie) {
65             throw new ClassUtilsException("Instance could not be created", ie);
66         }
67         return object;
68     }
69 }
70
Popular Tags