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);
}
}
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;
}
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);
}
}
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);
}
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();
}
Aggregations