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

net.dongliu.dbutils.mapping.BeanProperty Maven / Gradle / Ivy

package net.dongliu.dbutils.mapping;

import net.dongliu.commons.exception.Throwables;

import javax.annotation.Nullable;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.util.Objects;

/**
 * Property by Bean getter/setter
 *
 * @author Liu Dong
 */
public class BeanProperty implements Property {
    // PropertyDescriptor already weak ref to it's type and method
    private final PropertyDescriptor descriptor;

    public BeanProperty(PropertyDescriptor descriptor) {
        this.descriptor = Objects.requireNonNull(descriptor);
    }

    @Override
    public String name() {
        return descriptor.getName();
    }

    @Override
    public Class type() {
        return descriptor.getPropertyType();
    }

    @Override
    public boolean readable() {
        return descriptor.getReadMethod() != null;
    }

    @Override
    public boolean writeable() {
        return descriptor.getWriteMethod() != null;
    }

    @Override
    public Object get(Object obj) {
        try {
            return descriptor.getReadMethod().invoke(obj);
        } catch (IllegalAccessException | InvocationTargetException e) {
            throw Throwables.throwAny(e);
        }
    }

    @Override
    public void set(Object obj, @Nullable Object value) {
        try {
            descriptor.getWriteMethod().invoke(obj, value);
        } catch (IllegalAccessException | InvocationTargetException e) {
            throw Throwables.throwAny(e);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy