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

com.truthbean.debbie.proxy.JdkBeanProxyHandler Maven / Gradle / Ivy

The newest version!
package com.truthbean.debbie.proxy;

import com.truthbean.Logger;
import com.truthbean.LoggerFactory;
import com.truthbean.debbie.bean.BeanInfo;
import com.truthbean.debbie.bean.ClassBeanInfo;
import com.truthbean.debbie.core.ApplicationContext;
import com.truthbean.debbie.proxy.jdk.JdkDynamicProxy;

/**
 * @author TruthBean
 * @since 0.5.3
 * Created on 2021/11/14 22:16.
 */
public class JdkBeanProxyHandler implements BeanProxyHandler {
    private final ApplicationContext applicationContext;

    public JdkBeanProxyHandler(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    @Override
    public  T proxyCreatedBean(BeanInfo beanInfo, K bean, Class clazz, BeanProxyType proxyType) {
        if (proxyType == BeanProxyType.NO) {
            return bean;
        }
        if (proxyType == BeanProxyType.JDK) {
            if (clazz != null && clazz.isInterface()) {
                LOGGER.trace(() -> "resolve field dependent bean(" + clazz + ") by implement class " + clazz);
                JdkDynamicProxy dynamicProxy = new JdkDynamicProxy<>();
                return dynamicProxy.invokeJdkProxy(this.applicationContext, clazz, bean);
            } else if (beanInfo instanceof ClassBeanInfo classBeanInfo) {
                if (!classBeanInfo.hasAnnotatedMethod() && !classBeanInfo.containClassAnnotation(MethodProxy.class)) {
                    return bean;
                }
                Class beanClass = classBeanInfo.getBeanClass();
                Class beanInterface = classBeanInfo.getInterface(applicationContext.getBeanInfoManager().getIgnoreInterface());
                if (beanInterface != null) {
                    LOGGER.trace(() -> "resolve field dependent bean(" + beanInterface + ") by implement class " + beanClass);
                    JdkDynamicProxy dynamicProxy = new JdkDynamicProxy<>();
                    if (!classBeanInfo.isCreated()) {
                        LOGGER.error("bean(" + beanInterface + ") has no VALUE!!");
                        return null;
                    }
                    return dynamicProxy.invokeJdkProxy(this.applicationContext, beanInterface, bean);
                }
            }
        }
        return bean;
    }

    private static final Logger LOGGER = LoggerFactory.getLogger(BeanProxyHandler.class);
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy