Consider the following method which returns a field if it exists or recursively calls itself until the field is found:
private Field getField(Class>
The arguments for a method are always evaluated before the method is called. You want orElseGet
which takes a Supplier
that will only be invoked if the Optional
is not present:
private Field getField(Class<?> clazz, String p) {
return Arrays.stream(clazz.getDeclaredFields())
.filter(f -> p.equals(f.getName()))
.findFirst()
.orElseGet(() -> getField(clazz.getSuperclass(), p));
}