Java Method Call invokePublicMethod(Object o, String methodName, Class[] classes, Object[] objects)

Here you can find the source of invokePublicMethod(Object o, String methodName, Class[] classes, Object[] objects)

Description

invoke Public Method

License

Apache License

Declaration

public static void invokePublicMethod(Object o, String methodName, Class[] classes, Object[] objects) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.PrintStream;

import java.lang.reflect.Method;

public class Main {
    public static Object invokePublicMethod(Object o, String methodName) {
        try {/*from  w  w w.  j av a 2 s  .c o  m*/
            Method m = o.getClass().getMethod(methodName, new Class[0]);
            m.setAccessible(true);
            return m.invoke(o, new Object[0]);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void invokePublicMethod(Object o, String methodName, Class[] classes, Object[] objects) {
        try {
            Method m = o.getClass().getMethod(methodName, classes);
            m.setAccessible(true);
            m.invoke(o, objects);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static Method getMethod(Class<?> c, String methodName, Class<?>... parameterTypes) {
        try {
            try {
                return c.getDeclaredMethod(methodName, parameterTypes);
            } catch (NoSuchMethodException ex) {
                Class<?> superclass = c.getSuperclass();
                if (superclass != null) {
                    return getMethod(superclass, methodName, parameterTypes);
                }

                return null;
            }
        } catch (RuntimeException ex) {
            throw ex;
        } catch (Exception ex) {
            // throw newstate ImplementationError(ex);
            throw new RuntimeException("");
        }
    }

    public static void printStackTrace(PrintStream out, StackTraceElement[] stackTrace) {
        for (int i = 2; i < stackTrace.length; i++) {
            StackTraceElement stackTraceElement = stackTrace[i];
            out.println("\tat " + stackTraceElement);
        }
    }

    public static void printStackTrace(StackTraceElement[] stackTrace) {
        printStackTrace(System.err, stackTrace);
    }

    /**
     * Returns the declared method in the specified class or any of its super
     * classes.
     * 
     * @param c
     * @param methodName
     * @param types
     * @return
     * @throws NoSuchMethodException
     */
    public static Method getDeclaredMethod(Class<?> c, String methodName, Class<?>[] types)
            throws NoSuchMethodException {
        Method m = null;
        while (m == null && c != null) {
            try {
                m = c.getDeclaredMethod(methodName, types);
            } catch (SecurityException e) {
                throw new RuntimeException(e);
            } catch (NoSuchMethodException e) {
                // nop
            }
            c = c.getSuperclass();
        }
        if (m == null) {
            throw new NoSuchMethodException();
        }
        return m;
    }
}

Related

  1. invokePrivateMethod(String methodName, Class clazz, Object object)
  2. invokeProperty(Object obj, String property)
  3. invokeProtectedMethod(Class c, String method, Object... args)
  4. invokeProtectedMethod(Object o, Object[] args, String methodName, Class[] types)
  5. invokeProxied(final Callable callable, final ClassLoader classLoader)
  6. invokeQuietly(MethodHandle handle, Object instance)
  7. invokeQuietly(Object target, Method method, Object[] params)
  8. invokeReadResolveOn(Object object, Class clz)
  9. invokeReal(Object real, Method m, Object[] args)