Java Reflection Method Name getMethod(Class clazz, String methodName, Class[] params)

Here you can find the source of getMethod(Class clazz, String methodName, Class[] params)

Description

Determines the method with the specified signature via reflection look-up.

License

Apache License

Parameter

Parameter Description
clazz The java class to search in
methodName The method's name
params The parameter types

Return

The method object or null if no matching method was found

Declaration

public static Method getMethod(Class clazz, String methodName, Class[] params) 

Method Source Code

//package com.java2s;
/* Copyright 2002-2005 The Apache Software Foundation
 *
 * 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./*  ww w. j ava2 s  .  co m*/
 */

import java.lang.reflect.Method;

public class Main {
    /** The class loader currently used by OJB */
    private static ClassLoader _classLoader = null;

    /**
     * Determines the method with the specified signature via reflection look-up.
     * 
     * @param clazz      The java class to search in
     * @param methodName The method's name
     * @param params     The parameter types
     * @return The method object or <code>null</code> if no matching method was found
     */
    public static Method getMethod(Class clazz, String methodName, Class[] params) {
        try {
            return clazz.getMethod(methodName, params);
        } catch (Exception ignored) {
        }
        return null;
    }

    /**
     * Determines the method with the specified signature via reflection look-up.
     * 
     * @param object     The instance whose class is searched for the method
     * @param methodName The method's name
     * @param params     The parameter types
     * @return A method object or <code>null</code> if no matching method was found
     */
    public static Method getMethod(Object object, String methodName, Class[] params) {
        return getMethod(object.getClass(), methodName, params);
    }

    /**
     * Determines the method with the specified signature via reflection look-up.
     * 
     * @param className  The qualified name of the searched class
     * @param methodName The method's name
     * @param params     The parameter types
     * @return A method object or <code>null</code> if no matching method was found
     */
    public static Method getMethod(String className, String methodName, Class[] params) {
        try {
            return getMethod(getClass(className, false), methodName, params);
        } catch (Exception ignored) {
        }
        return null;
    }

    /**
     * Retrieves the class object for the given qualified class name.
     * 
     * @param className  The qualified name of the class
     * @param initialize Whether the class shall be initialized
     * @return The class object
     */
    public static Class getClass(String className, boolean initialize) throws ClassNotFoundException {
        return Class.forName(className, initialize, getClassLoader());
    }

    /**
     * Convenience method for {@link #getClass(String, boolean) getClass(name, true)}
     * 
     * @param name The qualified class name
     * @return The class object
     */
    public static Class getClass(String name) throws ClassNotFoundException {
        return getClass(name, true);
    }

    /**
     * Returns the class loader currently used by OJB. Defaults to the class loader of
     * the current thread (<code>Thread.currentThread().getContextClassLoader()</code>)
     * if not set differently. If class loader is not explicitly set and the loader for
     * the current thread context is null, the JVM default class loader will be used.
     * 
     * @return The classloader used by OJB
     * @see #setClassLoader(ClassLoader)
     */
    public static ClassLoader getClassLoader() {
        final ClassLoader ojbClassLoader;
        if (_classLoader != null) {
            ojbClassLoader = _classLoader;
        } else {
            final ClassLoader threadCtxtClassLoader;
            threadCtxtClassLoader = Thread.currentThread().getContextClassLoader();
            if (threadCtxtClassLoader == null) {
                // mkalen: happens only in "obscure" situations using JNI, revert to system CL
                ojbClassLoader = ClassLoader.getSystemClassLoader();
            } else {
                ojbClassLoader = threadCtxtClassLoader;
            }
        }
        return ojbClassLoader;
    }
}

Related

  1. getMethod(Class c, String name)
  2. getMethod(Class cl, String methodName, Class[] paramTypes)
  3. getMethod(Class clazz, String methodName)
  4. getMethod(Class clazz, String methodName, Class argType)
  5. getMethod(Class clazz, String methodName, Class[] classes)
  6. getMethod(Class clazz, String name)
  7. getMethod(Class clazz, String name, Class... args)
  8. getMethod(Class cls, String methodName, Class[] params)
  9. getMethod(Class clz, String methodName, Class expectedTypes[])