Java Reflection Method Invoke invokeMethodByName(@Nonnull T object, @Nonnull String name, Class typeArg, Object arg)

Here you can find the source of invokeMethodByName(@Nonnull T object, @Nonnull String name, Class typeArg, Object arg)

Description

Do method from superclass by name.

License

Open Source License

Parameter

Parameter Description
object object from super class which need to invoke method
name name of method which need to invoke
typeArg type of method argument
arg argument of method

Exception

Parameter Description
Exception an exception

Declaration

public static <T> void invokeMethodByName(@Nonnull T object, @Nonnull String name, Class typeArg, Object arg)
        throws Exception 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2012-2015 Codenvy, S.A.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors://from  w w w  .j a  v a2  s  . co m
 *   Codenvy, S.A. - initial API and implementation
 *******************************************************************************/

import javax.annotation.Nonnull;

import java.lang.reflect.Method;

public class Main {
    /**
     * Do method from superclass by name.
     *
     * @param object
     *         object from super class which need to invoke method
     * @param name
     *         name of method which need to invoke
     * @param arg
     *         argument of method
     * @throws Exception
     */
    public static <T> void invokeMethodByName(@Nonnull T object, @Nonnull String name, Object arg)
            throws Exception {
        Method method = object.getClass().getDeclaredMethod(name, Object.class);

        method.setAccessible(true);

        method.invoke(object, arg);
    }

    /**
     * Do method from superclass by name.
     *
     * @param object
     *         object from super class which need to invoke method
     * @param name
     *         name of method which need to invoke
     * @param typeArg
     *         type of method argument
     * @param arg
     *         argument of method
     * @throws Exception
     */
    public static <T> void invokeMethodByName(@Nonnull T object, @Nonnull String name, Class typeArg, Object arg)
            throws Exception {
        Method method = object.getClass().getDeclaredMethod(name, typeArg);

        method.setAccessible(true);

        method.invoke(object, arg);
    }
}

Related

  1. invokeMethod(String name, Object target)
  2. invokeMethod2(Class cls, Object obj, String methodName, Object[] args)
  3. invokeMethodAndGet(Object object, String methodName, Object... args)
  4. invokeMethodAndGetRecursively(Object object, String methodName, Object... args)
  5. invokeMethodAndReturn(Class clazz, String method, Object object)
  6. invokeMethodByName(final Object obj, final String methodName, final Object[] args)
  7. invokeMethodHandleException(Method method, Object... args)
  8. invokeMethodNoArgs(final Object obj, final String methodName, final Class... returnTypePreference)
  9. invokeMethodNoArgs(Object target, String methodName)