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 public method with the given signature, and return it if available (else return null ).

License

Apache License

Parameter

Parameter Description
clazz the clazz to analyze
methodName the name of the method
paramTypes the parameter types of the method (may be null to indicate any signature)

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;
//License from project: Apache License 

import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Set;

public class Main {
    /**//from   w w  w.j a  v  a 2 s. c  o m
     * Determine whether the given class has a public method with the given
     * signature, and return it if available (else return {@code null}).
     * <p>
     * In case of any signature specified, only returns the method if there is a
     * unique candidate, i.e. a single public method with the specified name.
     * <p>
     * Essentially translates {@code NoSuchMethodException} to {@code null}.
     * 
     * @param clazz
     *            the clazz to analyze
     * @param methodName
     *            the name of the method
     * @param paramTypes
     *            the parameter types of the method (may be {@code null} to
     *            indicate any signature)
     * @return the method, or {@code null} if not found
     * @see Class#getMethod
     */
    public static Method getMethodIfAvailable(Class<?> clazz, String methodName, Class<?>... paramTypes) {
        // Assert.notNull(clazz, "Class must not be null");
        // Assert.notNull(methodName, "Method name must not be null");
        if (paramTypes != null) {
            try {
                return clazz.getMethod(methodName, paramTypes);
            } catch (NoSuchMethodException ex) {
                return null;
            }
        } else {
            Set<Method> candidates = new HashSet<Method>(1);
            Method[] methods = clazz.getMethods();
            for (Method method : methods) {
                if (methodName.equals(method.getName())) {
                    candidates.add(method);
                }
            }
            if (candidates.size() == 1) {
                return candidates.iterator().next();
            }
            return null;
        }
    }
}

Related

  1. getMethodFromClass(Class cls, String methodName, Class argClass, boolean onlyProtectedAndHigher)
  2. getMethodFromClassHierarchy(Class clazz, String methodName)
  3. getMethodFromClassWithInheritance(Class cls, String methodName)
  4. getMethodFullName(Method method)
  5. getMethodIfAvailable(Class clazz, String methodName, Class... paramTypes)
  6. getMethodIfAvailable(Class clazz, String methodName, Class... paramTypes)
  7. getMethodIfAvailable(Class clazz, String methodName, Class... paramTypes)
  8. getMethodIncludingSuperClass(Class clazz, String methodName)
  9. getMethodInternal(Class clazz, String methodName)