Java Reflection Method Invoke invoke(Method method, Object it, Object... args)

Here you can find the source of invoke(Method method, Object it, Object... args)

Description

Invokes the specified method on the given it Object with the given arguments.

License

Apache License

Parameter

Parameter Description
method a parameter
it a parameter
args a parameter

Exception

Parameter Description
InvocationTargetException an exception
Error an exception

Declaration

public static Object invoke(Method method, Object it, Object... args) throws InvocationTargetException 

Method Source Code


//package com.java2s;
/*/* w w w  . j av  a  2s.c  o  m*/
 * Copyright 2011-2015 B2i Healthcare Pte Ltd, http://b2i.sg
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import static com.google.common.collect.Lists.newArrayList;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import com.google.common.base.Joiner;

public class Main {
    /**
     * Invokes the specified method on the given it Object with the given
     * arguments. Throws an {@link Error} if the method invocation results in a
     * checked exception.
     * 
     * @param method
     * @param it
     * @param args
     * @throws InvocationTargetException
     * @throws Error
     * @return
     * @see Method#invoke(Object, Object...)
     */
    public static Object invoke(Method method, Object it, Object... args) throws InvocationTargetException {
        try {
            method.setAccessible(true);
            return method.invoke(it, args);
        } catch (IllegalArgumentException e) {
            throw new Error(
                    String.format("Method '%s' rejected arguments(%s)", method.getName(), argumentList(args)), e);
        } catch (IllegalAccessException e) {
            throw new Error("Method became inaccessible.", e);
        } catch (InvocationTargetException e) {
            if (e.getCause() instanceof Error) {
                throw (Error) e.getCause();
            }
            throw e;
        }
    }

    /**
     * Creates a comma separated list from the given arguments.
     * 
     * @param args
     * @return
     */
    public static String argumentList(Object[] args) {
        if (args == null) {
            return "null";
        }
        return Joiner.on(", ").useForNull("null").join(newArrayList(args));
    }
}

Related

  1. invoke(Method m, Object target, Object... params)
  2. invoke(Method method)
  3. invoke(Method method)
  4. invoke(Method method, Object data, Object... arguments)
  5. invoke(Method method, Object instance, Object... parameters)
  6. invoke(Method method, Object javaBean, Object value)
  7. invoke(Method method, Object obj, Object... args)
  8. invoke(Method method, Object obj, Object... args)
  9. invoke(Method method, Object object, Object... args)