Java Reflection Method Get from Object getMethodInstace(Class c, Object inst, String name, Class[] types, boolean access)

Here you can find the source of getMethodInstace(Class c, Object inst, String name, Class[] types, boolean access)

Description

same as callMethod, but only returns a reference

License

Open Source License

Parameter

Parameter Description
c the class (can be null for objects)
inst the instance, null for static context
name the name of the method
types the types required for the method
access true if the method should be made accessible

Return

a reflective instance of the requested method, or null if it does not exist

Declaration

public static Object getMethodInstace(Class<?> c, Object inst, String name, Class<?>[] types, boolean access) 

Method Source Code

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

import java.lang.reflect.Method;

public class Main {
    /**//from  www .  j ava 2 s.  com
     * same as callMethod, but only returns a reference
     * @param c the class (can be null for objects)
     * @param inst the instance, null for static context
     * @param name the name of the method
     * @param types the types required for the method
     * @param access true if the method should be made accessible
     * @return a reflective instance of the requested method, or null if it does not exist
     */
    public static Object getMethodInstace(Class<?> c, Object inst, String name, Class<?>[] types, boolean access) {
        if (c == null)
            c = inst.getClass();
        try {
            Method method = null;
            while (method == null) {
                try {
                    method = c.getDeclaredMethod(name, types);
                } catch (NoSuchMethodException e) {
                    c = c.getSuperclass();
                    if (c == null)
                        throw new NoSuchMethodException(name);
                }
            }
            if (access)
                method.setAccessible(true);
            return method;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}

Related

  1. getMethodDescriptor(Object instance, String methodName, Class aClass, Class... parameters)
  2. getMethodFirstParamType(final String methodName, final Object o)
  3. getMethodIfAny(final Object instance, final String name, final Class[] params)
  4. getMethodIgnoreCaseWithNoParams(Object o, String p)
  5. getMethodInHolder(String methodName, Object holder)
  6. getMethodName(AccessibleObject method)
  7. getMethodNamed(String methodName, Object holder)
  8. getMethodNames(Object obj, boolean hasParent)
  9. getMethodNames(Object obj, boolean includeInheritedMethods)