Java Reflection Method Invoke invokeMethodOnObject(Object target, String methodName)

Here you can find the source of invokeMethodOnObject(Object target, String methodName)

Description

Invoke an arbitrary method that takes no arguments.

License

Open Source License

Parameter

Parameter Description
target The object on which to invoke the method
methodName The name of the method, which should take no parameters

Return

The value returned by the method.

Declaration

public static Object invokeMethodOnObject(Object target, String methodName) 

Method Source Code


//package com.java2s;
/*/* w  w  w.j a  va 2s .  c  om*/
 * The contents of this file are subject to the Automated Business Logic Public License Version 1.0 (the "License"),
 * which is derived from the Mozilla Public License version 1.1. You may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at http://www.automatedbusinesslogic.com/license/public-license
 *
 * 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.Method;

public class Main {
    /**
     * Invoke an arbitrary method that takes no arguments.
     * @param target The object on which to invoke the method
     * @param methodName The name of the method, which should take no parameters
     * @return The value returned by the method.
     */
    public static Object invokeMethodOnObject(Object target, String methodName) {
        try {
            // This is done this way so that we can invoke non-public methods. We don't really want to require people
            // to declare all their business logic methods as public because it's just plain distracting.
            Class<?> cls = target.getClass();
            while (cls != null) {
                Method[] allMethods = cls.getDeclaredMethods();
                for (Method meth : allMethods) {
                    if (meth.getName().equals(methodName) && meth.getParameterTypes().length == 0) {
                        meth.setAccessible(true);
                        return meth.invoke(target, (Object[]) null);
                    }
                }
                cls = cls.getSuperclass();
            }
            throw new RuntimeException("No such method: " + target.getClass().getName() + "." + methodName + "()");
        } catch (Exception ex) {
            throw new RuntimeException(
                    "Exception while invoking method " + target.getClass().getName() + "." + methodName, ex);
        }
    }
}

Related

  1. invokeMethodNoArgs(final Object obj, final String methodName, final Class... returnTypePreference)
  2. invokeMethodNoArgs(Object target, String methodName)
  3. invokeMethodOfClass(Class target, Method method)
  4. invokeMethodOnLoader(ClassLoader cl, String methodName, Object... params)
  5. invokeMethodOnObject(Method method, Object objectToInvokeOn, Object[] args)
  6. invokeMethodQuietly(Object receiver, Method method, Object... args)
  7. invokeMethods(final Object target, final List methods)
  8. invokeMethodsWithAnnotation(final Class annotation, final Object instance, final Object... args)
  9. invokeMethodWithArray2(Object target, Method method, Object[] args)