1 package org.catacomb.interlish.reflect;
2
3 import java.lang.reflect.Constructor;
4
5 import org.catacomb.be.Instantiator;
6 import org.catacomb.report.E;
7
8
9
10 public class ClassInstantiator implements Instantiator {
11
12
13
14
15 public Object newInstance(String cnm) {
16 Object ret = null;
17 Class<?> c = null;
18 try {
19 c = Class.forName(cnm);
20 ret = c.newInstance();
21
22 } catch (Exception e) {
23 E.error("cant instantiate " + cnm + " " + e);
24 }
25 return ret;
26 }
27
28
29
30 public Object newInstance(String cnm, Object arg) {
31 Object ret = null;
32 Class<?> c = null;
33 try {
34 c = Class.forName(cnm);
35 Class[] ca = {arg.getClass()};
36 Constructor con = c.getConstructor(ca);
37 Object[] oa = {arg};
38 ret = con.newInstance(oa);
39
40 } catch (Exception e) {
41 E.error("cant instantiate " + cnm + " " + e);
42 }
43 return ret;
44 }
45
46
47
48 public Class<?> forName(String s) {
49 return forName(s, false);
50 }
51
52
53 public Class<?> forName(String scl, boolean require) {
54 Class<?> ret = null;
55 try {
56 ret = Class.forName(scl);
57 } catch (Exception ex) {
58 if (require) {
59 E.error("cant instantiate: " + scl + " " + ex);
60 }
61 }
62 return ret;
63 }
64
65 public void reset() {
66 E.missing("reset does nothing on the class instantiator");
67 }
68
69 public ClassLoader getLoader() {
70 return null;
71 }
72
73 }