2 * Copyright The Apache Software Foundation
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 package org
.apache
.hadoop
.hbase
.util
;
22 import java
.lang
.reflect
.InvocationTargetException
;
23 import java
.lang
.reflect
.Method
;
24 import java
.lang
.reflect
.UndeclaredThrowableException
;
26 import org
.apache
.hadoop
.hbase
.log
.HBaseMarkers
;
27 import org
.apache
.yetus
.audience
.InterfaceAudience
;
28 import org
.slf4j
.Logger
;
29 import org
.slf4j
.LoggerFactory
;
31 @InterfaceAudience.Private
32 public final class Methods
{
33 private static final Logger LOG
= LoggerFactory
.getLogger(Methods
.class);
38 public static <T
> Object
call(Class
<T
> clazz
, T instance
, String methodName
,
39 Class
[] types
, Object
[] args
) throws Exception
{
41 Method m
= clazz
.getMethod(methodName
, types
);
42 return m
.invoke(instance
, args
);
43 } catch (IllegalArgumentException arge
) {
44 LOG
.error(HBaseMarkers
.FATAL
, "Constructed invalid call. class="+clazz
.getName()+
45 " method=" + methodName
+ " types=" + Classes
.stringify(types
), arge
);
47 } catch (NoSuchMethodException nsme
) {
48 throw new IllegalArgumentException(
49 "Can't find method "+methodName
+" in "+clazz
.getName()+"!", nsme
);
50 } catch (InvocationTargetException ite
) {
51 // unwrap the underlying exception and rethrow
52 if (ite
.getTargetException() != null) {
53 if (ite
.getTargetException() instanceof Exception
) {
54 throw (Exception
)ite
.getTargetException();
55 } else if (ite
.getTargetException() instanceof Error
) {
56 throw (Error
)ite
.getTargetException();
59 throw new UndeclaredThrowableException(ite
,
60 "Unknown exception invoking "+clazz
.getName()+"."+methodName
+"()");
61 } catch (IllegalAccessException iae
) {
62 throw new IllegalArgumentException(
63 "Denied access calling "+clazz
.getName()+"."+methodName
+"()", iae
);
64 } catch (SecurityException se
) {
65 LOG
.error(HBaseMarkers
.FATAL
, "SecurityException calling method. class="+
66 clazz
.getName()+" method=" + methodName
+ " types=" +
67 Classes
.stringify(types
), se
);