KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tirsen > nanning > samples > prevayler > Call


1 package com.tirsen.nanning.samples.prevayler;
2
3 import java.io.Serializable JavaDoc;
4 import java.lang.reflect.InvocationTargetException JavaDoc;
5 import java.lang.reflect.Method JavaDoc;
6
7 import com.tirsen.nanning.Invocation;
8
9 public class Call implements Serializable JavaDoc {
10     static final long serialVersionUID = -3336463259251779539L;
11
12     protected Object JavaDoc target;
13     protected Object JavaDoc[] args;
14     private Object JavaDoc classIdentifier;
15     private Class JavaDoc interfaceClass;
16     private Class JavaDoc[] parameterTypes;
17     private String JavaDoc methodName;
18
19     public Call(Invocation invocation) throws Exception JavaDoc {
20         setInvocation(invocation);
21     }
22
23     public Call() {
24     }
25
26     protected void setInvocation(Invocation invocation) throws Exception JavaDoc {
27         classIdentifier = invocation.getAspectInstance().getClassIdentifier();
28         interfaceClass = invocation.getTargetInterface();
29         methodName = invocation.getMethod().getName();
30         parameterTypes = invocation.getMethod().getParameterTypes();
31         target = invocation.getProxy();
32         args = invocation.getArgs();
33     }
34
35     public Method JavaDoc getMethod() {
36         try {
37             return interfaceClass.getMethod(methodName, parameterTypes);
38         } catch (Exception JavaDoc e) {
39             throw new RuntimeException JavaDoc("did not find method " + methodName + " returning null", e);
40         }
41     }
42
43     public Class JavaDoc getInterfaceClass() {
44         return interfaceClass;
45     }
46
47     public Object JavaDoc getClassIdentifier() {
48         return classIdentifier;
49     }
50
51     public Object JavaDoc[] getArgs() {
52         return args;
53     }
54
55     public Object JavaDoc getTarget() {
56         return target;
57     }
58
59     public Object JavaDoc invoke() throws Exception JavaDoc {
60         final Method JavaDoc method = getMethod();
61         final Object JavaDoc target = getTarget();
62         final Object JavaDoc[] args = getArgs();
63
64         try {
65             return method.invoke(target, args);
66         } catch (InvocationTargetException JavaDoc e) {
67             if (e.getTargetException() instanceof Exception JavaDoc) {
68                 throw (Exception JavaDoc) e.getTargetException();
69             } else if (e.getTargetException() instanceof Error JavaDoc) {
70                 throw (Error JavaDoc) e.getTargetException();
71             } else {
72                 throw e;
73             }
74         }
75     }
76 }
77
Popular Tags