Java Reflection Method Name getMethod(Class clazz, String name, Class... args)

Here you can find the source of getMethod(Class clazz, String name, Class... args)

Description

get Method

License

Open Source License

Declaration

public static Method getMethod(Class<?> clazz, String name, Class<?>... args) 

Method Source Code

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

import java.lang.reflect.Method;

public class Main {
    public static Method getMethod(Class<?> clazz, String name, Class<?>... args) {
        for (Method m : clazz.getDeclaredMethods())
            if (m.getName().equals(name) && (args.length == 0 && m.getParameterTypes().length == 0
                    || ClassListEqual(args, m.getParameterTypes()))) {
                m.setAccessible(true);/*w  w w .j  a v  a2  s .  com*/
                return m;
            }
        for (Method m : clazz.getMethods())
            if (m.getName().equals(name) && (args.length == 0 && m.getParameterTypes().length == 0
                    || ClassListEqual(args, m.getParameterTypes()))) {
                m.setAccessible(true);
                return m;
            }
        return null;
    }

    public static boolean ClassListEqual(Class<?>[] l1, Class<?>[] l2) {
        if (l1.length != l2.length)
            return false;
        for (int i = 0; i < l1.length; i++)
            if (l1[i] != l2[i])
                return false;
        return true;
    }
}

Related

  1. getMethod(Class clazz, String name)
  2. getMethod(Class clazz, String name)
  3. getMethod(Class clazz, String name)
  4. getMethod(Class clazz, String name)
  5. getMethod(Class clazz, String name, boolean declared, Class... args)
  6. getMethod(Class clazz, String name, Class... args)
  7. getMethod(Class clazz, String name, Class... params)
  8. getMethod(Class clazz, String name, int paramlength)
  9. getMethod(Class clazz, String... names)