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

com.talanlabs.configproperties.component.meta.ComponentMetaInfoBean Maven / Gradle / Ivy

The newest version!
package com.talanlabs.configproperties.component.meta;

import com.talanlabs.component.IComponent;
import com.talanlabs.component.factory.ComponentDescriptor;
import com.talanlabs.component.factory.ComponentFactory;
import com.talanlabs.configproperties.meta.DefaultMetaInfoBean;
import com.talanlabs.configproperties.meta.MetaInfoBean;

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.Set;
import java.util.function.Function;

public class ComponentMetaInfoBean extends MetaInfoBean {

    private ComponentDescriptor componentDescriptor;

    public ComponentMetaInfoBean(Class beanClass) {
        super(beanClass);

        this.componentDescriptor = ComponentFactory.getInstance().getDescriptor(beanClass);
    }

    public static  Function, MetaInfoBean> createDelegateMetaInfoBeanFunction() {
        return beanClass -> {
            if (ComponentFactory.getInstance().isComponentType(beanClass)) {
                return (MetaInfoBean) new ComponentMetaInfoBean<>((Class) beanClass);
            } else {
                return new DefaultMetaInfoBean<>(beanClass);
            }
        };
    }

    @Override
    public E createInstance() {
        return ComponentFactory.getInstance().createInstance(beanClass);
    }

    @Override
    public Set getPropertyNames() {
        return this.componentDescriptor.getPropertyNames();
    }

    private void checkProperty(String propertyName) {
        if (!this.componentDescriptor.getPropertyNames().contains(propertyName)) {
            throw new IllegalArgumentException("Property " + propertyName + " not found in " + beanClass);
        }
    }

    @Override
    public void setPropertyValue(E bean, String propertyName, Object value) {
        checkProperty(propertyName);
        bean.straightSetProperty(propertyName, value);
    }

    @Override
    public Object getPropertyValue(E bean, String propertyName) {
        checkProperty(propertyName);
        return bean.straightGetProperty(propertyName);
    }

    @Override
    public Type getPropertyType(String propertyName) {
        checkProperty(propertyName);
        return componentDescriptor.getPropertyType(propertyName);
    }

    @Override
    public Class getPropertyClass(String propertyName) {
        checkProperty(propertyName);
        return componentDescriptor.getPropertyClass(propertyName);
    }

    @Override
    public boolean isPropertyAnnotationPresent(String propertyName, Class annotationClass) {
        checkProperty(propertyName);
        return componentDescriptor.getPropertyDescriptor(propertyName).getMethod().isAnnotationPresent(annotationClass);
    }

    @SuppressWarnings("unchecked")
    @Override
    public  A getPropertyAnnotation(String propertyName, Class annotationClass) {
        checkProperty(propertyName);
        return componentDescriptor.getPropertyDescriptor(propertyName).getMethod().getAnnotation(annotationClass);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy