KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dynaop > util > MethodHandle


1 package dynaop.util;
2
3 import java.io.IOException JavaDoc;
4 import java.io.Serializable JavaDoc;
5 import java.lang.reflect.Method JavaDoc;
6 import java.util.HashMap JavaDoc;
7 import java.util.Iterator JavaDoc;
8 import java.util.Map JavaDoc;
9
10 /**
11  * Serializable method handle.
12  *
13  * @author Bob Lee (crazybob@crazybob.org)
14  */

15 public class MethodHandle implements Serializable JavaDoc {
16     
17     static long serialVersionUID = 0;
18     
19     Class JavaDoc declaringClass;
20     String JavaDoc name;
21     Class JavaDoc[] parameterTypes;
22     
23     public MethodHandle(Method JavaDoc method) {
24         this.declaringClass = method.getDeclaringClass();
25         this.name = method.getName();
26         this.parameterTypes = method.getParameterTypes();
27     }
28     
29     public Method JavaDoc getMethod() throws NoSuchMethodException JavaDoc {
30         return declaringClass.getDeclaredMethod(this.name,
31             this.parameterTypes);
32     }
33
34     public static Map JavaDoc handleMethodKeys(Map JavaDoc map) {
35         Map JavaDoc handled = new HashMap JavaDoc();
36         for (Iterator JavaDoc i = map.entrySet().iterator(); i.hasNext();) {
37             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) i.next();
38             handled.put(
39                 new MethodHandle((Method JavaDoc) entry.getKey()),
40                 entry.getValue()
41             );
42         }
43         return handled;
44     }
45
46     public static Map JavaDoc unhandleMethodKeys(Map JavaDoc map) throws IOException JavaDoc {
47         try {
48             Map JavaDoc unhandled = new HashMap JavaDoc();
49             for (Iterator JavaDoc i = map.entrySet().iterator(); i.hasNext();) {
50                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) i.next();
51                 unhandled.put(
52                     ((MethodHandle) entry.getKey()).getMethod(),
53                     entry.getValue()
54                 );
55             }
56             return unhandled;
57         }
58         catch (NoSuchMethodException JavaDoc e) {
59             throw NestedException.wrap(e);
60         }
61     }
62 }
63
Popular Tags