Java Reflection Method Static Invoke invokeStatic(final ClassLoader loader, final String className, final String methodName, final Object... parameters)

Here you can find the source of invokeStatic(final ClassLoader loader, final String className, final String methodName, final Object... parameters)

Description

Invokes a static method of a given class.

License

Open Source License

Parameter

Parameter Description
loader the class loader with which to load the class
className the name of the class whose static method is to be called
methodName the name of the static method to be called
parameters the parameters to pass to the static method

Exception

Parameter Description
SecurityException an exception
NoSuchMethodException an exception
IllegalArgumentException an exception
IllegalAccessException an exception
InvocationTargetException an exception
ClassNotFoundException an exception

Return

the return value of the static method, if any

Declaration

@SuppressWarnings("unchecked")
public static <T> T invokeStatic(final ClassLoader loader, final String className, final String methodName,
        final Object... parameters) throws SecurityException, NoSuchMethodException, IllegalArgumentException,
        IllegalAccessException, InvocationTargetException, ClassNotFoundException 

Method Source Code

//package com.java2s;

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

public class Main {
    /**/*from w w  w  . ja  v  a2  s . co  m*/
     * Invokes a static method of a given class.
     * <p>
     * This method tries to find a static method matching the given name and the
     * parameter list. Just like {@link #newInstance(String, Object...)}, this
     * works via reflection to avoid a compile-time dependency on ImageJ2.
     * </p>
     * 
     * @param loader the class loader with which to load the class
     * @param className the name of the class whose static method is to be called
     * @param methodName the name of the static method to be called
     * @param parameters the parameters to pass to the static method
     * @return the return value of the static method, if any
     * @throws SecurityException
     * @throws NoSuchMethodException
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     * @throws ClassNotFoundException
     */
    @SuppressWarnings("unchecked")
    public static <T> T invokeStatic(final ClassLoader loader, final String className, final String methodName,
            final Object... parameters) throws SecurityException, NoSuchMethodException, IllegalArgumentException,
            IllegalAccessException, InvocationTargetException, ClassNotFoundException {
        final Class<?> clazz = loader.loadClass(className);
        for (final Method method : clazz.getMethods()) {
            if (method.getName().equals(methodName) && doParametersMatch(method.getParameterTypes(), parameters)) {
                return (T) method.invoke(null, parameters);
            }
        }
        throw new NoSuchMethodException("No matching method found");
    }

    /**
     * Check whether a list of parameters matches a list of parameter types. This
     * is used to find matching constructors and (possibly static) methods.
     * 
     * @param types the parameter types
     * @param parameters the parameters
     * @return whether the parameters match the types
     */
    private static boolean doParametersMatch(Class<?>[] types, Object[] parameters) {
        if (types.length != parameters.length)
            return false;
        for (int i = 0; i < types.length; i++)
            if (parameters[i] != null) {
                Class<?> clazz = parameters[i].getClass();
                if (types[i].isPrimitive()) {
                    if (types[i] != Long.TYPE && types[i] != Integer.TYPE && types[i] != Boolean.TYPE)
                        throw new RuntimeException("unsupported primitive type " + clazz);
                    if (types[i] == Long.TYPE && clazz != Long.class)
                        return false;
                    else if (types[i] == Integer.TYPE && clazz != Integer.class)
                        return false;
                    else if (types[i] == Boolean.TYPE && clazz != Boolean.class)
                        return false;
                } else if (!types[i].isAssignableFrom(clazz))
                    return false;
            }
        return true;
    }

    /**
     * Invokes a method of a given object.
     * <p>
     * This method tries to find a method matching the given name and the
     * parameter list. Just like {@link #newInstance(String, Object...)}, this
     * works via reflection to avoid a compile-time dependency on ImageJ2.
     * </p>
     * 
     * @param loader the class loader with which to load the class
     * @param object the object whose method is to be called
     * @param methodName the name of the static method to be called
     * @param parameters the parameters to pass to the static method
     * @return the return value of the method, if any
     * @throws SecurityException
     * @throws NoSuchMethodException
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     * @throws ClassNotFoundException
     */
    @SuppressWarnings("unchecked")
    public static <T> T invoke(final Object object, final String methodName, final Object... parameters)
            throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException,
            InvocationTargetException, ClassNotFoundException {
        final Class<?> clazz = object.getClass();
        for (final Method method : clazz.getMethods()) {
            if (method.getName().equals(methodName) && doParametersMatch(method.getParameterTypes(), parameters)) {
                return (T) method.invoke(object, parameters);
            }
        }
        throw new NoSuchMethodException("No matching method found");
    }
}

Related

  1. invokeDefaultStaticMethod(Class expectedClass, String className, String methodName)
  2. invokeReflectStaticMethod(String methodName, Class cl, Object[] parameter, Class[] args)
  3. invokeStatic(Class clazz, String methodName, Class returnClass, Object... args)
  4. invokeStatic(final Class clazz, final String methodName, final Class[] argTypes, final Object[] args)
  5. invokeStatic(final String className, final String methodName, final I argument)
  6. invokeStatic(String cname, String methodName, ClassLoader cl)
  7. invokeStaticClass(Class staticClass, String methodName, Object[] args, Class... parameterTypes)
  8. invokeStaticMethod(Class objClass, String methodName, Object args[])