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

Open Source 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: Open Source License 

import java.lang.reflect.Method;

import java.util.HashSet;

import java.util.Set;

public class Main {
    /**//  ww w . ja  v  a 2  s  . com
     * 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) {
        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;
        }
    }

    /**
     * Determine whether the given class has a public method with the given signature,
     * and return it if available (else throws an {@code IllegalStateException}).
     * <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 IllegalStateException}.
     * @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 (never {@code null})
     * @throws IllegalStateException if the method has not been found
     * @see Class#getMethod
     */
    public static Method getMethod(Class<?> clazz, String methodName, Class<?>... paramTypes) {
        if (paramTypes != null) {
            try {
                return clazz.getMethod(methodName, paramTypes);
            } catch (NoSuchMethodException ex) {
                throw new IllegalStateException("Expected method not found: " + ex);
            }
        } 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();
            } else if (candidates.isEmpty()) {
                throw new IllegalStateException("Expected method not found: " + clazz + "." + methodName);
            } else {
                throw new IllegalStateException("No unique method found: " + clazz + "." + methodName);
            }
        }
    }
}

Related

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