Java Reflection Method Name getMethod(int requireMod, int bannedMod, Class clazz, String methodName, Class... params)

Here you can find the source of getMethod(int requireMod, int bannedMod, Class clazz, String methodName, Class... params)

Description

Search for the first publically and privately defined method of the given name and parameter count.

License

MIT License

Parameter

Parameter Description
requireMod - modifiers that are required.
bannedMod - modifiers that are banned.
clazz - a class to start with.
methodName - the method name, or NULL to skip.
params - the expected parameters.

Exception

Parameter Description
IllegalStateException If we cannot find this method.

Return

The first method by this name.

Declaration

private static Method getMethod(int requireMod, int bannedMod, Class<?> clazz, String methodName,
        Class<?>... params) 

Method Source Code

//package com.java2s;
/*/*from  w  w  w .ja  va 2  s. c  om*/
 * This file is part of UltimateCore, licensed under the MIT License (MIT).
 *
 * Copyright (c) Bammerbom
 *
 * 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.util.*;

public class Main {
    /**
     * Search for the first publically and privately defined method of the given
     * name and parameter count.
     *
     * @param requireMod - modifiers that are required.
     * @param bannedMod - modifiers that are banned.
     * @param clazz - a class to start with.
     * @param methodName - the method name, or NULL to skip.
     * @param params - the expected parameters.
     * @return The first method by this name.
     * @throws IllegalStateException If we cannot find this method.
     */
    private static Method getMethod(int requireMod, int bannedMod, Class<?> clazz, String methodName,
            Class<?>... params) {
        for (Method method : clazz.getDeclaredMethods()) {
            // Limitation: Doesn't handle overloads
            if ((method.getModifiers() & requireMod) == requireMod && (method.getModifiers() & bannedMod) == 0
                    && (methodName == null || method.getName().equals(methodName))
                    && Arrays.equals(method.getParameterTypes(), params)) {

                method.setAccessible(true);
                return method;
            }
        }
        // Search in every superclass
        if (clazz.getSuperclass() != null) {
            return getMethod(requireMod, bannedMod, clazz.getSuperclass(), methodName, params);
        }
        throw new IllegalStateException(
                String.format("Unable to find method %s (%s).", methodName, Arrays.asList(params)));
    }
}

Related

  1. getMethod(Class clazz, String methodName, Class[] calledTypes)
  2. getMethod(Class type, String methodName, Class... params)
  3. getMethod(final Class atClass, final String name, final Class[] paramType)
  4. getMethod(final Class clazz, final String methodName, final boolean factoryMethod, final boolean failIfNotFound)
  5. getMethod(final String methodName, final Class objClass, final Class[] paramClasses)
  6. getMethod(Method[] methods, String name)
  7. getMethod(Method[] methods, String name, Class[] params)
  8. getMethod(String className)
  9. getMethod(String className, String functionName, Class[] paramTypes)