Java Reflection Method Name getMethods(Class clazz, String name, int args)

Here you can find the source of getMethods(Class clazz, String name, int args)

Description

get Methods

License

LGPL

Declaration

public static Method[] getMethods(Class<?> clazz, String name, int args) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static Method[] getMethods(Class<?> clazz, String name, int args) {
        try {/*from w  w  w  .  j a va2  s  . com*/
            List<Method> list = new ArrayList<Method>();

            for (Method method : clazz.getDeclaredMethods()) {
                if (method.getParameterTypes().length == args) {
                    method.setAccessible(true);
                    list.add(method);
                }
            }

            for (Method method : clazz.getDeclaredMethods())
                if (method.getParameterTypes().length == args)
                    list.add(method);

            return list.toArray(new Method[list.size()]);
        } catch (Throwable error) {
            return null;
        }
    }

    public static Method[] getMethods(Class<?> clazz, String name, boolean declared, int args) {
        try {
            List<Method> list = new ArrayList<Method>();

            if (declared) {
                for (Method method : clazz.getDeclaredMethods()) {
                    if (method.getParameterTypes().length == args) {
                        method.setAccessible(true);
                        list.add(method);
                    }
                }
            } else
                for (Method method : clazz.getDeclaredMethods())
                    if (method.getParameterTypes().length == args)
                        list.add(method);

            return list.toArray(new Method[list.size()]);
        } catch (Throwable error) {
            return null;
        }
    }

    public static Method[] getMethods(Class<?> clazz, String name, Class<?> result) {
        try {
            List<Method> list = new ArrayList<Method>();

            for (Method method : clazz.getDeclaredMethods()) {
                if (method.getReturnType().getName().equals(result.getName())) {
                    method.setAccessible(true);
                    list.add(method);
                }
            }

            for (Method method : clazz.getDeclaredMethods())
                if (method.getReturnType().getName().equals(result.getName()))
                    list.add(method);

            return list.toArray(new Method[list.size()]);
        } catch (Throwable error) {
            return null;
        }
    }

    public static Method[] getMethods(Class<?> clazz, String name, boolean declared, Class<?> result) {
        try {
            List<Method> list = new ArrayList<Method>();

            if (declared) {
                for (Method method : clazz.getDeclaredMethods()) {
                    if (method.getReturnType().getName().equals(result.getName())) {
                        method.setAccessible(true);
                        list.add(method);
                    }
                }
            } else
                for (Method method : clazz.getDeclaredMethods())
                    if (method.getReturnType().getName().equals(result.getName()))
                        list.add(method);

            return list.toArray(new Method[list.size()]);
        } catch (Throwable error) {
            return null;
        }
    }
}

Related

  1. getMethodNames(Class cls)
  2. getMethodNameWithClassName(Method a_Method)
  3. getMethodOnlyByName(Class c, String methodName)
  4. getMethodOrNull(Class cls, String name, Class[] args)
  5. getMethodPropertyName(java.lang.reflect.Method method)
  6. getMethods(Class type, String name)
  7. getMethods(final Class cl, final String name, final int params)
  8. getMethods(final Class clazz, final String methodName)
  9. getMethods(String classname)