/** * Create a new advisor for this DeclareParents field. * @param interfaceType static field defining the introduction * @param typePattern type pattern the introduction is restricted to * @param delegateRef the delegate implementation object */ public DeclareParentsAdvisor(Class<?> interfaceType, String typePattern, Object delegateRef) { this(interfaceType, typePattern, new DelegatingIntroductionInterceptor(delegateRef)); }
/** * Wrap each concrete endpoint instance with an AOP proxy, * exposing the message listener's interfaces as well as the * endpoint SPI through an AOP introduction. */ @Override public MessageEndpoint createEndpoint(XAResource xaResource) throws UnavailableException { GenericMessageEndpoint endpoint = (GenericMessageEndpoint) super.createEndpoint(xaResource); ProxyFactory proxyFactory = new ProxyFactory(getMessageListener()); DelegatingIntroductionInterceptor introduction = new DelegatingIntroductionInterceptor(endpoint); introduction.suppressInterface(MethodInterceptor.class); proxyFactory.addAdvice(introduction); return (MessageEndpoint) proxyFactory.getProxy(); }
@Override public Object invoke(MethodInvocation mi) throws Throwable { if (!(mi instanceof ProxyMethodInvocation)) { throw new IllegalStateException("MethodInvocation is not a Spring ProxyMethodInvocation: " + mi); } ProxyMethodInvocation pmi = (ProxyMethodInvocation) mi; pmi.setUserAttribute(BEAN_NAME_ATTRIBUTE, this.beanName); return super.invoke(mi); }
/** * Both constructors use this init method, as it is impossible to pass * a "this" reference from one constructor to another. * @param delegate the delegate object */ private void init(Object delegate) { Assert.notNull(delegate, "Delegate must not be null"); this.delegate = delegate; implementInterfacesOnObject(delegate); // We don't want to expose the control interface suppressInterface(IntroductionInterceptor.class); suppressInterface(DynamicIntroductionAdvice.class); }
/** * Subclasses may need to override this if they want to perform custom * behaviour in around advice. However, subclasses should invoke this * method, which handles introduced interfaces and forwarding to the target. */ @Override @Nullable public Object invoke(MethodInvocation mi) throws Throwable { if (isMethodOnIntroducedInterface(mi)) { // Using the following method rather than direct reflection, we // get correct handling of InvocationTargetException // if the introduced method throws an exception. Object retVal = AopUtils.invokeJoinpointUsingReflection(this.delegate, mi.getMethod(), mi.getArguments()); // Massage return value if possible: if the delegate returned itself, // we really want to return the proxy. if (retVal == this.delegate && mi instanceof ProxyMethodInvocation) { Object proxy = ((ProxyMethodInvocation) mi).getProxy(); if (mi.getMethod().getReturnType().isInstance(proxy)) { retVal = proxy; } } return retVal; } return doProceed(mi); }
ii.suppressInterface(TimeStamped.class);
/** * Construct a new DelegatingIntroductionInterceptor, providing * a delegate that implements the interfaces to be introduced. * @param delegate the delegate that implements the introduced interfaces */ public DelegatingIntroductionInterceptor(Object delegate) { init(delegate); }
/** * Subclasses may need to override this if they want to perform custom * behaviour in around advice. However, subclasses should invoke this * method, which handles introduced interfaces and forwarding to the target. */ public Object invoke(MethodInvocation mi) throws Throwable { if (isMethodOnIntroducedInterface(mi)) { if (logger.isDebugEnabled()) { logger.debug("Invoking self on invocation [" + mi + "]; breaking interceptor chain"); } // Using the following method rather than direct reflection, we // get correct handling of InvocationTargetException // if the introduced method throws an exception. Object retVal = AopUtils.invokeJoinpointUsingReflection(this.delegate, mi.getMethod(), mi.getArguments()); // Massage return value if possible: if the delegate returned itself, // we really want to return the proxy. if (retVal == delegate && (mi instanceof ProxyMethodInvocation)) { retVal = ((ProxyMethodInvocation) mi).getProxy(); } return retVal; } // If we get here, just pass the invocation on. return mi.proceed(); }
/** * Both constructors use this, as it's impossible to pass * "this" from one constructor to another. */ private void init(Object delegate) { Assert.notNull(delegate, "delegate is required"); this.delegate = delegate; implementInterfacesOnObject(delegate); // We don't want to expose the control interface suppressInterface(IntroductionInterceptor.class); suppressInterface(DynamicIntroductionAdvice.class); }
/** * Subclasses may need to override this if they want to perform custom * behaviour in around advice. However, subclasses should invoke this * method, which handles introduced interfaces and forwarding to the target. */ @Override @Nullable public Object invoke(MethodInvocation mi) throws Throwable { if (isMethodOnIntroducedInterface(mi)) { // Using the following method rather than direct reflection, we // get correct handling of InvocationTargetException // if the introduced method throws an exception. Object retVal = AopUtils.invokeJoinpointUsingReflection(this.delegate, mi.getMethod(), mi.getArguments()); // Massage return value if possible: if the delegate returned itself, // we really want to return the proxy. if (retVal == this.delegate && mi instanceof ProxyMethodInvocation) { Object proxy = ((ProxyMethodInvocation) mi).getProxy(); if (mi.getMethod().getReturnType().isInstance(proxy)) { retVal = proxy; } } return retVal; } return doProceed(mi); }
/** * Construct a new DelegatingIntroductionInterceptor. * The delegate will be the subclass, which must implement * additional interfaces. */ protected DelegatingIntroductionInterceptor() { init(this); }
/** * Return an IntroductionAdvisor that providing a mixin * exposing statistics about the pool maintained by this object. */ public DefaultIntroductionAdvisor getPoolingConfigMixin() { DelegatingIntroductionInterceptor dii = new DelegatingIntroductionInterceptor(this); return new DefaultIntroductionAdvisor(dii, PoolingConfig.class); }
/** * Wrap each concrete endpoint instance with an AOP proxy, * exposing the message listener's interfaces as well as the * endpoint SPI through an AOP introduction. */ @Override public MessageEndpoint createEndpoint(XAResource xaResource) throws UnavailableException { GenericMessageEndpoint endpoint = (GenericMessageEndpoint) super.createEndpoint(xaResource); ProxyFactory proxyFactory = new ProxyFactory(getMessageListener()); DelegatingIntroductionInterceptor introduction = new DelegatingIntroductionInterceptor(endpoint); introduction.suppressInterface(MethodInterceptor.class); proxyFactory.addAdvice(introduction); return (MessageEndpoint) proxyFactory.getProxy(); }
/** * Both constructors use this init method, as it is impossible to pass * a "this" reference from one constructor to another. * @param delegate the delegate object */ private void init(Object delegate) { Assert.notNull(delegate, "Delegate must not be null"); this.delegate = delegate; implementInterfacesOnObject(delegate); // We don't want to expose the control interface suppressInterface(IntroductionInterceptor.class); suppressInterface(DynamicIntroductionAdvice.class); }
/** * Note that we need to override around advice. * If the method is a setter and we're locked, prevent execution. * Otherwise let super.invoke() handle it, and do normal * Lockable(this) then target behaviour. * @see org.aopalliance.MethodInterceptor#invoke(org.aopalliance.MethodInvocation) */ @Override public Object invoke(MethodInvocation invocation) throws Throwable { if (locked() && invocation.getMethod().getName().indexOf("set") == 0) throw new LockedException(); return super.invoke(invocation); }
/** * Subclasses may need to override this if they want to perform custom * behaviour in around advice. However, subclasses should invoke this * method, which handles introduced interfaces and forwarding to the target. */ @Override public Object invoke(MethodInvocation mi) throws Throwable { if (isMethodOnIntroducedInterface(mi)) { // Using the following method rather than direct reflection, we // get correct handling of InvocationTargetException // if the introduced method throws an exception. Object retVal = AopUtils.invokeJoinpointUsingReflection(this.delegate, mi.getMethod(), mi.getArguments()); // Massage return value if possible: if the delegate returned itself, // we really want to return the proxy. if (retVal == this.delegate && mi instanceof ProxyMethodInvocation) { Object proxy = ((ProxyMethodInvocation) mi).getProxy(); if (mi.getMethod().getReturnType().isInstance(proxy)) { retVal = proxy; } } return retVal; } return doProceed(mi); }
/** * Construct a new DelegatingIntroductionInterceptor, providing * a delegate that implements the interfaces to be introduced. * @param delegate the delegate that implements the introduced interfaces */ public DelegatingIntroductionInterceptor(Object delegate) { init(delegate); }
/** * Return an introduction advisor mixin that allows the AOP proxy to be * cast to ThreadLocalInvokerStats. */ public IntroductionAdvisor getStatsMixin() { DelegatingIntroductionInterceptor dii = new DelegatingIntroductionInterceptor(this); return new DefaultIntroductionAdvisor(dii, ThreadLocalTargetSourceStats.class); }
/** * Create a refreshable proxy for the given AOP TargetSource. * @param ts the refreshable TargetSource * @param interfaces the proxy interfaces (may be {@code null} to * indicate proxying of all interfaces implemented by the target class) * @return the generated proxy * @see RefreshableScriptTargetSource */ protected Object createRefreshableProxy(TargetSource ts, @Nullable Class<?>[] interfaces, boolean proxyTargetClass) { ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.setTargetSource(ts); ClassLoader classLoader = this.beanClassLoader; if (interfaces != null) { proxyFactory.setInterfaces(interfaces); } else { Class<?> targetClass = ts.getTargetClass(); if (targetClass != null) { proxyFactory.setInterfaces(ClassUtils.getAllInterfacesForClass(targetClass, this.beanClassLoader)); } } if (proxyTargetClass) { classLoader = null; // force use of Class.getClassLoader() proxyFactory.setProxyTargetClass(true); } DelegatingIntroductionInterceptor introduction = new DelegatingIntroductionInterceptor(ts); introduction.suppressInterface(TargetSource.class); proxyFactory.addAdvice(introduction); return proxyFactory.getProxy(classLoader); }
/** * Both constructors use this init method, as it is impossible to pass * a "this" reference from one constructor to another. * @param delegate the delegate object */ private void init(Object delegate) { Assert.notNull(delegate, "Delegate must not be null"); this.delegate = delegate; implementInterfacesOnObject(delegate); // We don't want to expose the control interface suppressInterface(IntroductionInterceptor.class); suppressInterface(DynamicIntroductionAdvice.class); }