1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 package com.generationjava.lang;
33
34 import java.lang.reflect.InvocationTargetException;
35 import java.lang.reflect.Method;
36
37 import com.generationjava.collections.CollectionsW;
38
39 /***
40 * A set of static utilities for use with Classes.
41 *
42 * @author bayard@generationjava.com
43 * @date 2001-05-19
44 */
45 final public class ClassW {
46
47 /***
48 * Create an object from the classname. Must have an empty constructor.
49 *
50 * @param classname String name of the class
51 *
52 * @return Object instance of the class or null
53 */
54 static public Object createObject(String classname) {
55 Class tmpClass = null;
56
57 tmpClass = getClass(classname);
58
59 if(tmpClass == null) {
60
61 return null;
62 }
63
64
65 return createObject(tmpClass);
66 }
67
68 /***
69 * Create an object from a class.
70 *
71 * @param clss Class object to instantiate
72 *
73 * @return Object instance of the class or null
74 */
75 static public Object createObject(Class clss) {
76 if(clss == null) {
77
78 return null;
79 }
80
81 try {
82 return clss.newInstance();
83 } catch (IllegalAccessException iae) {
84
85
86 } catch (InstantiationException ie) {
87
88
89 }
90
91 return null;
92 }
93
94 /***
95 * Is this Class in the CLASSPATH
96 *
97 * @param classname String of the class
98 *
99 * @return boolean exists or not.
100 */
101 static public boolean classExists(String classname) {
102 Class tmpClass = null;
103
104
105 try {
106
107 tmpClass = Thread.currentThread().getContextClassLoader().loadClass(classname);
108 } catch (ClassNotFoundException cnfe) {
109 return false;
110 } catch (IllegalArgumentException iae) {
111 return false;
112 }
113
114 return true;
115 }
116
117 /***
118 * Get the Class object for a classname.
119 *
120 * @param classname String of the class
121 *
122 * @return Class instance for the class.
123 */
124 static public Class getClass(String classname) {
125 Class tmpClass = null;
126
127
128 try {
129
130 tmpClass = Thread.currentThread().getContextClassLoader().loadClass(classname);
131 } catch (ClassNotFoundException cnfe) {
132
133 } catch (IllegalArgumentException iae) {
134
135 }
136
137 return tmpClass;
138 }
139
140 static public void callMain(String[] args) {
141 callMain(args[0], CollectionsW.getSubArray(args, 1) );
142 }
143 static public void callMain(String classname, String[] args) {
144 callStatic(classname, "main", new Class[] { String[].class }, new Object[] { args } );
145 }
146 static public void callMain(Class clss, String[] args) {
147 callStatic(clss, "main", new Class[] { String[].class }, new Object[] { args } );
148 }
149
150
151
152 /***
153 * Runs a static method on a class.
154 *
155 * @param classname String name of class to invoke on.
156 * @param methodName String name of method to call.
157 * @param args Object[] arguments to method.
158 */
159 static public Object callStatic(String classname, String methodName,
160 Class[] types, Object[] args)
161 {
162 return callStatic(getClass(classname), methodName, types, args);
163 }
164
165 static public Object callStatic(Class clss, String methodName,
166 Class[] types, Object[] args)
167 {
168 try {
169 Method method = clss.getMethod(methodName, types );
170 return method.invoke( null, args );
171 } catch(NoSuchMethodException nsme) {
172 } catch(IllegalAccessException iae) {
173 } catch(InvocationTargetException ite) {
174 }
175 return null;
176 }
177
178 }