Java Reflection Method Get from Object getMethod(Class clz, String methodName, Object... params)

Here you can find the source of getMethod(Class clz, String methodName, Object... params)

Description

Get a non-public (i.e.

License

Apache License

Parameter

Parameter Description
clz The class.
methodName A static method name.
params Optional parameters.

Exception

Parameter Description
NoSuchMethodException an exception
SecurityException an exception

Return

A Method object.

Declaration

public static Method getMethod(Class<?> clz, String methodName, Object... params)
        throws NoSuchMethodException, SecurityException 

Method Source Code

//package com.java2s;
/*   Copyright (c) 2015 Magnet Systems, Inc.
 *
 *  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.// w w  w .  j a  va2  s  .c om
 */

import java.lang.reflect.Method;

public class Main {
    /**
     * Get a non-public (i.e. declared) static method from a class.
     * @param clz The class.
     * @param methodName A static method name.
     * @param params Optional parameters.
     * @return A Method object.
     * @throws NoSuchMethodException
     * @throws SecurityException
     */
    public static Method getMethod(Class<?> clz, String methodName, Object... params)
            throws NoSuchMethodException, SecurityException {
        if (params == null) {
            return clz.getMethod(methodName);
        } else {
            int i = 0;
            Class<?>[] paramClasses = new Class<?>[params.length];
            for (Object param : params) {
                paramClasses[i++] = param.getClass();
            }
            return clz.getDeclaredMethod(methodName, paramClasses);
        }
    }

    /**
     * Get a non-public (i.e. declared) method from an object.
     * @param obj The object to be used.
     * @param methodName A method name.
     * @param params Optional parameters.
     * @return A Method object.
     * @throws NoSuchMethodException
     * @throws SecurityException
     */
    public static Method getMethod(Object obj, String methodName, Object... params)
            throws NoSuchMethodException, SecurityException {
        if (params == null) {
            return obj.getClass().getMethod(methodName);
        } else {
            int i = 0;
            Class<?>[] paramClasses = new Class<?>[params.length];
            for (Object param : params) {
                paramClasses[i++] = param.getClass();
            }
            return obj.getClass().getDeclaredMethod(methodName, paramClasses);
        }
    }

    /**
     * Get a non-public (i.e. declared) static method from a class.
     * @param clz The class.
     * @param methodName A method name.
     * @param paramClasses Classes of the optional parameters.
     * @return A Method object.
     * @throws NoSuchMethodException
     * @throws SecurityException
     */
    public static Method getMethod(Class<?> clz, String methodName, Class<?>... paramClasses)
            throws NoSuchMethodException, SecurityException {
        if (paramClasses == null) {
            return clz.getMethod(methodName);
        } else {
            return clz.getDeclaredMethod(methodName, paramClasses);
        }
    }

    /**
     * Get a non-public (i.e. declared) method from an object.
     * @param obj The object to be used.
     * @param methodName A method name.
     * @param paramClasses Classes of the optional parameters.
     * @return A Method object.
     * @throws NoSuchMethodException
     * @throws SecurityException
     */
    public static Method getMethod(Object obj, String methodName, Class<?>... paramClasses)
            throws NoSuchMethodException, SecurityException {
        if (paramClasses == null) {
            return obj.getClass().getMethod(methodName);
        } else {
            return obj.getClass().getDeclaredMethod(methodName, paramClasses);
        }
    }
}

Related

  1. getMethod(Class objectClass, String methodName, String argumentType)
  2. getMethod(Class type, String name, Object[] args)
  3. getMethod(Class clz, String methodName, Class[] methodArgs)
  4. getMethod(Class type, String name, Class... parameterTypes)
  5. getMethod(Class cls, String method, Object[] params)
  6. getMethod(Class klass, String methodName, Object... params)
  7. getMethod(Class objectType, String methodName, Class... parameterTypes)
  8. getMethod(Class type, String name, Object[] args)
  9. getMethod(final Class clazz, final String methodName, final Class[] parTypes, final Object[] parameters)