Search in sources :

Example 21 with AdvisedSupport

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

the class DefaultProxyMethodGenerator method generateProxyMethod.

protected void generateProxyMethod(Method method, String targetInvField, GeneratorContext context, CodeEmitter codeEmitter) {
    final AdvisedSupport config = context.getConfig();
    final boolean exposeProxy = config.isExposeProxy();
    final boolean isStatic = config.getTargetSource().isStatic();
    // 
    final boolean opaque = config.isOpaque();
    if (opaque) {
        // cannot change interceptor chain
        // load proxy object: this
        codeEmitter.loadThis();
        codeEmitter.loadThis();
        if (isStatic) {
            // Object target, Target targetInv, Object[] args
            codeEmitter.getField(FIELD_TARGET);
            codeEmitter.getField(targetInvField);
            prepareArgs(method, codeEmitter);
            if (exposeProxy) {
                codeEmitter.invokeStatic(stdProxyInvoker, staticExposeProceed);
            } else {
                codeEmitter.invokeStatic(stdProxyInvoker, proceed);
            }
        } else {
            // TargetSource targetSource, Target targetInv, Object[] args
            codeEmitter.getField(FIELD_TARGET_SOURCE);
            codeEmitter.getField(targetInvField);
            prepareArgs(method, codeEmitter);
            if (exposeProxy) {
                codeEmitter.invokeStatic(stdProxyInvoker, dynamicExposeProceed);
            } else {
                codeEmitter.invokeStatic(stdProxyInvoker, dynamicProceed);
            }
        }
    } else {
        // ------------------------------
        // dynamic Advised
        // Object proxy, AdvisedSupport advised, TargetInvocation targetInv, Object[] args
        codeEmitter.loadThis();
        codeEmitter.loadThis();
        codeEmitter.getField(FIELD_CONFIG);
        codeEmitter.getField(targetInvField);
        prepareArgs(method, codeEmitter);
        codeEmitter.invokeStatic(stdProxyInvoker, dynamicAdvisedProceed);
    }
}
Also used : AdvisedSupport(cn.taketoday.aop.framework.AdvisedSupport)

Example 22 with AdvisedSupport

use of cn.taketoday.aop.framework.AdvisedSupport 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 23 with AdvisedSupport

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

the class DefaultProxyMethodGenerator method generateProxyMethod.

protected void generateProxyMethod(Method method, String targetInvField, GeneratorContext context, CodeEmitter codeEmitter) {
    final AdvisedSupport config = context.getConfig();
    final boolean exposeProxy = config.isExposeProxy();
    final boolean isStatic = config.getTargetSource().isStatic();
    // 
    final boolean opaque = config.isOpaque();
    if (opaque) {
        // cannot change interceptor chain
        // load proxy object: this
        codeEmitter.loadThis();
        codeEmitter.loadThis();
        if (isStatic) {
            // Object target, Target targetInv, Object[] args
            codeEmitter.getField(FIELD_TARGET);
            codeEmitter.getField(targetInvField);
            prepareArgs(method, codeEmitter);
            if (exposeProxy) {
                codeEmitter.invokeStatic(stdProxyInvoker, staticExposeProceed);
            } else {
                codeEmitter.invokeStatic(stdProxyInvoker, proceed);
            }
        } else {
            // TargetSource targetSource, Target targetInv, Object[] args
            codeEmitter.getField(FIELD_TARGET_SOURCE);
            codeEmitter.getField(targetInvField);
            prepareArgs(method, codeEmitter);
            if (exposeProxy) {
                codeEmitter.invokeStatic(stdProxyInvoker, dynamicExposeProceed);
            } else {
                codeEmitter.invokeStatic(stdProxyInvoker, dynamicProceed);
            }
        }
    } else {
        // ------------------------------
        // dynamic Advised
        // Object proxy, AdvisedSupport advised, TargetInvocation targetInv, Object[] args
        codeEmitter.loadThis();
        codeEmitter.loadThis();
        codeEmitter.getField(FIELD_CONFIG);
        codeEmitter.getField(targetInvField);
        prepareArgs(method, codeEmitter);
        codeEmitter.invokeStatic(stdProxyInvoker, dynamicAdvisedProceed);
    }
}
Also used : AdvisedSupport(cn.taketoday.aop.framework.AdvisedSupport)

