1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.ipc;
19
20 import java.lang.reflect.Method;
21 import java.util.Map;
22 import java.util.concurrent.ConcurrentHashMap;
23
24 import org.apache.hadoop.hbase.IpcProtocol;
25
26
27 import com.google.protobuf.Message;
28
29
30
31
32 class ReflectionCache {
33 private final Map<String, Message> methodArgCache = new ConcurrentHashMap<String, Message>();
34 private final Map<String, Method> methodInstanceCache = new ConcurrentHashMap<String, Method>();
35
36 public ReflectionCache() {
37 super();
38 }
39
40 Method getMethod(Class<? extends IpcProtocol> protocol, String methodName) {
41 Method method = this.methodInstanceCache.get(methodName);
42 if (method != null) return method;
43 Method [] methods = protocol.getMethods();
44 for (Method m : methods) {
45 if (m.getName().equals(methodName)) {
46 m.setAccessible(true);
47 this.methodInstanceCache.put(methodName, m);
48 return m;
49 }
50 }
51 return null;
52 }
53
54 Message getMethodArgType(Method method) throws Exception {
55 Message protoType = this.methodArgCache.get(method.getName());
56 if (protoType != null) return protoType;
57 Class<?>[] args = method.getParameterTypes();
58 Class<?> arg;
59 if (args.length == 2) {
60
61
62 arg = args[1];
63 } else if (args.length == 1) {
64 arg = args[0];
65 } else {
66
67 return null;
68 }
69
70 Method newInstMethod = arg.getMethod("getDefaultInstance");
71 newInstMethod.setAccessible(true);
72 protoType = (Message) newInstMethod.invoke(null, (Object[]) null);
73 this.methodArgCache.put(method.getName(), protoType);
74 return protoType;
75 }
76 }