Java Method Call invoke(Object object, String methodName)

Here you can find the source of invoke(Object object, String methodName)

Description

invoke

License

Open Source License

Declaration

public static Object invoke(Object object, String methodName)
            throws NoSuchMethodException, IllegalAccessException, InvocationTargetException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 *  Copyright (c) 2012 Google, Inc.//from  w  w  w. jav a 2  s.c o  m
 *  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:
 *  Google, Inc. - initial API and implementation
 *******************************************************************************/

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

public class Main {
    public static Object invoke(Object object, String methodName)
            throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        Class<?> objectClass = object.getClass();
        Method method;
        try {
            method = objectClass.getMethod(methodName, new Class<?>[] {});
        } catch (NoSuchMethodException e) {
            method = objectClass.getDeclaredMethod(methodName, new Class<?>[] {});
            method.setAccessible(true);
        }
        return method.invoke(object, new Object[] {});
    }
}

Related

  1. invoke(Object object, Method method)
  2. invoke(Object object, Object[] args, String methodName)
  3. invoke(Object object, String function, Object... params)
  4. invoke(Object object, String function, String parameter)
  5. invoke(Object object, String method, Object... args)
  6. invoke(Object object, String methodName, Class[] argTypes, Object... args)
  7. invoke(Object object, String methodName, Class returnType, Object... parameters)
  8. invoke(Object object, String methodName, Object[] args)
  9. invoke(Object objToInvoke, Class classToInvoke, String method, Class[] argumentClasses, Object[] arguments)