Java Reflection Method Static Invoke invokeStaticMethod(Class pClass, String pMethod, Object pParam)

Here you can find the source of invokeStaticMethod(Class pClass, String pMethod, Object pParam)

Description

Gets an object from any given static method, with the given parameter.

License

Open Source License

Parameter

Parameter Description
pClass The class to invoke method on
pMethod The name of the method to invoke
pParam The parameter to the method

Exception

Parameter Description
InvocationTargetException if the invocation failed

Return

The object returned by the static method. If the return type of the method is a primitive type, it is wrapped in the corresponding wrapper object (int is wrapped in an Integer). If the return type of the method is void, null is returned. If the method could not be invoked for any reason, null is returned.

Declaration



public static Object invokeStaticMethod(Class<?> pClass, String pMethod, Object pParam)
        throws InvocationTargetException 

Method Source Code


//package com.java2s;

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

public class Main {
    /**/*from  ww  w  .j  a v  a  2s.co m*/
     * Gets an object from any given static method, with the given parameter.
     *
     * @param pClass  The class to invoke method on
     * @param pMethod The name of the method to invoke
     * @param pParam  The parameter to the method
     *
     * @return The object returned by the static method.
     *         If the return type of the method is a primitive type, it is wrapped in
     *         the corresponding wrapper object (int is wrapped in an Integer).
     *         If the return type of the method is void, null is returned.
     *         If the method could not be invoked for any reason, null is returned.
     *
     * @throws InvocationTargetException if the invocation failed
     */
    // TODO: Move to ReflectUtil
    // TODO: Rename to invokeStatic?
    public static Object invokeStaticMethod(Class<?> pClass, String pMethod, Object pParam)
            throws InvocationTargetException {

        return invokeStaticMethod(pClass, pMethod, new Object[] { pParam });
    }

    /**
     * Gets an object from any given static method, with the given parameter.
     *
     * @param pClass  The class to invoke method on
     * @param pMethod The name of the method to invoke
     * @param pParams The parameters to the method
     *
     * @return The object returned by the static method.
     *         If the return type of the method is a primitive type, it is wrapped in
     *         the corresponding wrapper object (int is wrapped in an Integer).
     *         If the return type of the method is void, null is returned.
     *         If the method could not be invoked for any reason, null is returned.
     *
     * @throws InvocationTargetException if the invocation failed
     */
    // TODO: Move to ReflectUtil
    // TODO: Rename to invokeStatic?
    public static Object invokeStaticMethod(Class<?> pClass, String pMethod, Object... pParams)
            throws InvocationTargetException {

        Object value = null;

        try {
            // Create param and argument arrays
            Class[] paramTypes = new Class[pParams.length];
            for (int i = 0; i < pParams.length; i++) {
                paramTypes[i] = pParams[i].getClass();
            }

            // Get method
            // *** If more than one such method is found in the class, and one
            // of these methods has a RETURN TYPE that is more specific than
            // any of the others, that method is reflected; otherwise one of
            // the methods is chosen ARBITRARILY.
            // java/lang/Class.html#getMethod(java.lang.String, java.lang.Class[])
            Method method = pClass.getMethod(pMethod, paramTypes);

            // Invoke public static method
            if (Modifier.isPublic(method.getModifiers()) && Modifier.isStatic(method.getModifiers())) {
                value = method.invoke(null, pParams);
            }

        }
        /* All this to let InvocationTargetException pass on */
        catch (NoSuchMethodException nsme) {
            return null;
        } catch (IllegalAccessException iae) {
            return null;
        } catch (IllegalArgumentException iarge) {
            return null;
        }

        return value;
    }
}

Related

  1. invokeStaticMethod(Class cls, String method, Class[] params, Object... args)
  2. invokeStaticMethod(Class cls, String methodName, Object... args)
  3. invokeStaticMethod(Class cls, String name)
  4. invokeStaticMethod(Class klass, String methodName)
  5. invokeStaticMethod(Class objectType, String name, Class paramTypes[], Object args[])
  6. invokeStaticMethod(final Class cls, final String methodName)
  7. invokeStaticMethod(final Class cls, final String methodName, final boolean throwException)
  8. invokeStaticMethod(final Class clazz, final String methodName, final Class[] parameterTypes, final Object[] arglist)
  9. invokeStaticMethod(final Method method, final Object... args)