001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.util; 019 020import java.lang.reflect.InvocationTargetException; 021import java.lang.reflect.Method; 022import java.lang.reflect.UndeclaredThrowableException; 023import org.apache.hadoop.hbase.log.HBaseMarkers; 024import org.apache.yetus.audience.InterfaceAudience; 025import org.slf4j.Logger; 026import org.slf4j.LoggerFactory; 027 028@InterfaceAudience.Private 029public final class Methods { 030 private static final Logger LOG = LoggerFactory.getLogger(Methods.class); 031 032 private Methods() { 033 } 034 035 public static <T> Object call(Class<T> clazz, T instance, String methodName, Class[] types, 036 Object[] args) throws Exception { 037 try { 038 Method m = clazz.getMethod(methodName, types); 039 return m.invoke(instance, args); 040 } catch (IllegalArgumentException arge) { 041 LOG.error(HBaseMarkers.FATAL, "Constructed invalid call. class=" + clazz.getName() 042 + " method=" + methodName + " types=" + Classes.stringify(types), arge); 043 throw arge; 044 } catch (NoSuchMethodException nsme) { 045 throw new IllegalArgumentException( 046 "Can't find method " + methodName + " in " + clazz.getName() + "!", nsme); 047 } catch (InvocationTargetException ite) { 048 // unwrap the underlying exception and rethrow 049 if (ite.getTargetException() != null) { 050 if (ite.getTargetException() instanceof Exception) { 051 throw (Exception) ite.getTargetException(); 052 } else if (ite.getTargetException() instanceof Error) { 053 throw (Error) ite.getTargetException(); 054 } 055 } 056 throw new UndeclaredThrowableException(ite, 057 "Unknown exception invoking " + clazz.getName() + "." + methodName + "()"); 058 } catch (IllegalAccessException iae) { 059 throw new IllegalArgumentException( 060 "Denied access calling " + clazz.getName() + "." + methodName + "()", iae); 061 } catch (SecurityException se) { 062 LOG.error(HBaseMarkers.FATAL, "SecurityException calling method. class=" + clazz.getName() 063 + " method=" + methodName + " types=" + Classes.stringify(types), se); 064 throw se; 065 } 066 } 067}