io.github.konohiroaki.deepinitializer.ReflectionUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of deep-initializer Show documentation
Show all versions of deep-initializer Show documentation
Initialize deep bean recursively filling with default values.
The newest version!
package io.github.konohiroaki.deepinitializer;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
class ReflectionUtils {
static Set getAllFields(Class> clazz) {
Set fields = new HashSet<>(Arrays.asList(clazz.getDeclaredFields()));
if (clazz.getSuperclass() != null) {
fields.addAll(getAllFields(clazz.getSuperclass()));
}
return fields.stream()
.filter(field -> !field.isSynthetic())
.collect(Collectors.toSet());
}
static void setProperty(Object object, Field field, Object value) {
boolean access = field.isAccessible();
field.setAccessible(true);
try {
field.set(object, value);
} catch (IllegalAccessException e) {
throw new IllegalArgumentException("Failed to set property to field " + field.getName());
}
field.setAccessible(access);
}
}