Invocation.java

  1. /*
  2.  *    Copyright 2009-2024 the original author or authors.
  3.  *
  4.  *    Licensed under the Apache License, Version 2.0 (the "License");
  5.  *    you may not use this file except in compliance with the License.
  6.  *    You may obtain a copy of the License at
  7.  *
  8.  *       https://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  *    Unless required by applicable law or agreed to in writing, software
  11.  *    distributed under the License is distributed on an "AS IS" BASIS,
  12.  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  *    See the License for the specific language governing permissions and
  14.  *    limitations under the License.
  15.  */
  16. package org.apache.ibatis.plugin;

  17. import java.lang.reflect.InvocationTargetException;
  18. import java.lang.reflect.Method;
  19. import java.util.Arrays;
  20. import java.util.List;

  21. import org.apache.ibatis.executor.Executor;
  22. import org.apache.ibatis.executor.parameter.ParameterHandler;
  23. import org.apache.ibatis.executor.resultset.ResultSetHandler;
  24. import org.apache.ibatis.executor.statement.StatementHandler;

  25. /**
  26.  * @author Clinton Begin
  27.  */
  28. public class Invocation {

  29.   private static final List<Class<?>> targetClasses = Arrays.asList(Executor.class, ParameterHandler.class,
  30.       ResultSetHandler.class, StatementHandler.class);
  31.   private final Object target;
  32.   private final Method method;
  33.   private final Object[] args;

  34.   public Invocation(Object target, Method method, Object[] args) {
  35.     if (!targetClasses.contains(method.getDeclaringClass())) {
  36.       throw new IllegalArgumentException("Method '" + method + "' is not supported as a plugin target.");
  37.     }
  38.     this.target = target;
  39.     this.method = method;
  40.     this.args = args;
  41.   }

  42.   public Object getTarget() {
  43.     return target;
  44.   }

  45.   public Method getMethod() {
  46.     return method;
  47.   }

  48.   public Object[] getArgs() {
  49.     return args;
  50.   }

  51.   public Object proceed() throws InvocationTargetException, IllegalAccessException {
  52.     return method.invoke(target, args);
  53.   }

  54. }