01: /*
02: * @(#)CallableMethod.java 1.3 05/05/25
03: *
04: * Copyright (c) 2004,2005 Sun Microsystems, Inc. All Rights Reserved.
05: *
06: * See the file "LICENSE.txt" for information on usage and redistribution
07: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
08: */
09: package org.pnuts.lang;
10:
11: import java.io.Serializable;
12: import pnuts.lang.Callable;
13: import pnuts.lang.Context;
14: import pnuts.lang.Runtime;
15:
16: /**
17: * Convert a set of methods to a callable object without specifying target
18: * object.
19: */
20: public class CallableMethod implements Callable, Serializable {
21:
22: static final long serialVersionUID = -3161644634782634511L;
23:
24: /**
25: * @serial
26: */
27: private Class cls;
28:
29: /**
30: * @serial
31: */
32: private String name;
33:
34: private transient Object target;
35:
36: protected CallableMethod() {
37: }
38:
39: /**
40: * Constructor
41: */
42: public CallableMethod(Class cls, String name) {
43: this .cls = cls;
44: this .name = name;
45: }
46:
47: /**
48: * Constructor
49: */
50: public CallableMethod(Object target, String name) {
51: this .target = target;
52: this .name = name;
53: this .cls = target.getClass();
54: }
55:
56: CallableMethod(Class cls, Object target, String name) {
57: this .cls = cls;
58: this .name = name;
59: this .target = target;
60: }
61:
62: public void setTargetObject(Object target) {
63: this .target = target;
64: }
65:
66: public Object getTargetObject() {
67: return target;
68: }
69:
70: public Object call(Object[] args, Context context) {
71: return Runtime.callMethod(context, cls, name, args, null,
72: target);
73: }
74:
75: public String toString() {
76: return "method " + name;
77: }
78: }
|