Java Reflection Method Name getMethodIfAvailable(Class clazz, String methodName, Class... paramTypes)

Here you can find the source of getMethodIfAvailable(Class clazz, String methodName, Class... paramTypes)

Description

Determine whether the given class has a method with the given signature, and return it if available (else return null).

License

Open Source License

Parameter

Parameter Description
clazz the clazz to analyze
methodName the name of the method
paramTypes the parameter types of the method

Return

the method, or null if not found

Declaration

public static Method getMethodIfAvailable(Class<?> clazz, String methodName, Class<?>... paramTypes) 

Method Source Code

//package com.java2s;

import java.lang.reflect.Method;

public class Main {
    /**/*from  ww w . j  a  va  2 s.  com*/
     * Determine whether the given class has a method with the given signature,
     * and return it if available (else return <code>null</code>).
     * <p>Essentially translates <code>NoSuchMethodException</code> to <code>null</code>.
     * @param clazz the clazz to analyze
     * @param methodName the name of the method
     * @param paramTypes the parameter types of the method
     * @return the method, or <code>null</code> if not found
     * @see java.lang.Class#getMethod
     */
    public static Method getMethodIfAvailable(Class<?> clazz, String methodName, Class<?>... paramTypes) {
        if (clazz == null) {
            throw new IllegalArgumentException("Class must not be null");
        }
        if (methodName == null) {
            throw new IllegalArgumentException("Method name must not be null");
        }
        try {
            return clazz.getMethod(methodName, paramTypes);
        } catch (NoSuchMethodException ex) {
            return null;
        }
    }
}

Related

  1. getMethodExceptionType(Class cls, String methodName, Class[] argTypes, int methodPosition, int classPosition, Class defaultType)
  2. getMethodFromClass(Class cls, String methodName, Class argClass, boolean onlyProtectedAndHigher)
  3. getMethodFromClassHierarchy(Class clazz, String methodName)
  4. getMethodFromClassWithInheritance(Class cls, String methodName)
  5. getMethodFullName(Method method)
  6. getMethodIfAvailable(Class clazz, String methodName, Class... paramTypes)
  7. getMethodIfAvailable(Class clazz, String methodName, Class... paramTypes)
  8. getMethodIfAvailable(Class clazz, String methodName, Class... paramTypes)
  9. getMethodIncludingSuperClass(Class clazz, String methodName)