Example 24 with AdvisedSupport

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

the class AbstractAopProxyTests method testDeclaredException.

@Test
public void testDeclaredException() throws Throwable {
    final Exception expectedException = new Exception();
    // Test return value
    MethodInterceptor mi = new MethodInterceptor() {

        @Override
        public Object invoke(MethodInvocation invocation) throws Throwable {
            throw expectedException;
        }
    };
    AdvisedSupport pc = new AdvisedSupport(ITestBean.class);
    pc.addAdvice(ExposeInvocationInterceptor.INSTANCE);
    pc.addAdvice(mi);
    // We don't care about the object
    mockTargetSource.setTarget(new TestBean());
    pc.setTargetSource(mockTargetSource);
    AopProxy aop = createAopProxy(pc);
    assertThatExceptionOfType(Exception.class).isThrownBy(() -> {
        ITestBean tb = (ITestBean) aop.getProxy();
        // Note: exception param below isn't used
        tb.exceptional(expectedException);
    }).matches(expectedException::equals);
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) AopProxy(cn.taketoday.aop.framework.AopProxy) MethodInvocation(org.aopalliance.intercept.MethodInvocation) AopConfigException(cn.taketoday.aop.framework.AopConfigException) LockedException(cn.taketoday.aop.mixin.LockedException) FileNotFoundException(java.io.FileNotFoundException) SQLException(java.sql.SQLException) MarshalException(java.rmi.MarshalException) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) IOException(java.io.IOException) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) AdvisedSupport(cn.taketoday.aop.framework.AdvisedSupport) Test(org.junit.jupiter.api.Test)

Example 25 with AdvisedSupport

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

the class AbstractAopProxyTests method testTargetCanGetInvocationEvenIfNoAdviceChain.

/**
 * Check that although a method is eligible for advice chain optimization and
 * direct reflective invocation, it doesn't happen if we've asked to see the proxy,
 * so as to guarantee a consistent programming model.
 */
@Test
public void testTargetCanGetInvocationEvenIfNoAdviceChain() throws Throwable {
    NeedsToSeeProxy target = new NeedsToSeeProxy();
    AdvisedSupport pc = new AdvisedSupport(INeedsToSeeProxy.class);
    pc.setTarget(target);
    pc.setExposeProxy(true);
    // Now let's try it with the special target
    AopProxy aop = createAopProxy(pc);
    INeedsToSeeProxy proxied = (INeedsToSeeProxy) aop.getProxy();
    // It will complain if it can't get the proxy
    proxied.incrementViaProxy();
}
Also used : AopProxy(cn.taketoday.aop.framework.AopProxy) AdvisedSupport(cn.taketoday.aop.framework.AdvisedSupport) Test(org.junit.jupiter.api.Test)

Aggregations

AdvisedSupport (cn.taketoday.aop.framework.AdvisedSupport)34 Test (org.junit.jupiter.api.Test)27 AopProxy (cn.taketoday.aop.framework.AopProxy)17 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)12 CglibAopProxy (cn.taketoday.aop.framework.CglibAopProxy)11 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)9 MethodInterceptor (org.aopalliance.intercept.MethodInterceptor)9 NopInterceptor (cn.taketoday.aop.NopInterceptor)8 MethodInvocation (org.aopalliance.intercept.MethodInvocation)7 AopConfigException (cn.taketoday.aop.framework.AopConfigException)5 JdkDynamicAopProxy (cn.taketoday.aop.framework.JdkDynamicAopProxy)4 TargetSource (cn.taketoday.aop.TargetSource)3 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)3 LockedException (cn.taketoday.aop.mixin.LockedException)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)2 MarshalException (java.rmi.MarshalException)2 SQLException (java.sql.SQLException)2 Assertions.assertThatIllegalStateException (org.assertj.core.api.Assertions.assertThatIllegalStateException)2