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

com.github.leeonky.dal.runtime.JavaClassPropertyAccessor Maven / Gradle / Ivy

package com.github.leeonky.dal.runtime;

import com.github.leeonky.util.BeanClass;
import com.github.leeonky.util.NoSuchAccessorException;

import java.util.LinkedHashSet;
import java.util.Objects;
import java.util.Set;

import static java.lang.String.format;

public class JavaClassPropertyAccessor implements PropertyAccessor {
    private final BeanClass beanClass;

    public JavaClassPropertyAccessor(BeanClass type) {
        beanClass = type;
    }

    @Override
    public Object getValue(T instance, Object property) {
        try {
            return beanClass.getPropertyValue(instance, (String) property);
        } catch (NoSuchAccessorException ignore) {
            throw new InvalidPropertyException(format("Method or property `%s` does not exist in `%s`", property, instance.getClass().getName()));
        }
    }

    @Override
    public Set getPropertyNames(T instance) {
        return new LinkedHashSet<>(beanClass.getPropertyReaders().keySet());
    }

    @Override
    public boolean isNull(T instance) {
        return Objects.equals(instance, null);
    }
}