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.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
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 }