Java Reflection Method Get from Object getMethodReturn(String className, String methodName, Class[] params, Object[] args, boolean isStatic)

Here you can find the source of getMethodReturn(String className, String methodName, Class[] params, Object[] args, boolean isStatic)

Description

get Method Return

License

Open Source License

Parameter

Parameter Description
className a parameter
methodName a parameter
params a parameter
args a parameter
isStatic a parameter

Exception

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

Return

Returns the Method object from the specified class name, method name, parameters of this method and if this method is static.

Declaration

public static Object getMethodReturn(String className, String methodName, Class<?>[] params, Object[] args,
        boolean isStatic) throws ClassNotFoundException, NoSuchMethodException, SecurityException,
        InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    /**//from   ww w .j a va  2 s  . c o  m
     * 
     * @param className
     * @param methodName
     * @param params
     * @param args
     * @param isStatic
     * 
     * @return Returns the Method object from the specified class name, method name, parameters of this method and if this method is static.
     * 
     * @throws ClassNotFoundException
     * @throws NoSuchMethodException
     * @throws SecurityException
     * @throws InstantiationException
     * @throws IllegalAccessException
     * @throws IllegalArgumentException
     * @throws InvocationTargetException
     */
    public static Object getMethodReturn(String className, String methodName, Class<?>[] params, Object[] args,
            boolean isStatic) throws ClassNotFoundException, NoSuchMethodException, SecurityException,
            InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        Class<?> clazz = Class.forName(className);
        Object instance = clazz.newInstance();
        Method method = clazz.getMethod(methodName, params);
        if (isStatic) {
            return method.invoke(null, args);
        } else {
            return method.invoke(instance, args);
        }
    }

    /**
     * 
     * @param className
     * @param methodName
     * @param params
     * 
     * @return Returns the Method object from the specified class name, method name and parameters of this method.
     * 
     * @throws ClassNotFoundException
     * @throws NoSuchMethodException
     * @throws SecurityException
     */
    public static Method getMethod(String className, String methodName, Class<?>[] params)
            throws ClassNotFoundException, NoSuchMethodException, SecurityException {
        Class<?> clazz = Class.forName(className);
        return clazz.getMethod(methodName, params);
    }
}

Related

  1. getMethodNames(Object obj, boolean hasParent)
  2. getMethodNames(Object obj, boolean includeInheritedMethods)
  3. getMethodObject(Class type, Class clazz, String method, Class[] args, Object object, Object[] objects)
  4. getMethodObject(Object object, String method, Object[] parametre)
  5. getMethodResult(final Object element, final String methodName)
  6. getMethodReturnTypeGeneric(Object source, Method method)
  7. getMethods(Class objectClass, Class annotationClass)
  8. getMethods(Object instance, String name, Class[] arguments, boolean isPrefix)
  9. getMethods(Object obj, boolean hasParent)