Java Reflection Method Name getMethodByName(Class clazz, String methodName)

Here you can find the source of getMethodByName(Class clazz, String methodName)

Description

get Method By Name

License

Apache License

Declaration

private static Method getMethodByName(Class<?> clazz, String methodName) 

Method Source Code

//package com.java2s;
/*/* w  w w.  ja va2  s  .  co m*/
 * Copyright 2012 Ryan W Tenney (http://ryan.10e.us)
 *            and Martello Technologies (http://martellotech.com)
 *
 * 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;
import java.util.ArrayList;
import java.util.List;

public class Main {
    private static Method getMethodByName(Class<?> clazz, String methodName) {
        List<Method> methodsFound = new ArrayList<Method>();
        for (Method method : clazz.getMethods()) {
            if (method.getName().equals(methodName)) {
                methodsFound.add(method);
            }
        }
        if (methodsFound.size() == 1) {
            return methodsFound.get(0);
        } else {
            throw new RuntimeException("No unique method " + methodName + " found on class " + clazz.getName());
        }
    }
}

Related

  1. getMethod(String strMethodPrefix, A instance, String strAttributeName, Class clazz)
  2. getMethodAsAccessible(String methodName, Class clazz)
  3. getMethodByName(Class clazz, String methodName)
  4. getMethodByName(Class aClass, String methodName, Class... params)
  5. getMethodByName(Class clazz, String methodName)
  6. getMethodByName(Class clazz, String name)
  7. getMethodByName(Class cls, String methodName)
  8. getMethodByName(Class type, String methodName)
  9. getMethodByName(final Class cls, final String action)