Java Reflection Method Name getMethod(@Nonnull Class clazz, @Nonnull String methodName, @Nonnull Class... types)

Here you can find the source of getMethod(@Nonnull Class clazz, @Nonnull String methodName, @Nonnull Class... types)

Description

get Method

License

Open Source License

Declaration

private static Method getMethod(@Nonnull Class clazz,
            @Nonnull String methodName, @Nonnull Class... types) 

Method Source Code

//package com.java2s;
/*/*from   w ww  .j av  a 2 s  . com*/
 * The MIT License
 *
 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

import javax.annotation.Nonnull;

public class Main {
    private static Method getMethod(@Nonnull Class clazz,
            @Nonnull String methodName, @Nonnull Class... types) {
        Method res = null;
        try {
            res = clazz.getDeclaredMethod(methodName, types);
            // private, static or final methods can not be overridden
            if (res != null
                    && (Modifier.isPrivate(res.getModifiers())
                            || Modifier.isFinal(res.getModifiers()) || Modifier
                                .isStatic(res.getModifiers()))) {
                res = null;
            }
        } catch (NoSuchMethodException e) {
            // Method not found in clazz, let's search in superclasses
            Class superclass = clazz.getSuperclass();
            if (superclass != null) {
                res = getMethod(superclass, methodName, types);
            }
        } catch (SecurityException e) {
            throw new AssertionError(e);
        }
        if (res == null) {
            throw new IllegalArgumentException(
                    String.format(
                            "Method %s not found in %s (or it is private, final or static)",
                            methodName, clazz.getName()));
        }
        return res;
    }
}

Related

  1. getMethod(Class c, String methodName)
  2. getMethod(Class c, String name)
  3. getMethod(Class cl, String methodName, Class[] paramTypes)
  4. getMethod(Class clazz, String methodName)