001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.utils;
014
015import java.lang.reflect.*;
016
017/**
018 * A wrapper around a {@link Method#invoke(Object, Object...)} method that allows for basic instrumentation.
019 */
020public class MethodInvoker {
021   private final Method m;
022   private final MethodExecStats stats;
023
024   /**
025    * Constructor.
026    *
027    * @param m The method being wrapped.
028    * @param stats The instrumentor.
029    */
030   public MethodInvoker(Method m, MethodExecStats stats) {
031      this.m = m;
032      this.stats = stats;
033   }
034
035   /**
036    * Returns the inner method.
037    *
038    * @return The inner method.
039    */
040   public Method inner() {
041      return m;
042   }
043
044   /**
045    * Invokes the underlying method.
046    *
047    * @param o  The object the underlying method is invoked from.
048    * @param args  The arguments used for the method call.
049    * @return  The result of dispatching the method represented by this object on {@code obj} with parameters {@code args}
050    * @throws IllegalAccessException Thrown from underlying method.
051    * @throws IllegalArgumentException Thrown from underlying method.
052    * @throws InvocationTargetException Thrown from underlying method.
053    */
054   public Object invoke(Object o, Object...args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
055      long startTime = System.nanoTime();
056      stats.started();
057      try {
058         return m.invoke(o, args);
059      } catch (IllegalAccessException|IllegalArgumentException e) {
060         stats.error(e);
061         throw e;
062      } catch (InvocationTargetException e) {
063         stats.error(e.getTargetException());
064         throw e;
065      } finally {
066         stats.finished(System.nanoTime() - startTime);
067      }
068   }
069
070   /**
071    * Convenience method for calling <c>inner().getDeclaringClass()</c>
072    *
073    * @return The declaring class of the method.
074    */
075   public Class<?> getDeclaringClass() {
076      return m.getDeclaringClass();
077   }
078
079   /**
080    * Convenience method for calling <c>inner().getName()</c>
081    *
082    * @return The name of the method.
083    */
084   public String getName() {
085      return m.getName();
086   }
087}