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