Java Method Call invoke(String name, Object object, Object... args)

Here you can find the source of invoke(String name, Object object, Object... args)

Description

invoke

License

Open Source License

Declaration

public static Object invoke(String name, Object object, Object... args) 

Method Source Code

//package com.java2s;
/*/*from  w  w w .  ja v  a2  s .c o m*/
 * Copyright (C) 2006-2007 Mindquarry GmbH, All Rights Reserved
 *
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (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.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 */

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Main {
    public static Object invoke(Method method, Object object, Object... args) {
        try {
            return method.invoke(object, args);
        } catch (IllegalArgumentException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e.getTargetException());
        }
    }

    public static Object invoke(String name, Object object, Object... args) {
        try {
            Class<?>[] parameterTypes = new Class<?>[args.length];
            for (int i = 0; i < args.length; i++)
                parameterTypes[i] = args[i].getClass();

            Method method = findMethod(object.getClass(), name, parameterTypes);
            return method.invoke(object, args);

        } catch (IllegalArgumentException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e.getTargetException());
        }
    }

    public static Method findMethod(Class<?> clazz, String name, Class<?>[] parameterTypes) {
        try {
            return clazz.getMethod(name, parameterTypes);
        } catch (SecurityException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. invoke(String cls_name, String method, Class[] param_cls, Object cls, Object[] params)
  2. invoke(String method)
  3. invoke(String methodName, Object obj)
  4. invoke(String methodName, Object object, Class[] argTypes, Object[] args)
  5. invoke(String methodName, Object target, Class targetClass, Object[] args)
  6. invoke_ex(Object obj, String method, Class[] params, Object[] args)
  7. invokeAccessableMethodWithArguments(Object instance, String method, Object... arguments)
  8. invokeAndCastCollection(T returnType, Method m, Object obj, Object... args)
  9. invokeAnnotatedDeclaredMethod(Object obj, Class annotationType)