Java Reflection Method Signature getMethodWithSignature(Class clazz, String methodName, Class[] params)

Here you can find the source of getMethodWithSignature(Class clazz, String methodName, Class[] params)

Description

get Method With Signature

License

Open Source License

Declaration

public static Method getMethodWithSignature(Class<?> clazz, String methodName, Class<?>[] params) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2006-2010 eBay Inc. All Rights Reserved.
 * 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
 *******************************************************************************/

import java.lang.reflect.Method;

public class Main {
    public static Method getMethodWithSignature(Class<?> clazz, String methodName, Class<?>[] params) {
        Method resultMethod = null;
        Method[] allMethods = clazz.getDeclaredMethods();
        if (params == null) {
            params = new Class[0];
        }//from ww  w  .j  a  v  a  2  s  .co m
        for (Method method : allMethods) {
            if (method.getName().equals(methodName)) {
                Class<?> methodParams[] = method.getParameterTypes();
                if (methodParams.length != params.length) {
                    continue;
                }
                int paramIndex = 0;
                for (Class<?> paramType : methodParams) {
                    if (paramType != params[paramIndex]) {
                        break;
                    }
                    paramIndex++;
                }
                if (methodParams.length == paramIndex) {
                    resultMethod = method;
                }
            }
        }

        return resultMethod;
    }
}

Related

  1. getMethodSignature(Method method)
  2. getMethodSignature(Method method)
  3. getMethodSignature(Method method)
  4. getMethodSignature(Method method)
  5. getMethodSignatureWithLongTypeNames(Method method)