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

com.github.leeonky.util.FieldPropertyWriter Maven / Gradle / Ivy

The newest version!
package com.github.leeonky.util;

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

import static com.github.leeonky.util.Suppressor.run;

class FieldPropertyWriter extends FieldPropertyAccessor implements PropertyWriter {
    private final BiConsumer SETTER = (bean, value) -> run(() -> field.set(bean, tryConvert(value)));

    FieldPropertyWriter(BeanClass beanClass, Field field) {
        super(beanClass, field);
    }

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

    @Override
    public void setValue(T bean, Object value) {
        try {
            setter().accept(bean, value);
        } catch (IllegalArgumentException e) {
            throw new IllegalArgumentException(String.format("Can not set %s to property %s.%s<%s>",
                    value == null ? "null" : Classes.getClassName(value) + "[" + value + "]",
                    getBeanType().getName(), getName(), getType().getName()), e);
        }
    }

    @Override
    public boolean isBeanProperty() {
        return !Modifier.isStatic(field.getModifiers());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy