Java Reflection Method Name getMethod(Class clazz, String name)

Here you can find the source of getMethod(Class clazz, String name)

Description

Get the first method corresponding with name parameter in the clazz class.

License

Open Source License

Parameter

Parameter Description
clazz The class to browse.
name The method name to search.

Return

The first found method which name is name otherwise null.

Declaration

public static Method getMethod(Class<?> clazz, String name) 

Method Source Code

//package com.java2s;
/*// w ww.  java  2  s . c  o  m
 * #%L
 * Webmotion server
 * 
 * $Id$
 * $HeadURL$
 * %%
 * Copyright (C) 2011 - 2015 Debux
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as 
 * published by the Free Software Foundation, either version 3 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public 
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
 * #L%
 */

import java.lang.reflect.Method;

public class Main {
    /**
     * Get the first method corresponding with <code>name</code> parameter in the <code>clazz</code> class.
     * @param clazz The class to browse.
     * @param name The method name to search.
     * @return The first found method which name is <code>name</code> otherwise null.
     */
    public static Method getMethod(Class<?> clazz, String name) {
        Method[] all = clazz.getMethods();

        for (Method method : all) {
            String methodName = method.getName();
            if (methodName.equals(name)) {
                return method;
            }
        }

        return null;
    }
}

Related

  1. getMethod(Class clazz, String methodName, Class... params)
  2. getMethod(Class clazz, String methodName, Class... params)
  3. getMethod(Class clazz, String methodName, final Class[] classes)
  4. getMethod(Class clazz, String methodName, int numParams)
  5. getMethod(Class clazz, String name)
  6. getMethod(Class clazz, String name)
  7. getMethod(Class clazz, String name)
  8. getMethod(Class clazz, String name)
  9. getMethod(Class clazz, String name, boolean declared, Class... args)