Optional<Method> (Class<?> clazz, String methodName) { for (; clazz != null && clazz != Object.class; clazz = clazz.getSuperclass()) { var methods = Arrays.stream(clazz.getDeclaredMethods()) .filter(method -> methodName.equals(method.getName()) && method.getParameterCount() == 0).toList(); if (methods.size() > 1) { break; } if (methods.size() == 1) { return Optional.of(methods.get(0)); } } return Optional.empty(); }