getMethodIfAvailable static method

Method? getMethodIfAvailable(
  1. Class clazz,
  2. String methodName, [
  3. List<Class>? paramTypes
])

Retrieves the method with the specified name and parameter types from the given class.

  • Parameters clazz: the class to analyze for method methodName: the name of the method to retrieve paramTypes: the parameter types of the method to retrieve

  • Returns The method with the specified name and parameter types from the given class.

  • Throws PodDefinitionValidationException if the specified method is not found in the given class.

  • Example

final method = ClassUtils.getMethodIfAvailable(Class.of<UserService>(), 'configure', [Class.of<String>()]);

Implementation

static Method? getMethodIfAvailable(Class clazz, String methodName, [List<Class>? paramTypes]) {
  if (paramTypes != null) {
			return getMethodOrNull(clazz, methodName, paramTypes);
		} else {
			Set<Method> candidates = findMethodCandidatesByName(clazz, methodName);
			if (candidates.length == 1) {
				return candidates.iterator.moveNext() ? candidates.iterator.current : candidates.single;
			}
			return null;
		}
}