Java Reflection Method Name getMethodOnlyByName(Class c, String methodName)

Here you can find the source of getMethodOnlyByName(Class c, String methodName)

Description

Searches in the given class for the given method name.

License

Apache License

Parameter

Parameter Description
c Class to search
methodName Methodname to search

Exception

Parameter Description
NoSuchMethodException If the search method isn't shown

Return

The found method

Declaration

public static Method getMethodOnlyByName(Class<?> c, String methodName) throws NoSuchMethodException 

Method Source Code


//package com.java2s;
// SMSLib is distributed under the terms of the Apache License version 2.0

import java.lang.reflect.Method;

public class Main {
    /**//  www  . jav  a 2  s .com
     * Searches in the given class for the given method name. The argument list
     * is ignored. Overload methods shouldn't used with it - You can't be sure
     * which method you will get!
     * 
     * @param c
     *            Class to search
     * @param methodName
     *            Methodname to search
     * @return The found method
     * @throws NoSuchMethodException
     *             If the search method isn't shown
     */
    public static Method getMethodOnlyByName(Class<?> c, String methodName) throws NoSuchMethodException {
        Method method = null;
        for (Method m : c.getMethods()) {
            if (m.getName().equals(methodName)) {
                method = m;
                break;
            }
        }
        if (method == null) {
            throw new NoSuchMethodException(methodName);
        }
        return method;
    }
}

Related

  1. getMethodNameList(Class theClass)
  2. getMethodNameMinusGet(Method aMethod)
  3. getMethodNames(Class cls, boolean insertDefaultValues)
  4. getMethodNames(Class cls)
  5. getMethodNameWithClassName(Method a_Method)
  6. getMethodOrNull(Class cls, String name, Class[] args)
  7. getMethodPropertyName(java.lang.reflect.Method method)
  8. getMethods(Class clazz, String name, int args)
  9. getMethods(Class type, String name)