KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > core > util > ClassUtils


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: ClassUtils.java,v 1.4 2007/01/07 06:14:01 bastafidli Exp $
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21
22 package org.opensubsystems.core.util;
23
24 import java.util.logging.Logger JavaDoc;
25
26 import org.opensubsystems.core.error.OSSDynamicClassException;
27 import org.opensubsystems.core.error.OSSException;
28
29 /**
30  * Set of utilities to work with classes.
31  *
32  * @version $Id: ClassUtils.java,v 1.4 2007/01/07 06:14:01 bastafidli Exp $
33  * @author Miro Halas
34  * @code.reviewer Miro Halas
35  * @code.reviewed Initial revision
36  */

37 public final class ClassUtils
38 {
39    // Cached values ////////////////////////////////////////////////////////////
40

41    /**
42     * Logger for this class
43     */

44    private static Logger JavaDoc s_logger = Log.getInstance(ClassUtils.class);
45
46    // Constructors /////////////////////////////////////////////////////////////
47

48    /**
49     * Private constructor since this class cannot be instantiated
50     */

51    private ClassUtils(
52    )
53    {
54       // Do nothing
55
}
56
57    // Public methods ///////////////////////////////////////////////////////////
58

59    /**
60     * Create new instance of the class
61     *
62     * @param strClassName - identifier of the class for which new instance
63     * should be created
64     * @return Object - new instance of the object
65     * @throws OSSException - an error has occured
66     */

67    public static Object JavaDoc createNewInstance(
68       String JavaDoc strClassName
69    ) throws OSSException
70    {
71       Object JavaDoc objInstance;
72
73       try
74       {
75          objInstance = createNewInstance(Class.forName(strClassName));
76       }
77       catch (ClassNotFoundException JavaDoc eNoClass)
78       {
79          throw new OSSDynamicClassException("Unexpected exception.", eNoClass);
80       }
81       
82       return objInstance;
83    }
84
85    /**
86     * Create new instance of the class
87     *
88     * @param templateClass - identifier of the class for which new instance
89     * should be created
90     * @return Object - new instance of the object
91     * @throws OSSException - an error has occured
92     */

93    public static Object JavaDoc createNewInstance(
94       Class JavaDoc templateClass
95    ) throws OSSException
96    {
97       Object JavaDoc objInstance;
98
99       try
100       {
101          objInstance = templateClass.newInstance();
102          s_logger.finest("Instantiated class " + templateClass.getName());
103       }
104       catch (IllegalAccessException JavaDoc eIllAcc)
105       {
106          throw new OSSDynamicClassException("Unexpected exception.", eIllAcc);
107       }
108       catch (InstantiationException JavaDoc ineExc)
109       {
110          throw new OSSDynamicClassException("Unexpected exception.", ineExc);
111       }
112       
113       return objInstance;
114    }
115 }
116
Popular Tags