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

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

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

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

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

class MethodPropertyWriter extends MethodProperty implements PropertyWriter {
    private static final int SETTER_PREFIX_LENGTH = 3;
    private final BiConsumer SETTER = (bean, value) -> run(() -> method.invoke(bean, tryConvert(value)));
    private String name;

    MethodPropertyWriter(BeanClass beanClass, Method method) {
        super(beanClass, method);
    }

    static boolean isSetter(Method method) {
        return method.getName().startsWith("set") && method.getParameterTypes().length == 1;
    }

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

    @Override
    public String getName() {
        if (name == null)
            return name = unCapitalize(method.getName().substring(SETTER_PREFIX_LENGTH));
        return name;
    }

    @Override
    protected Type provideGenericType() {
        return method.getGenericParameterTypes()[0];
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy