1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.struts.chain.commands.util;
22
23
24 /**
25 * <p>Utility methods to load application classes and create instances.</p>
26 *
27 * @version $Rev: 471754 $ $Date: 2005-11-12 13:01:44 -0500 (Sat, 12 Nov 2005)
28 * $
29 */
30 public final class ClassUtils {
31
32
33 /**
34 * <p>Return the <code>Class</code> object for the specified fully
35 * qualified class name, from this web application's class loader.
36 *
37 * @param className Fully qualified class name
38 * @throws ClassNotFoundException if the specified class cannot be loaded
39 */
40 public static Class getApplicationClass(String className)
41 throws ClassNotFoundException {
42 if (className == null) {
43 throw new NullPointerException(
44 "getApplicationClass called with null className");
45 }
46
47 ClassLoader classLoader =
48 Thread.currentThread().getContextClassLoader();
49
50 if (classLoader == null) {
51 classLoader = ClassUtils.class.getClassLoader();
52 }
53
54 return (classLoader.loadClass(className));
55 }
56
57 /**
58 * <p>Return a new instance of the specified fully qualified class name,
59 * after loading the class (if necessary) from this web application's
60 * class loader.</p>
61 *
62 * @param className Fully qualified class name
63 * @throws ClassNotFoundException if the specified class cannot be loaded
64 * @throws IllegalAccessException if this class is not concrete
65 * @throws InstantiationException if this class has no zero-arguments
66 * constructor
67 */
68 public static Object getApplicationInstance(String className)
69 throws ClassNotFoundException, IllegalAccessException,
70 InstantiationException {
71 return (getApplicationClass(className).newInstance());
72 }
73 }