01: package uk.org.ponder.reflect;
02:
03: import uk.org.ponder.util.UniversalRuntimeException;
04:
05: /**
06: * Owing to a peculiar bug/inconsistency in certain JVMs, this class is
07: * necessary to swallow the ClassNotFoundException that otherwise cannot be
08: * handled if Class.forName is tried in certain contexts (static/inner)
09: */
10:
11: public class ClassGetter {
12: /**
13: * Returns the Class object corresponding to the supplied fully-qualified
14: * classname, as if looked up by Class.forName().
15: *
16: * @param classname The classname to be looked up.
17: * @return The Class object corresponding to the classname, or
18: * <code>null</code> if the name cannot be looked up.
19: */
20: //TODO: initialise array classes as per this posting:
21: //http://lists.gnu.org/archive/html/classpath/2003-01/msg00022.html
22: public static Class forName(String classname) {
23: try {
24: return Class.forName(classname);
25: } catch (ClassNotFoundException cnfe) {
26: return null;
27: }
28: }
29:
30: /**
31: * Constructs an object of the given class with only a runtime exception in
32: * the case of failure.
33: *
34: * @param class1
35: * @return
36: */
37: // TODO: replace with ReflectiveCache implementation.
38: public static Object construct(Class clazz) {
39: try {
40: return clazz.newInstance();
41: } catch (Throwable t) {
42: throw UniversalRuntimeException.accumulate(t,
43: "Could not create instance of " + clazz
44: + " using default constructor");
45: }
46: }
47: }
|