Java Reflection Method Return getMethod(Class clazz, String name, String returnType, String[] args)

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

Description

get Method

License

Apache License

Declaration

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

Method Source Code

//package com.java2s;
/* Copyright Francesco Andreuzzi

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License. */

import java.lang.reflect.Method;

public class Main {
    public static Method getMethod(Class<?> clazz, String name,
            String returnType, String[] args) {
        Method[] methods = clazz.getDeclaredMethods();

        MainLoop: for (Method method : methods) {

            if (name != null && !name.equals(method.getName())) {
                continue;
            }//from   w w w. jav a2 s .  c  o  m

            if (returnType != null
                    && !returnType.equals(method.getReturnType().getName())) {
                continue;
            }

            if (args != null) {
                Class<?>[] args2 = method.getParameterTypes();
                if (args2.length != args.length) {
                    continue;
                }

                for (int count = 0; count < args.length; count++) {
                    if (!(args[count].equals(args2[count].getName()))) {
                        continue MainLoop;
                    }
                }
            }

            return method;
        }

        return null;
    }

    public static Method getMethod(Class<?> clazz, String name) {
        return getMethod(clazz, name, null, null);
    }

    public static Method getMethod(Class<?> clazz, Class<?> returnType) {
        return getMethod(clazz, null, returnType.getName(), null);
    }

    public static Method getMethod(Class<?> clazz, Class<?>[] args) {
        String[] params = new String[args.length];
        for (int count = 0; count < params.length; count++) {
            params[count] = args[count].getName();
        }
        return getMethod(clazz, null, null, params);
    }
}

Related

  1. getMethod(Class clazz, String methodName, Class returnType, Class... inputParams)
  2. getMethod(Class parentClass, Class returnType, String methodName, Class... types)
  3. getMethod(Class type, String name, Class returnType, Class paramType, boolean caseSensitive)
  4. getMethodByReturnType(final Class source, final Class type)
  5. getMethodByTypes(Class clazz, String name, Class returnType, Class... parameterTypes)