Java Reflection Method Parameter getMethod(final Class javaClass, final String methodName, final Class[] methodParameterTypes, final boolean shouldSetAccessible)

Here you can find the source of getMethod(final Class javaClass, final String methodName, final Class[] methodParameterTypes, final boolean shouldSetAccessible)

Description

Get a method declared in the given class.

License

Open Source License

Parameter

Parameter Description
javaClass The class to get the method from
methodName The name of the method to get
methodParameterTypes A list of classes representing the classes of the parameters of the mthod
shouldSetAccessible whether or not to call the setAccessible API

Exception

Parameter Description

Declaration

public static Method getMethod(final Class javaClass, final String methodName,
        final Class[] methodParameterTypes, final boolean shouldSetAccessible) throws NoSuchMethodException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 1998, 2015 Oracle and/or its affiliates. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0
 * which accompanies this distribution.// w  w w . jav a  2  s .  c  o m
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * Contributors:
 *     Oracle - initial API and implementation from Oracle TopLink
 *     08/23/2010-2.2 Michael O'Brien 
 *        - 323043: application.xml module ordering may cause weaving not to occur causing an NPE.
 *                       warn if expected "_persistence_*_vh" method not found
 *                       instead of throwing NPE during deploy validation.
 *     30/05/2012-2.4 Guy Pelletier
 *       - 354678: Temp classloader is still being used during metadata processing
 *
 ******************************************************************************/

import java.lang.reflect.Method;

public class Main {
    /**
     * Get a method declared in the given class. Wrap the call in doPrivileged 
     * if necessary. This call will traverse the superclasses. The 
     * shouldSetAccessible parameter allows the the setAccessible API to be 
     * called as well. This option was added to avoid making multiple 
     * doPrivileged calls within MethodBasedAttributeAccessor.
     * @param javaClass The class to get the method from
     * @param methodName The name of the method to get
     * @param methodParameterTypes A list of classes representing the classes of the parameters of the mthod
     * @param shouldSetAccessible whether or not to call the setAccessible API
     * @throws java.lang.NoSuchMethodException
     */
    public static Method getMethod(final Class javaClass, final String methodName,
            final Class[] methodParameterTypes, final boolean shouldSetAccessible) throws NoSuchMethodException {
        Method method = findMethod(javaClass, methodName, methodParameterTypes);
        if (shouldSetAccessible) {
            method.setAccessible(true);
        }
        return method;
    }

    /**
     * Finding a method within a class potentially has to navigate through it's superclasses to eventually
     * find the method.  This method is called by the public getDeclaredMethod() method and does a recursive
     * search for the named method in the given classes or it's superclasses.
     */
    private static Method findMethod(Class javaClass, String methodName, Class[] methodParameterTypes)
            throws NoSuchMethodException {
        try {
            // use a combination of getDeclaredMethod() and recursion to ensure we get the non-public methods
            // getMethod will not help because it returns only public methods
            return javaClass.getDeclaredMethod(methodName, methodParameterTypes);
        } catch (NoSuchMethodException ex) {
            Class superclass = javaClass.getSuperclass();
            if (superclass == null) {
                throw ex;
            } else {
                try {
                    return findMethod(superclass, methodName, methodParameterTypes);
                } catch (NoSuchMethodException lastEx) {
                    throw ex;
                }
            }
        }
    }

    /**
      * Return a method on a given class with the given method name and parameter 
      * types. This call will NOT traverse the superclasses. Wrap the call in 
      * doPrivileged if necessary.
      * @param method the class to get the method from
      * @param methodName the name of the method to get
      * @param methodParameters a list of classes representing the classes of the
      *  parameters of the method.
      */
    public static Method getDeclaredMethod(final Class clazz, final String methodName,
            final Class[] methodParameterTypes) throws NoSuchMethodException {
        return clazz.getDeclaredMethod(methodName, methodParameterTypes);
    }
}

Related

  1. getMethod(Class type, String name, Class... parametersType)
  2. getMethod(Class type, String name, Class... parameterTypes)
  3. getMethod(Class type, String name, Class... parameterTypes)
  4. getMethod(Class clazz, String methodName, Class[] parameterTypes)
  5. getMethod(final Class clazz, final String name, final Class... parameterTypes)
  6. getMethod(final Class aClass, final String methodName, Class[] parameterTypes)
  7. getMethod(final Class clazz, final String methodName, final Class... parameterTypes)
  8. getMethod(final Class clazz, final String name, final Class... parametertypes)
  9. getMethod(final Class clazz, final String name, final Class... parameterTypes)