Java Method Call invoke(Object owner, String methodName)

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

Description

invoke

License

Open Source License

Declaration

static <T> T invoke(Object owner, String methodName) 

Method Source Code


//package com.java2s;
/*//  ww w  .  j  ava  2 s . co m
 * Copyright (c) 2006-2011 Rog?rio Liesenfeld
 * This file is subject to the terms of the MIT license (see LICENSE.txt).
 */

import java.lang.reflect.*;

public class Main {
    static <T> T invoke(Object owner, String methodName) {
        Method m;

        try {
            m = owner.getClass().getDeclaredMethod(methodName);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        }

        m.setAccessible(true);

        try {
            //noinspection unchecked
            return (T) m.invoke(owner);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e.getCause());
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. invoke(Object object, String methodName)
  2. invoke(Object object, String methodName, Class[] argTypes, Object... args)
  3. invoke(Object object, String methodName, Class returnType, Object... parameters)
  4. invoke(Object object, String methodName, Object[] args)
  5. invoke(Object objToInvoke, Class classToInvoke, String method, Class[] argumentClasses, Object[] arguments)
  6. invoke(Object proxy, java.lang.reflect.Method method, Object[] args)
  7. invoke(Object source, String key)
  8. invoke(Object target, Class clazz, String methodName, Class[] parameterTypes, Object... args)
  9. invoke(Object target, Class clazz, String method)