1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
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 }