Search in sources :

Example 11 with TargetSource

use of cn.taketoday.aop.TargetSource in project today-framework by TAKETODAY.

the class LazyCreationTargetSourceTests method testCreateLazy.

@Test
public void testCreateLazy() {
    TargetSource targetSource = new AbstractLazyCreationTargetSource() {

        @Override
        protected Object createObject() {
            return new InitCountingBean();
        }

        @Override
        public Class<?> getTargetClass() {
            return InitCountingBean.class;
        }
    };
    InitCountingBean proxy = (InitCountingBean) ProxyFactory.getProxy(targetSource);
    assertThat(InitCountingBean.initCount).as("Init count should be 0").isEqualTo(0);
    assertThat(targetSource.getTargetClass()).as("Target class incorrect").isEqualTo(InitCountingBean.class);
    assertThat(InitCountingBean.initCount).as("Init count should still be 0 after getTargetClass()").isEqualTo(0);
    proxy.doSomething();
    assertThat(InitCountingBean.initCount).as("Init count should now be 1").isEqualTo(1);
    proxy.doSomething();
    assertThat(InitCountingBean.initCount).as("Init count should still be 1").isEqualTo(1);
}
Also used : TargetSource(cn.taketoday.aop.TargetSource) Test(org.junit.jupiter.api.Test)

Example 12 with TargetSource

use of cn.taketoday.aop.TargetSource in project today-framework by TAKETODAY.

the class PrototypeBasedTargetSourceTests method testSerializability.

@Test
public void testSerializability() throws Exception {
    PropertyValues tsPvs = new PropertyValues();
    tsPvs.add("targetBeanName", "person");
    RootBeanDefinition tsBd = new RootBeanDefinition(TestTargetSource.class);
    tsBd.setPropertyValues(tsPvs);
    PropertyValues pvs = new PropertyValues();
    RootBeanDefinition bd = new RootBeanDefinition(SerializablePerson.class);
    bd.setPropertyValues(pvs);
    bd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
    StandardBeanFactory bf = new StandardBeanFactory();
    bf.registerBeanDefinition("ts", tsBd);
    bf.registerBeanDefinition("person", bd);
    TestTargetSource cpts = (TestTargetSource) bf.getBean("ts");
    TargetSource serialized = SerializationTestUtils.serializeAndDeserialize(cpts);
    boolean condition = serialized instanceof SingletonTargetSource;
    assertThat(condition).as("Changed to SingletonTargetSource on deserialization").isTrue();
    SingletonTargetSource sts = (SingletonTargetSource) serialized;
    assertThat(sts.getTarget()).isNotNull();
}
Also used : TargetSource(cn.taketoday.aop.TargetSource) PropertyValues(cn.taketoday.beans.PropertyValues) RootBeanDefinition(cn.taketoday.beans.factory.support.RootBeanDefinition) StandardBeanFactory(cn.taketoday.beans.factory.support.StandardBeanFactory) Test(org.junit.jupiter.api.Test)

Example 13 with TargetSource

use of cn.taketoday.aop.TargetSource in project today-framework by TAKETODAY.

the class NoneProxyMethodGenerator method generate.

@Override
public boolean generate(Method method, GeneratorContext context) {
    final AdvisedSupport config = context.getConfig();
    final MethodInterceptor[] interceptors = context.getConfig().getInterceptors(method, context.getTargetClass());
    if (ObjectUtils.isEmpty(interceptors)) {
        final TargetSource targetSource = config.getTargetSource();
        if (targetSource.isStatic()) {
            invokeStaticTarget(method, context);
        } else {
            invokeTargetFromTargetSource(method, context);
        }
        return true;
    }
    return false;
}
Also used : TargetSource(cn.taketoday.aop.TargetSource) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) AdvisedSupport(cn.taketoday.aop.framework.AdvisedSupport)

Example 14 with TargetSource

use of cn.taketoday.aop.TargetSource in project today-framework by TAKETODAY.

the class AbstractSingletonProxyFactoryBean method afterPropertiesSet.

