use of cn.taketoday.aop.TargetSource 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.TargetSource in project today-infrastructure by TAKETODAY.
the class JdkDynamicAopProxy method invoke.
/**
* Implementation of {@code InvocationHandler.invoke}.
* <p>Callers will see exactly the exception thrown by the target,
* unless a hook method throws an exception.
*/
@Override
@Nullable
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object oldProxy = null;
boolean setProxyContext = false;
TargetSource targetSource = advised.getTargetSource();
Object target = null;
try {
if (!equalsDefined && ReflectionUtils.isEqualsMethod(method)) {
// The target does not implement the equals(Object) method itself.
return equals(args[0]);
} else if (!hashCodeDefined && ReflectionUtils.isHashCodeMethod(method)) {
// The target does not implement the hashCode() method itself.
return hashCode();
} else {
Class<?> declaringClass = method.getDeclaringClass();
if (declaringClass == DecoratingProxy.class) {
// There is only getDecoratedClass() declared -> dispatch to proxy config.
return AopProxyUtils.ultimateTargetClass(advised);
} else if (!advised.isOpaque()) {
if (declaringClass.isInterface() && declaringClass.isAssignableFrom(Advised.class)) {
// Service invocations on ProxyConfig with the proxy config...
return AopUtils.invokeJoinpointUsingReflection(advised, method, args);
}
}
}
if (advised.isExposeProxy()) {
// Make invocation available if necessary.
oldProxy = AopContext.setCurrentProxy(proxy);
setProxyContext = true;
}
// Get as late as possible to minimize the time we "own" the target,
// in case it comes from a pool.
target = targetSource.getTarget();
Class<?> targetClass = (target != null ? target.getClass() : null);
// Get the interception chain for this method.
MethodInterceptor[] chain = advised.getInterceptors(method, targetClass);
Object retVal;
// reflective invocation of the target, and avoid creating a MethodInvocation.
if (ObjectUtils.isEmpty(chain)) {
// We can skip creating a MethodInvocation: just invoke the target directly
// Note that the final invoker must be an InvokerInterceptor so we know it does
// nothing but a reflective operation on the target, and no hot swapping or fancy proxying.
Object[] argsToUse = ClassUtils.adaptArgumentsIfNecessary(method, args);
retVal = AopUtils.invokeJoinpointUsingReflection(target, method, argsToUse);
} else {
// Proceed to the join-point through the interceptor chain.
retVal = new DefaultMethodInvocation(proxy, target, method, targetClass, args, chain).proceed();
}
// Massage return value if necessary
Class<?> returnType;
if (retVal != null && retVal == target && (returnType = method.getReturnType()) != Object.class && returnType.isInstance(proxy) && !RawTargetAccess.class.isAssignableFrom(method.getDeclaringClass())) {
// Special case: it returned "this" and the return type of the method
// is type-compatible. Note that we can't help if the target sets
// a reference to itself in another returned object.
retVal = proxy;
}
if (retVal == null) {
returnType = method.getReturnType();
if (returnType != Void.TYPE && returnType.isPrimitive()) {
throw new AopInvocationException("Null return value from advice does not match primitive return type for: " + method);
}
}
return retVal;
} finally {
if (target != null && !targetSource.isStatic()) {
// Must have come from TargetSource.
targetSource.releaseTarget(target);
}
if (setProxyContext) {
// Restore old proxy.
AopContext.setCurrentProxy(oldProxy);
}
}
}
use of cn.taketoday.aop.TargetSource in project today-infrastructure by TAKETODAY.
the class AbstractAopProxyTests method testProxyIsBoundBeforeTargetSourceInvoked.
@Test
public void testProxyIsBoundBeforeTargetSourceInvoked() {
final TestBean target = new TestBean();
ProxyFactory pf = new ProxyFactory(target);
pf.addAdvice(new DebugInterceptor());
pf.setExposeProxy(true);
final ITestBean proxy = (ITestBean) createProxy(pf);
Advised config = (Advised) proxy;
// This class just checks proxy is bound before getTarget() call
config.setTargetSource(new TargetSource() {
@Override
public Class<?> getTargetClass() {
return TestBean.class;
}
@Override
public boolean isStatic() {
return false;
}
@Override
public Object getTarget() throws Exception {
assertThat(AopContext.currentProxy()).isEqualTo(proxy);
return target;
}
@Override
public void releaseTarget(Object target) throws Exception {
}
});
// Just test anything: it will fail if context wasn't found
assertThat(proxy.getAge()).isEqualTo(0);
}
use of cn.taketoday.aop.TargetSource in project today-infrastructure 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);
}
use of cn.taketoday.aop.TargetSource in project today-infrastructure 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);
}
Aggregations