01: /*
02: * SimpleInvoker.java
03: *
04: * Created on January 25, 2004, 5:53 PM
05: */
06:
07: package org.netbeans.actions.simple;
08:
09: import java.lang.reflect.InvocationTargetException;
10: import java.lang.reflect.Method;
11: import java.util.Map;
12:
13: /**
14: *
15: * @author tim
16: */
17: class SimpleInvoker {
18: private String targetClass;
19: private String targetMethod;
20: private String name;
21: private boolean isDirect;
22:
23: /** Creates a new instance of SimpleInvoker */
24: public SimpleInvoker(String name, String targetClass,
25: String targetMethod, boolean isDirect) {
26: this .targetClass = targetClass;
27: this .targetMethod = targetMethod;
28: this .isDirect = isDirect;
29: this .name = name;
30: }
31:
32: public String getName() {
33: return name;
34: }
35:
36: private Class clazz = null;
37:
38: private Class getTargetClass() throws ClassNotFoundException {
39: if (clazz == null) {
40: clazz = Class.forName(targetClass);
41: }
42: return clazz;
43: }
44:
45: private Method method = null;
46:
47: private Method getTargetMethod() throws InvocationTargetException,
48: ClassNotFoundException, IllegalAccessException,
49: NoSuchMethodException {
50: if (method == null) {
51: Class c = getTargetClass();
52: method = c.getDeclaredMethod(targetMethod, null);
53: method.setAccessible(true);
54: }
55: return method;
56: }
57:
58: public void invoke(Map context) {
59: try {
60: if (isDirect) {
61: getTargetMethod().invoke(null, null);
62: } else {
63: Class clazz = getTargetClass();
64: Object o = context.get(clazz);
65: if (o == null) {
66: throw new NullPointerException("No instance of "
67: + clazz + " in context");
68: }
69: getTargetMethod().invoke(o, null);
70: }
71: } catch (Exception e) {
72: e.printStackTrace();
73: }
74: }
75:
76: public String toString() {
77: return getClass() + "[" + targetClass + " method "
78: + targetMethod + " name=" + getName() + " isDirect="
79: + isDirect + "]";
80: }
81:
82: public int hashCode() {
83: return getName().hashCode();
84: }
85:
86: public boolean equals(Object o) {
87: boolean result = false;
88: if (o.getClass() == SimpleInvoker.class) {
89: result = ((SimpleInvoker) o).getName().equals(toString());
90: }
91: return result;
92: }
93:
94: }
|