Here you can find the source of getMethodIfAvailable(Class> clazz, String methodName, Class>... paramTypes)
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) |
public static Method getMethodIfAvailable(Class<?> clazz, String methodName, Class<?>... paramTypes)
//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); } } } }