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

se.fortnox.reactivewizard.util.FieldSetter Maven / Gradle / Ivy

There is a newer version: 24.6.0
Show newest version
package se.fortnox.reactivewizard.util;

import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.function.BiConsumer;

/**
 * Represents a setter field.
 */
public class FieldSetter implements Setter {
    public static  Setter create(Class cls, Field field) {
        AccessorUtil.MemberTypeInfo memberTypeInfo = AccessorUtil.fieldTypeInfo(cls, field);
        return new FieldSetter<>(field, memberTypeInfo.getReturnType(), memberTypeInfo.getGenericReturnType());
    }

    private final Field           field;
    private final BiConsumer setter;
    private final Class        parameterType;
    private final Type            genericParameterType;

    private FieldSetter(Field field, Class parameterType, Type genericParameterType) {
        this.field = field;
        this.parameterType = parameterType;
        this.genericParameterType = genericParameterType;
        field.setAccessible(true);
        setter = (instance,value) -> {
            try {
                field.set(instance, value);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        };
    }

    @Override
    public void invoke(I instance, T value) throws IllegalAccessException {
        field.set(instance, value);
    }

    @Override
    public Class getParameterType() {
        return parameterType;
    }

    @Override
    public Type getGenericParameterType() {
        return genericParameterType;
    }

    @Override
    public BiConsumer setterFunction() {
        return setter;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy