View Javadoc

1   /*
2    * Copyright The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.apache.hadoop.hbase.util;
22  
23  import java.lang.reflect.InvocationTargetException;
24  import java.lang.reflect.Method;
25  import java.lang.reflect.UndeclaredThrowableException;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.apache.hadoop.hbase.classification.InterfaceAudience;
30  
31  @InterfaceAudience.Private
32  public class Methods {
33    private static final Log LOG = LogFactory.getLog(Methods.class);
34  
35    public static <T> Object call(Class<T> clazz, T instance, String methodName,
36        Class[] types, Object[] args) throws Exception {
37      try {
38        Method m = clazz.getMethod(methodName, types);
39        return m.invoke(instance, args);
40      } catch (IllegalArgumentException arge) {
41        LOG.fatal("Constructed invalid call. class="+clazz.getName()+
42            " method=" + methodName + " types=" + Classes.stringify(types), arge);
43        throw arge;
44      } catch (NoSuchMethodException nsme) {
45        throw new IllegalArgumentException(
46            "Can't find method "+methodName+" in "+clazz.getName()+"!", nsme);
47      } catch (InvocationTargetException ite) {
48        // unwrap the underlying exception and rethrow
49        if (ite.getTargetException() != null) {
50          if (ite.getTargetException() instanceof Exception) {
51            throw (Exception)ite.getTargetException();
52          } else if (ite.getTargetException() instanceof Error) {
53            throw (Error)ite.getTargetException();
54          }
55        }
56        throw new UndeclaredThrowableException(ite,
57            "Unknown exception invoking "+clazz.getName()+"."+methodName+"()");
58      } catch (IllegalAccessException iae) {
59        throw new IllegalArgumentException(
60            "Denied access calling "+clazz.getName()+"."+methodName+"()", iae);
61      } catch (SecurityException se) {
62        LOG.fatal("SecurityException calling method. class="+clazz.getName()+
63            " method=" + methodName + " types=" + Classes.stringify(types), se);
64        throw se;
65      }
66    }
67  }