Java Reflection Method Get from Object getMethod(final Object target, final String methodName, final Class... argumentTypes)

Here you can find the source of getMethod(final Object target, final String methodName, final Class... argumentTypes)

Description

Returns a the specified method of the target object, or null if the method does not exist.

License

Apache License

Parameter

Parameter Description
target Target object to check
methodName Name of the method being queried
argumentTypes Types of the method's arguments

Return

Method instance if the method exists, false otherwise.

Declaration

public static synchronized Method getMethod(final Object target, final String methodName,
        final Class... argumentTypes) 

Method Source Code

//package com.java2s;
/**/*from   w  w  w  .jav a2s .co m*/
 * A collection of small utilities for component management and reflection handling.
 * <p/>
 * <hr/> Copyright 2006-2012 Torsten Heup
 * <p/>
 * 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
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * 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.
 */

import java.lang.reflect.Method;

public class Main {
    /**
     * Returns a the specified method of the target object, or null if the method does not exist.  This is merely a
     * conveniance method to avoid reflection's exceptions.
     *
     * @param target        Target object to check
     * @param methodName    Name of the method being queried
     * @param argumentTypes Types of the method's arguments
     * @return Method instance if the method exists, false otherwise.
     */
    public static synchronized Method getMethod(final Object target, final String methodName,
            final Class... argumentTypes) {
        return getMethod(target.getClass(), methodName, argumentTypes);
    }

    /**
     * Attempts to find a method with the given name and argument types on the specified class. Other than Class's
     * getMethod(String, Class...) method, this method will also return protected and private methods.
     *
     * @param clazz         Class supposed to offer the method being searched.
     * @param methodName    Name of the method.
     * @param argumentTypes The arguments found in the method signature, if any.
     * @return a method with the given name and argument types on the specified class, if any.
     */
    public static synchronized Method getMethod(final Class clazz, final String methodName,
            final Class... argumentTypes) {
        try {
            return clazz.getMethod(methodName, argumentTypes);
        } catch (NoSuchMethodException e) {
            /* Ok, so there's no public method... */
        }

        Class runner = clazz;
        while (runner != null) {
            try {
                return runner.getDeclaredMethod(methodName, argumentTypes);
            } catch (NoSuchMethodException e) {
                /* No luck here either */
            }
            runner = runner.getSuperclass();
        }

        /* Still no luck, means there is no suitable method */
        return null;
    }
}

Related

  1. getMethod(final Class clazz, final String methodName, final Class[] parTypes, final Object[] parameters)
  2. getMethod(final Object object, final String methodName, final Class... parameterClass)
  3. getMethod(final Object object, final String methodName, final Object... arguments)
  4. getMethod(final Object object, final String methodName, final Object... arguments)
  5. getMethod(final Object object, String methodName, final Class[] argTypes)
  6. getMethod(final String methodName, final Object obj, final Class... argTypes)
  7. getMethod(Object bean, String propertyName)
  8. getMethod(Object instance, String methodName, Class... argsClass)
  9. getMethod(Object instance, String methodName, Class[] parameters)