@Override
public void afterPropertiesSet() {
    if (this.target == null) {
        throw new IllegalArgumentException("Property 'target' is required");
    }
    if (this.target instanceof String) {
        throw new IllegalArgumentException("'target' needs to be a bean reference, not a bean name as value");
    }
    if (this.proxyClassLoader == null) {
        this.proxyClassLoader = ClassUtils.getDefaultClassLoader();
    }
    ProxyFactory proxyFactory = new ProxyFactory();
    if (this.preInterceptors != null) {
        for (Object interceptor : this.preInterceptors) {
            proxyFactory.addAdvisor(this.advisorAdapterRegistry.wrap(interceptor));
        }
    }
    // Add the main interceptor (typically an Advisor).
    proxyFactory.addAdvisor(this.advisorAdapterRegistry.wrap(createMainInterceptor()));
    if (this.postInterceptors != null) {
        for (Object interceptor : this.postInterceptors) {
            proxyFactory.addAdvisor(this.advisorAdapterRegistry.wrap(interceptor));
        }
    }
    proxyFactory.copyFrom(this);
    TargetSource targetSource = createTargetSource(this.target);
    proxyFactory.setTargetSource(targetSource);
    if (this.proxyInterfaces != null) {
        proxyFactory.setInterfaces(this.proxyInterfaces);
    } else if (!isProxyTargetClass()) {
        // Rely on AOP infrastructure to tell us what interfaces to proxy.
        Class<?> targetClass = targetSource.getTargetClass();
        if (targetClass != null) {
            proxyFactory.setInterfaces(ClassUtils.getAllInterfacesForClass(targetClass, this.proxyClassLoader));
        }
    }
    postProcessProxyFactory(proxyFactory);
    this.proxy = proxyFactory.getProxy(this.proxyClassLoader);
}
Also used : TargetSource(cn.taketoday.aop.TargetSource) SingletonTargetSource(cn.taketoday.aop.target.SingletonTargetSource)

Example 15 with TargetSource

use of cn.taketoday.aop.TargetSource in project today-infrastructure by TAKETODAY.

the class ContextAnnotationAutowireCandidateResolver method buildLazyResolutionProxy.

protected Object buildLazyResolutionProxy(final DependencyDescriptor descriptor, @Nullable final String beanName) {
    BeanFactory beanFactory = getBeanFactory();
    Assert.state(beanFactory instanceof StandardBeanFactory, "BeanFactory needs to be a StandardBeanFactory");
    final StandardBeanFactory dlbf = (StandardBeanFactory) beanFactory;
    TargetSource ts = new TargetSource() {

        @Override
        public Class<?> getTargetClass() {
            return descriptor.getDependencyType();
        }

        @Override
        public boolean isStatic() {
            return false;
        }

        @Override
        public Object getTarget() {
            Set<String> autowiredBeanNames = beanName != null ? new LinkedHashSet<>(1) : null;
            Object target = dlbf.doResolveDependency(descriptor, beanName, autowiredBeanNames, null);
            if (target == null) {
                Class<?> type = getTargetClass();
                if (Map.class == type) {
                    return Collections.emptyMap();
                } else if (List.class == type) {
                    return Collections.emptyList();
                } else if (Set.class == type || Collection.class == type) {
                    return Collections.emptySet();
                }
                throw new NoSuchBeanDefinitionException(descriptor.getResolvableType(), "Optional dependency not present for lazy injection point");
            }
            if (autowiredBeanNames != null) {
                for (String autowiredBeanName : autowiredBeanNames) {
                    if (dlbf.containsBean(autowiredBeanName)) {
                        dlbf.registerDependentBean(autowiredBeanName, beanName);
                    }
                }
            }
            return target;
        }

        @Override
        public void releaseTarget(Object target) {
        }
    };
    ProxyFactory pf = new ProxyFactory();
    pf.setTargetSource(ts);
    Class<?> dependencyType = descriptor.getDependencyType();
    if (dependencyType.isInterface()) {
        pf.addInterface(dependencyType);
    }
    return pf.getProxy(dlbf.getBeanClassLoader());
}
Also used : TargetSource(cn.taketoday.aop.TargetSource) Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) BeanFactory(cn.taketoday.beans.factory.BeanFactory) StandardBeanFactory(cn.taketoday.beans.factory.support.StandardBeanFactory) Collection(java.util.Collection) List(java.util.List) StandardBeanFactory(cn.taketoday.beans.factory.support.StandardBeanFactory) NoSuchBeanDefinitionException(cn.taketoday.beans.factory.NoSuchBeanDefinitionException)

Aggregations

TargetSource (cn.taketoday.aop.TargetSource)22 MethodInterceptor (org.aopalliance.intercept.MethodInterceptor)8 Test (org.junit.jupiter.api.Test)7 ProxyFactory (cn.taketoday.aop.framework.ProxyFactory)6 SingletonTargetSource (cn.taketoday.aop.target.SingletonTargetSource)6 StandardBeanFactory (cn.taketoday.beans.factory.support.StandardBeanFactory)4 AdvisedSupport (cn.taketoday.aop.framework.AdvisedSupport)3 DebugInterceptor (cn.taketoday.aop.interceptor.DebugInterceptor)3 HotSwappableTargetSource (cn.taketoday.aop.target.HotSwappableTargetSource)3 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)3 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)3 FileNotFoundException (java.io.FileNotFoundException)3 IOException (java.io.IOException)3 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)3 MarshalException (java.rmi.MarshalException)3 SQLException (java.sql.SQLException)3 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)3 Assertions.assertThatIllegalStateException (org.assertj.core.api.Assertions.assertThatIllegalStateException)3 AopInvocationException (cn.taketoday.aop.AopInvocationException)2 AbstractLazyCreationTargetSource (cn.taketoday.aop.target.AbstractLazyCreationTargetSource)2