All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.konohiroaki.deepinitializer.ReflectionUtils Maven / Gradle / Ivy

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);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy