use of cn.taketoday.aop.framework.AdvisedSupport in project today-infrastructure 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-framework by TAKETODAY.
the class AbstractAopProxyTests method testTargetCanGetInvocation.
@Test
public void testTargetCanGetInvocation() throws Throwable {
final InvocationCheckExposedInvocationTestBean expectedTarget = new InvocationCheckExposedInvocationTestBean();
AdvisedSupport pc = new AdvisedSupport(ITestBean.class, IOther.class);
pc.addAdvice(ExposeInvocationInterceptor.INSTANCE);
TrapTargetInterceptor tii = new TrapTargetInterceptor() {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
// Assert that target matches BEFORE invocation returns
assertThat(invocation.getThis()).as("Target is correct").isEqualTo(expectedTarget);
return super.invoke(invocation);
}
};
pc.addAdvice(tii);
pc.setTarget(expectedTarget);
AopProxy aop = createAopProxy(pc);
ITestBean tb = (ITestBean) aop.getProxy();
tb.getName();
}
use of cn.taketoday.aop.framework.AdvisedSupport in project today-framework by TAKETODAY.
the class AbstractAopProxyTests method testUserAttributes.
@Test
public void testUserAttributes() throws Throwable {
class MapAwareMethodInterceptor implements MethodInterceptor {
private final Map<String, String> expectedValues;
private final Map<String, String> valuesToAdd;
public MapAwareMethodInterceptor(Map<String, String> expectedValues, Map<String, String> valuesToAdd) {
this.expectedValues = expectedValues;
this.valuesToAdd = valuesToAdd;
}
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
DefaultMethodInvocation rmi = (DefaultMethodInvocation) invocation;
for (Iterator<String> it = rmi.getAttributes().keySet().iterator(); it.hasNext(); ) {
Object key = it.next();
assertThat(rmi.getAttributes().get(key)).isEqualTo(expectedValues.get(key));
}
rmi.getAttributes().putAll(valuesToAdd);
return invocation.proceed();
}
}
AdvisedSupport pc = new AdvisedSupport(ITestBean.class);
MapAwareMethodInterceptor mami1 = new MapAwareMethodInterceptor(new HashMap<>(), new HashMap<String, String>());
Map<String, String> firstValuesToAdd = new HashMap<>();
firstValuesToAdd.put("test", "");
MapAwareMethodInterceptor mami2 = new MapAwareMethodInterceptor(new HashMap<>(), firstValuesToAdd);
MapAwareMethodInterceptor mami3 = new MapAwareMethodInterceptor(firstValuesToAdd, new HashMap<>());
MapAwareMethodInterceptor mami4 = new MapAwareMethodInterceptor(firstValuesToAdd, new HashMap<>());
Map<String, String> secondValuesToAdd = new HashMap<>();
secondValuesToAdd.put("foo", "bar");
secondValuesToAdd.put("cat", "dog");
MapAwareMethodInterceptor mami5 = new MapAwareMethodInterceptor(firstValuesToAdd, secondValuesToAdd);
Map<String, String> finalExpected = new HashMap<>(firstValuesToAdd);
finalExpected.putAll(secondValuesToAdd);
MapAwareMethodInterceptor mami6 = new MapAwareMethodInterceptor(finalExpected, secondValuesToAdd);
pc.addAdvice(mami1);
pc.addAdvice(mami2);
pc.addAdvice(mami3);
pc.addAdvice(mami4);
pc.addAdvice(mami5);
pc.addAdvice(mami6);
// We don't care about the object
pc.setTarget(new TestBean());
AopProxy aop = createAopProxy(pc);
ITestBean tb = (ITestBean) aop.getProxy();
String newName = "foo";
tb.setName(newName);
assertThat(tb.getName()).isEqualTo(newName);
}
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 JdkDynamicAopProxyTests method testTargetCanGetInvocationWithPrivateClass.
@Test
public void testTargetCanGetInvocationWithPrivateClass() {
final ExposedInvocationTestBean expectedTarget = new ExposedInvocationTestBean() {
@Override
protected void assertions(MethodInvocation invocation) {
assertThat(invocation.getThis()).isEqualTo(this);
assertThat(invocation.getMethod().getDeclaringClass()).as("Invocation should be on ITestBean: " + invocation.getMethod()).isEqualTo(ITestBean.class);
}
};
AdvisedSupport pc = new AdvisedSupport(ITestBean.class, IOther.class);
pc.addAdvice(ExposeInvocationInterceptor.INSTANCE);
TrapTargetInterceptor tii = new TrapTargetInterceptor() {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
// Assert that target matches BEFORE invocation returns
assertThat(invocation.getThis()).as("Target is correct").isEqualTo(expectedTarget);
return super.invoke(invocation);
}
};
pc.addAdvice(tii);
pc.setTarget(expectedTarget);
AopProxy aop = createAopProxy(pc);
ITestBean tb = (ITestBean) aop.getProxy();
tb.getName();
}
Aggregations