A pure Java solution1 could be using reflection. Here a very simple implementation, mostly to show the idea (not complete, assuming fields are String
, poor (no) exception handling):
public static boolean isEmpty(Demo demo) throws IllegalArgumentException, IllegalAccessException {
for (var field : Demo.class.getDeclaredFields()) {
if (field.getType() == String.class) {
var value = (String) field.get(demo);
if (value != null && !value.isEmpty()) {
return false;
}
}
}
return true;
}
It could be improved for example by adding more types, using annotation on fields, ...
1: question is only tagged java
var
was introduced in Java 10; use the concrete type (for (Field...
or String value...
) if using previous version of Java