Refine search
private static Object getTargetObject(final Object object) { try { Field advised = object.getClass().getDeclaredField("advised"); advised.setAccessible(true); return ((AdvisedSupport) advised.get(object)).getTargetSource().getTarget(); // CHECKSTYLE:OFF } catch (final Exception ex) { // CHECKSTYLE:ON throw new JobSystemException(ex); } } }
/** * Build a configuration-only copy of this AdvisedSupport, * replacing the TargetSource. */ AdvisedSupport getConfigurationOnlyCopy() { AdvisedSupport copy = new AdvisedSupport(); copy.copyFrom(this); copy.targetSource = EmptyTargetSource.forClass(getTargetClass(), getTargetSource().isStatic()); copy.advisorChainFactory = this.advisorChainFactory; copy.interfaces = this.interfaces; copy.advisors = this.advisors; copy.updateAdvisorArray(); return copy; }
@Test public void testProxyAProxyWithAdditionalInterface() { ITestBean target = new TestBean(); mockTargetSource.setTarget(target); AdvisedSupport as = new AdvisedSupport(); as.setTargetSource(mockTargetSource); as.addAdvice(new NopInterceptor()); as.addInterface(Serializable.class); CglibAopProxy cglib = new CglibAopProxy(as); ITestBean proxy1 = (ITestBean) cglib.getProxy(); mockTargetSource.setTarget(proxy1); as = new AdvisedSupport(new Class<?>[]{}); as.setTargetSource(mockTargetSource); as.addAdvice(new NopInterceptor()); cglib = new CglibAopProxy(as); ITestBean proxy2 = (ITestBean) cglib.getProxy(); assertTrue(proxy2 instanceof Serializable); }
@Override public boolean replaceAdvisor(Advisor a, Advisor b) throws AopConfigException { Assert.notNull(a, "Advisor a must not be null"); Assert.notNull(b, "Advisor b must not be null"); int index = indexOf(a); if (index == -1) { return false; } removeAdvisor(index); addAdvisor(index, b); return true; }
/** * Construct a new JdkDynamicAopProxy for the given AOP configuration. * @param config the AOP configuration as AdvisedSupport object * @throws AopConfigException if the config is invalid. We try to throw an informative * exception in this case, rather than let a mysterious failure happen later. */ public JdkDynamicAopProxy(AdvisedSupport config) throws AopConfigException { Assert.notNull(config, "AdvisedSupport must not be null"); if (config.getAdvisors().length == 0 && config.getTargetSource() == AdvisedSupport.EMPTY_TARGET_SOURCE) { throw new AopConfigException("No advisors and no TargetSource specified"); } this.advised = config; }
private Callback[] getCallbacks(Class<?> rootClass) throws Exception { boolean exposeProxy = this.advised.isExposeProxy(); boolean isFrozen = this.advised.isFrozen(); boolean isStatic = this.advised.getTargetSource().isStatic(); if (exposeProxy) { targetInterceptor = (isStatic ? new StaticUnadvisedExposedInterceptor(this.advised.getTargetSource().getTarget()) : new DynamicUnadvisedExposedInterceptor(this.advised.getTargetSource())); new StaticUnadvisedInterceptor(this.advised.getTargetSource().getTarget()) : new DynamicUnadvisedInterceptor(this.advised.getTargetSource())); new StaticDispatcher(this.advised.getTargetSource().getTarget()) : new SerializableNoOp()); List<Object> chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(methods[x], rootClass); fixedCallbacks[x] = new FixedChainStaticTargetInterceptor( chain, this.advised.getTargetSource().getTarget(), this.advised.getTargetClass()); this.fixedInterceptorMap.put(methods[x].toString(), x);
boolean setProxyContext = false; Object target = null; TargetSource targetSource = this.advised.getTargetSource(); try { if (this.advised.exposeProxy) { target = targetSource.getTarget(); Class<?> targetClass = (target != null ? target.getClass() : null); List<Object> chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass); Object retVal; if (target != null && !targetSource.isStatic()) { targetSource.releaseTarget(target);
@Override public Object getProxy(@Nullable ClassLoader classLoader) { if (logger.isTraceEnabled()) { logger.trace("Creating CGLIB proxy: " + this.advised.getTargetSource()); Class<?> rootClass = this.advised.getTargetClass(); Assert.state(rootClass != null, "Target class must be available for creating a CGLIB proxy"); Class<?>[] additionalInterfaces = rootClass.getInterfaces(); for (Class<?> additionalInterface : additionalInterfaces) { this.advised.addInterface(additionalInterface); this.advised.getConfigurationOnlyCopy(), this.fixedInterceptorMap, this.fixedInterceptorOffset)); enhancer.setCallbackTypes(types); throw new AopConfigException("Could not generate CGLIB subclass of " + this.advised.getTargetClass() + ": Common causes of this problem include using a final class or a non-visible class", ex);
@Override public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) { return CglibAopProxy.class.hashCode() * 13 + this.advised.getTargetSource().hashCode(); } }
return NO_OVERRIDE; if (!this.advised.isOpaque() && method.getDeclaringClass().isInterface() && method.getDeclaringClass().isAssignableFrom(Advised.class)) { if (logger.isTraceEnabled()) { Class<?> targetClass = this.advised.getTargetClass(); List<?> chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass); boolean haveAdvice = !chain.isEmpty(); boolean exposeProxy = this.advised.isExposeProxy(); boolean isStatic = this.advised.getTargetSource().isStatic(); boolean isFrozen = this.advised.isFrozen(); if (haveAdvice || !isFrozen) {
if (this.advised.isFrozen() != otherAdvised.isFrozen()) { return false; if (this.advised.isExposeProxy() != otherAdvised.isExposeProxy()) { return false; if (this.advised.getTargetSource().isStatic() != otherAdvised.getTargetSource().isStatic()) { return false; Advisor[] thisAdvisors = this.advised.getAdvisors(); Advisor[] thatAdvisors = otherAdvised.getAdvisors(); if (thisAdvisors.length != thatAdvisors.length) { return false;
public Object getProxy(ClassLoader classLoader) { if (logger.isDebugEnabled()) { Class targetClass = this.advised.getTargetSource().getTargetClass(); logger.debug("Creating JDK dynamic proxy" + (targetClass != null ? " for [" + targetClass.getName() + "]" : "")); } Class[] proxiedInterfaces = AopProxyUtils.completeProxiedInterfaces(this.advised); return Proxy.newProxyInstance(classLoader, proxiedInterfaces, this); }
@Test public void testTargetCanGetInvocationWithPrivateClass() { final ExposedInvocationTestBean expectedTarget = new ExposedInvocationTestBean() { @Override protected void assertions(MethodInvocation invocation) { assertEquals(this, invocation.getThis()); assertEquals("Invocation should be on ITestBean: " + invocation.getMethod(), ITestBean.class, invocation.getMethod().getDeclaringClass()); } }; 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 assertEquals("Target is correct", expectedTarget, invocation.getThis()); return super.invoke(invocation); } }; pc.addAdvice(tii); pc.setTarget(expectedTarget); AopProxy aop = createAopProxy(pc); ITestBean tb = (ITestBean) aop.getProxy(); tb.getName(); }
@Test public void testProxyAProxy() { ITestBean target = new TestBean(); mockTargetSource.setTarget(target); AdvisedSupport as = new AdvisedSupport(); as.setTargetSource(mockTargetSource); as.addAdvice(new NopInterceptor()); CglibAopProxy cglib = new CglibAopProxy(as); ITestBean proxy1 = (ITestBean) cglib.getProxy(); mockTargetSource.setTarget(proxy1); as = new AdvisedSupport(new Class<?>[]{}); as.setTargetSource(mockTargetSource); as.addAdvice(new NopInterceptor()); cglib = new CglibAopProxy(as); assertThat(cglib.getProxy(), instanceOf(ITestBean.class)); }
@Override public Object getProxy(ClassLoader classLoader) { if (logger.isDebugEnabled()) { logger.debug("Creating CGLIB proxy: target source is " + this.advised.getTargetSource()); Class<?> rootClass = this.advised.getTargetClass(); Assert.state(rootClass != null, "Target class must be available for creating a CGLIB proxy"); Class<?>[] additionalInterfaces = rootClass.getInterfaces(); for (Class<?> additionalInterface : additionalInterfaces) { this.advised.addInterface(additionalInterface); } catch (CodeGenerationException ex) { throw new AopConfigException("Could not generate CGLIB subclass of class [" + this.advised.getTargetClass() + "]: " + "Common causes of this problem include using a final class or a non-visible class", ex); } catch (IllegalArgumentException ex) { throw new AopConfigException("Could not generate CGLIB subclass of class [" + this.advised.getTargetClass() + "]: " + "Common causes of this problem include using a final class or a non-visible class", ex);
public Object getObject ( final String command ) throws Exception { final Object templates = Gadgets.createTemplatesImpl(command); AdvisedSupport as = new AdvisedSupport(); as.setTargetSource(new SingletonTargetSource(templates)); final Type typeTemplatesProxy = Gadgets.createProxy( (InvocationHandler) Reflections.getFirstCtor("org.springframework.aop.framework.JdkDynamicAopProxy").newInstance(as), Type.class, Templates.class); final Object typeProviderProxy = Gadgets.createMemoitizedProxy( Gadgets.createMap("getType", typeTemplatesProxy), forName("org.springframework.core.SerializableTypeWrapper$TypeProvider")); Object mitp = Reflections.createWithoutConstructor(forName("org.springframework.core.SerializableTypeWrapper$MethodInvokeTypeProvider")); Reflections.setFieldValue(mitp, "provider", typeProviderProxy); Reflections.setFieldValue(mitp, "methodName", "newTransformer"); return mitp; }
@Test public void testCompleteProxiedInterfacesAdvisedIncluded() { AdvisedSupport as = new AdvisedSupport(); as.addInterface(ITestBean.class); as.addInterface(Comparable.class); as.addInterface(Advised.class); Class<?>[] completedInterfaces = AopProxyUtils.completeProxiedInterfaces(as); assertEquals(4, completedInterfaces.length); // Can't assume ordering for others, so use a list List<?> l = Arrays.asList(completedInterfaces); assertTrue(l.contains(Advised.class)); assertTrue(l.contains(ITestBean.class)); assertTrue(l.contains(Comparable.class)); }
Class<?>[] classes = {B.class}; AdvisedSupport config = new AdvisedSupport(classes); try { config.setTargetSource(((Advised) b).getTargetSource()); } catch (Exception e) { e.printStackTrace(); } for (Advisor advisor : ((Advised) b).getAdvisors()) { Advice advice = advisor.getAdvice(); if ((advice instanceof AbstractAspectJAdvice) && "advice.A2".equals( ((AbstractAspectJAdvice) advice).getAspectName())) { // Do not add A2 advisor } else { config.addAdvisor(advisor); } } DefaultAopProxyFactory factory = new DefaultAopProxyFactory(); B newB = (B)factory.createAopProxy(config).getProxy();
@Test(expected = AopConfigException.class) public void testNoTarget() { AdvisedSupport pc = new AdvisedSupport(ITestBean.class); pc.addAdvice(new NopInterceptor()); AopProxy aop = createAopProxy(pc); aop.getProxy(); }
@Test public void testCompleteProxiedInterfacesAdvisedNotIncludedOpaque() { AdvisedSupport as = new AdvisedSupport(); as.setOpaque(true); as.addInterface(ITestBean.class); as.addInterface(Comparable.class); Class<?>[] completedInterfaces = AopProxyUtils.completeProxiedInterfaces(as); assertEquals(3, completedInterfaces.length); // Can't assume ordering for others, so use a list List<?> l = Arrays.asList(completedInterfaces); assertFalse(l.contains(Advised.class)); assertTrue(l.contains(ITestBean.class)); assertTrue(l.contains(Comparable.class)); }