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.classification.InterfaceAudience;
30  import org.apache.hadoop.classification.InterfaceStability;
31  
32  @InterfaceAudience.Public
33  @InterfaceStability.Stable
34  public class Methods {
35    private static Log LOG = LogFactory.getLog(Methods.class);
36  
37    public static <T> Object call(Class<T> clazz, T instance, String methodName,
38        Class[] types, Object[] args) throws Exception {
39      try {
40        Method m = clazz.getMethod(methodName, types);
41        return m.invoke(instance, args);
42      } catch (IllegalArgumentException arge) {
43        LOG.fatal("Constructed invalid call. class="+clazz.getName()+
44            " method=" + methodName + " types=" + Classes.stringify(types), arge);
45        throw arge;
46      } catch (NoSuchMethodException nsme) {
47        throw new IllegalArgumentException(
48            "Can't find method "+methodName+" in "+clazz.getName()+"!", nsme);
49      } catch (InvocationTargetException ite) {
50        // unwrap the underlying exception and rethrow
51        if (ite.getTargetException() != null) {
52          if (ite.getTargetException() instanceof Exception) {
53            throw (Exception)ite.getTargetException();
54          } else if (ite.getTargetException() instanceof Error) {
55            throw (Error)ite.getTargetException();
56          }
57        }
58        throw new UndeclaredThrowableException(ite,
59            "Unknown exception invoking "+clazz.getName()+"."+methodName+"()");
60      } catch (IllegalAccessException iae) {
61        throw new IllegalArgumentException(
62            "Denied access calling "+clazz.getName()+"."+methodName+"()", iae);
63      } catch (SecurityException se) {
64        LOG.fatal("SecurityException calling method. class="+clazz.getName()+
65            " method=" + methodName + " types=" + Classes.stringify(types), se);
66        throw se;
67      }
68    }
69  }