/** * Add all {@link Advisor Advisors} from the supplied {@link MetadataAwareAspectInstanceFactory} * to the current chain. Exposes any special purpose {@link Advisor Advisors} if needed. * @see AspectJProxyUtils#makeAdvisorChainAspectJCapableIfNecessary(List) */ private void addAdvisorsFromAspectInstanceFactory(MetadataAwareAspectInstanceFactory instanceFactory) { List<Advisor> advisors = this.aspectFactory.getAdvisors(instanceFactory); Class<?> targetClass = getTargetClass(); Assert.state(targetClass != null, "Unresolvable target class"); advisors = AopUtils.findAdvisorsThatCanApply(advisors, targetClass); AspectJProxyUtils.makeAdvisorChainAspectJCapableIfNecessary(advisors); AnnotationAwareOrderComparator.sort(advisors); addAdvisors(advisors); }
private Advice instantiateAdvice(AspectJExpressionPointcut pointcut) { Advice advice = this.aspectJAdvisorFactory.getAdvice(this.aspectJAdviceMethod, pointcut, this.aspectInstanceFactory, this.declarationOrder, this.aspectName); return (advice != null ? advice : EMPTY_ADVICE); }
continue; if (this.advisorFactory.isAspect(beanType)) { aspectNames.add(beanName); AspectMetadata amd = new AspectMetadata(beanType, beanName); MetadataAwareAspectInstanceFactory factory = new BeanFactoryAspectInstanceFactory(this.beanFactory, beanName); List<Advisor> classAdvisors = this.advisorFactory.getAdvisors(factory); if (this.beanFactory.isSingleton(beanName)) { this.advisorsCache.put(beanName, classAdvisors); new PrototypeAspectInstanceFactory(this.beanFactory, beanName); this.aspectFactoryCache.put(beanName, factory); advisors.addAll(this.advisorFactory.getAdvisors(factory)); advisors.addAll(this.advisorFactory.getAdvisors(factory));
@Override protected boolean isInfrastructureClass(Class<?> beanClass) { // Previously we setProxyTargetClass(true) in the constructor, but that has too // broad an impact. Instead we now override isInfrastructureClass to avoid proxying // aspects. I'm not entirely happy with that as there is no good reason not // to advise aspects, except that it causes advice invocation to go through a // proxy, and if the aspect implements e.g the Ordered interface it will be // proxied by that interface and fail at runtime as the advice method is not // defined on the interface. We could potentially relax the restriction about // not advising aspects in the future. return (super.isInfrastructureClass(beanClass) || (this.aspectJAdvisorFactory != null && this.aspectJAdvisorFactory.isAspect(beanClass))); }
List<Advisor> advisors = new LinkedList<Advisor>(); for (Method method : bean.getClass().getMethods()) { Advisor advisor = aspectJAdvisorFactory.getAdvisor(method, factory, advisors.size(), "lockHardler"); if (advisor != null) { advisors.add(advisor);
@Override protected boolean isInfrastructureClass(Class<?> beanClass) { // Previously we setProxyTargetClass(true) in the constructor, but that has too // broad an impact. Instead we now override isInfrastructureClass to avoid proxying // aspects. I'm not entirely happy with that as there is no good reason not // to advise aspects, except that it causes advice invocation to go through a // proxy, and if the aspect implements e.g the Ordered interface it will be // proxied by that interface and fail at runtime as the advice method is not // defined on the interface. We could potentially relax the restriction about // not advising aspects in the future. return (super.isInfrastructureClass(beanClass) || this.aspectJAdvisorFactory.isAspect(beanClass)); }
@Test public void testIntroductionAdvisorExcludedFromTargetImplementingInterface() { assertTrue(AopUtils.findAdvisorsThatCanApply( getFixture().getAdvisors( new SingletonMetadataAwareAspectInstanceFactory(new MakeLockable(), "someBean")), CannotBeUnlocked.class).isEmpty()); assertEquals(2, AopUtils.findAdvisorsThatCanApply(getFixture().getAdvisors( new SingletonMetadataAwareAspectInstanceFactory(new MakeLockable(),"someBean")), NotLockable.class).size()); }
continue; if (this.advisorFactory.isAspect(beanType)) { aspectNames.add(beanName); AspectMetadata amd = new AspectMetadata(beanType, beanName); MetadataAwareAspectInstanceFactory factory = new BeanFactoryAspectInstanceFactory(this.beanFactory, beanName); List<Advisor> classAdvisors = this.advisorFactory.getAdvisors(factory); if (this.beanFactory.isSingleton(beanName)) { this.advisorsCache.put(beanName, classAdvisors); new PrototypeAspectInstanceFactory(this.beanFactory, beanName); this.aspectFactoryCache.put(beanName, factory); advisors.addAll(this.advisorFactory.getAdvisors(factory)); advisors.addAll(this.advisorFactory.getAdvisors(factory));
private Advice instantiateAdvice(AspectJExpressionPointcut pointcut) { Advice advice = this.aspectJAdvisorFactory.getAdvice(this.aspectJAdviceMethod, pointcut, this.aspectInstanceFactory, this.declarationOrder, this.aspectName); return (advice != null ? advice : EMPTY_ADVICE); }
@Override protected boolean isInfrastructureClass(Class<?> beanClass) { // Previously we setProxyTargetClass(true) in the constructor, but that has too // broad an impact. Instead we now override isInfrastructureClass to avoid proxying // aspects. I'm not entirely happy with that as there is no good reason not // to advise aspects, except that it causes advice invocation to go through a // proxy, and if the aspect implements e.g the Ordered interface it will be // proxied by that interface and fail at runtime as the advice method is not // defined on the interface. We could potentially relax the restriction about // not advising aspects in the future. return (super.isInfrastructureClass(beanClass) || (this.aspectJAdvisorFactory != null && this.aspectJAdvisorFactory.isAspect(beanClass))); }
@Test public void testIntroductionBasedOnAnnotationMatch_SPR5307() { AnnotatedTarget target = new AnnotatedTargetImpl(); List<Advisor> advisors = getFixture().getAdvisors( new SingletonMetadataAwareAspectInstanceFactory(new MakeAnnotatedTypeModifiable(), "someBean")); Object proxy = createProxy(target, advisors, AnnotatedTarget.class); System.out.println(advisors.get(1)); assertTrue(proxy instanceof Lockable); Lockable lockable = (Lockable)proxy; lockable.locked(); }
continue; if (this.advisorFactory.isAspect(beanType)) { aspectNames.add(beanName); AspectMetadata amd = new AspectMetadata(beanType, beanName); MetadataAwareAspectInstanceFactory factory = new BeanFactoryAspectInstanceFactory(this.beanFactory, beanName); List<Advisor> classAdvisors = this.advisorFactory.getAdvisors(factory); if (this.beanFactory.isSingleton(beanName)) { this.advisorsCache.put(beanName, classAdvisors); new PrototypeAspectInstanceFactory(this.beanFactory, beanName); this.aspectFactoryCache.put(beanName, factory); advisors.addAll(this.advisorFactory.getAdvisors(factory)); advisors.addAll(this.advisorFactory.getAdvisors(factory));
private Advice instantiateAdvice(AspectJExpressionPointcut pcut) { return this.aspectJAdvisorFactory.getAdvice(this.aspectJAdviceMethod, pcut, this.aspectInstanceFactory, this.declarationOrder, this.aspectName); }
@Test public void testRejectsPerCflowBelowAspect() { try { getFixture().getAdvisors( new SingletonMetadataAwareAspectInstanceFactory(new PerCflowBelowAspect(), "someBean")); fail("Cannot accept cflowbelow"); } catch (AopConfigException ex) { assertTrue(ex.getMessage().contains("PERCFLOWBELOW")); } }
@Test public void testRejectsPerCflowAspect() { try { getFixture().getAdvisors( new SingletonMetadataAwareAspectInstanceFactory(new PerCflowAspect(), "someBean")); fail("Cannot accept cflow"); } catch (AopConfigException ex) { assertTrue(ex.getMessage().contains("PERCFLOW")); } }
@Test public void testIntroductionOnTargetExcludedByTypePattern() { LinkedList<Object> target = new LinkedList<>(); List<?> proxy = (List<?>) createProxy(target, AopUtils.findAdvisorsThatCanApply( getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(new MakeLockable(), "someBean")), List.class ), List.class); assertFalse("Type pattern must have excluded mixin", proxy instanceof Lockable); }
@Test public void testMultiplePerTargetAspectsWithOrderAnnotation() throws SecurityException, NoSuchMethodException { TestBean target = new TestBean(); int realAge = 65; target.setAge(realAge); List<Advisor> advisors = new LinkedList<>(); PerTargetAspectWithOrderAnnotation10 aspect1 = new PerTargetAspectWithOrderAnnotation10(); aspect1.count = 100; advisors.addAll( getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(aspect1, "someBean1"))); PerTargetAspectWithOrderAnnotation5 aspect2 = new PerTargetAspectWithOrderAnnotation5(); advisors.addAll( getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(aspect2, "someBean2"))); Collections.sort(advisors, new OrderComparator()); TestBean itb = (TestBean) createProxy(target, advisors, TestBean.class); assertEquals("Around advice must NOT apply", realAge, itb.getAge()); // Hit the method in the per clause to instantiate the aspect itb.getSpouse(); assertEquals("Around advice must apply", 0, itb.getAge()); assertEquals("Around advice must apply", 1, itb.getAge()); }
@Test public void testAspectMethodThrowsExceptionLegalOnSignature() { TestBean target = new TestBean(); UnsupportedOperationException expectedException = new UnsupportedOperationException(); List<Advisor> advisors = getFixture().getAdvisors( new SingletonMetadataAwareAspectInstanceFactory(new ExceptionAspect(expectedException), "someBean")); assertEquals("One advice method was found", 1, advisors.size()); ITestBean itb = (ITestBean) createProxy(target, advisors, ITestBean.class); try { itb.getAge(); fail(); } catch (UnsupportedOperationException ex) { assertSame(expectedException, ex); } }
@Test(expected = IllegalArgumentException.class) public void testDeclarePrecedenceNotSupported() { TestBean target = new TestBean(); MetadataAwareAspectInstanceFactory aspectInstanceFactory = new SingletonMetadataAwareAspectInstanceFactory( new DeclarePrecedenceShouldSucceed(), "someBean"); createProxy(target, getFixture().getAdvisors(aspectInstanceFactory), ITestBean.class); }
@Test public void testBindingWithMultipleArgsDifferentlyOrdered() { ManyValuedArgs target = new ManyValuedArgs(); ManyValuedArgs mva = (ManyValuedArgs) createProxy(target, getFixture().getAdvisors( new SingletonMetadataAwareAspectInstanceFactory(new ManyValuedArgs(), "someBean")), ManyValuedArgs.class); String a = "a"; int b = 12; int c = 25; String d = "d"; StringBuffer e = new StringBuffer("stringbuf"); String expectedResult = a + b+ c + d + e; assertEquals(expectedResult, mva.mungeArgs(a, b, c, d, e)); }