View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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   * Save on relection by keeping around method, method argument, and constructor instances
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        // RpcController + Message in the method args
61        // (generated code from RPC bits in .proto files have RpcController)
62        arg = args[1];
63      } else if (args.length == 1) {
64        arg = args[0];
65      } else {
66        //unexpected
67        return null;
68      }
69      //in the protobuf methods, args[1] is the only significant argument
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  }