KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > displaytag > util > ReflectHelper


1 /**
2  * Licensed under the Artistic License; you may not use this file
3  * except in compliance with the License.
4  * You may obtain a copy of the License at
5  *
6  * http://displaytag.sourceforge.net/license.html
7  *
8  * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
9  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
10  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
11  */

12 package org.displaytag.util;
13
14 /**
15  * Utility method for reflection.
16  * @author Fabrizio Giustina
17  * @version $Revision: 720 $ ($Author: fgiust $)
18  */

19 public final class ReflectHelper
20 {
21
22     /**
23      * Don't instantiate.
24      */

25     private ReflectHelper()
26     {
27         // unused
28
}
29
30     /**
31      * Tries to load a class with more classloaders. Can be useful in J2EE applications if jar is loaded from a
32      * different classloader than user classes. If class is not found using the standard classloader, tries whit the
33      * thread classloader.
34      * @param className class name
35      * @return Class loaded class
36      * @throws ClassNotFoundException if none of the ClassLoaders is able to found the reuested class
37      */

38     public static Class JavaDoc classForName(String JavaDoc className) throws ClassNotFoundException JavaDoc
39     {
40         try
41         {
42             // trying with the default ClassLoader
43
return Class.forName(className);
44         }
45         catch (ClassNotFoundException JavaDoc cnfe)
46         {
47             try
48             {
49                 // trying with thread ClassLoader
50
Thread JavaDoc thread = Thread.currentThread();
51                 ClassLoader JavaDoc threadClassLoader = thread.getContextClassLoader();
52                 return Class.forName(className, false, threadClassLoader);
53             }
54             catch (ClassNotFoundException JavaDoc cnfe2)
55             {
56                 throw cnfe2;
57             }
58         }
59     }
60
61 }
Popular Tags