KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > util > ClassUtil


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2006 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.util;
14
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17
18
19 /**
20  * Utility methods for classes.
21  * @author Fabrizio Giustina
22  * @version $Revision: 7343 $ ($Author: philipp $)
23  */

24 public final class ClassUtil {
25
26     /**
27      * Logger.
28      */

29     private static Logger log = LoggerFactory.getLogger(ClassUtil.class);
30
31     /**
32      * Don't instantiate.
33      */

34     private ClassUtil() {
35         // unused
36
}
37
38     /**
39      * Load a class trying both with the standard that with the thread classloader.
40      * @param className class name
41      * @return loaded class
42      * @throws ClassNotFoundException if the given class can't be loaded by both classloaders.
43      */

44     public static Class JavaDoc classForName(String JavaDoc className) throws ClassNotFoundException JavaDoc {
45         Class JavaDoc loadedClass;
46         try {
47             loadedClass = Class.forName(className);
48         }
49         catch (ClassNotFoundException JavaDoc e) {
50             loadedClass = Class.forName(className, true, Thread.currentThread().getContextClassLoader());
51         }
52         return loadedClass;
53     }
54
55     /**
56      * Shortcut for <code>ClassUtil.classForName(className).newInstance()</code>
57      * @param className class name
58      * @return instance of the given class
59      * @throws InstantiationException exception thrown by newInstance()
60      * @throws IllegalAccessException exception thrown by newInstance()
61      * @throws ClassNotFoundException if the given class can't be loaded by both classloaders
62      */

63     public static Object JavaDoc newInstance(String JavaDoc className) throws InstantiationException JavaDoc, IllegalAccessException JavaDoc,
64         ClassNotFoundException JavaDoc {
65         return classForName(className).newInstance();
66     }
67 }
68
Popular Tags