@Test(expected = IllegalArgumentException.class) public void testBindingInPointcutUsedByAdvice() { TestBean tb = new TestBean(); AspectJProxyFactory proxyFactory = new AspectJProxyFactory(tb); proxyFactory.addAspect(NamedPointcutWithArgs.class); ITestBean proxiedTestBean = proxyFactory.getProxy(); proxiedTestBean.setName("Supercalifragalisticexpialidocious"); }
@Test public void testProgrammaticProxyCreation() { ITestBean testBean = new TestBean(); AspectJProxyFactory factory = new AspectJProxyFactory(); factory.setTarget(testBean); CounterAspect myCounterAspect = new CounterAspect(); factory.addAspect(myCounterAspect); ITestBean proxyTestBean = factory.getProxy(); assertTrue("Expected a proxy", proxyTestBean instanceof Advised); proxyTestBean.setAge(20); assertEquals("Programmatically created proxy shouldn't match bean()", 0, myCounterAspect.count); }
/** * 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); }
/** * Add an aspect of the supplied type to the end of the advice chain. * @param aspectClass the AspectJ aspect class */ public void addAspect(Class<?> aspectClass) { String aspectName = aspectClass.getName(); AspectMetadata am = createAspectMetadata(aspectClass, aspectName); MetadataAwareAspectInstanceFactory instanceFactory = createAspectInstanceFactory(am, aspectClass, aspectName); addAdvisorsFromAspectInstanceFactory(instanceFactory); }
/** * Create a new AspectJProxyFactory. * <p>Will proxy all interfaces that the given target implements. * @param target the target object to be proxied */ public AspectJProxyFactory(Object target) { Assert.notNull(target, "Target object must not be null"); setInterfaces(ClassUtils.getAllInterfaces(target)); setTarget(target); }
/** * Add the supplied aspect instance to the chain. The type of the aspect instance * supplied must be a singleton aspect. True singleton lifecycle is not honoured when * using this method - the caller is responsible for managing the lifecycle of any * aspects added in this way. * @param aspectInstance the AspectJ aspect instance */ public void addAspect(Object aspectInstance) { Class<?> aspectClass = aspectInstance.getClass(); String aspectName = aspectClass.getName(); AspectMetadata am = createAspectMetadata(aspectClass, aspectName); if (am.getAjType().getPerClause().getKind() != PerClauseKind.SINGLETON) { throw new IllegalArgumentException( "Aspect class [" + aspectClass.getName() + "] does not define a singleton aspect"); } addAdvisorsFromAspectInstanceFactory( new SingletonMetadataAwareAspectInstanceFactory(aspectInstance, aspectName)); }
@Test(expected = IllegalArgumentException.class) public void testWithNonSingletonAspectInstance() throws Exception { AspectJProxyFactory pf = new AspectJProxyFactory(); pf.addAspect(new PerThisAspect()); }
public Object applyAspect(ProceedingJoinPoint pjp) throws Throwable { final Object result = pjp.proceed(); if (result == null) { return result; } final AspectJProxyFactory portletPreferencesProxyFactory = new AspectJProxyFactory(result); for (final Advice advice : this.advices) { portletPreferencesProxyFactory.addAdvice(advice); } return portletPreferencesProxyFactory.getProxy(); } }
/** * Create a new proxy according to the settings in this factory. * <p>Can be called repeatedly. Effect will vary if we've added * or removed interfaces. Can add and remove interceptors. * <p>Uses a default class loader: Usually, the thread context class loader * (if necessary for proxy creation). * @return the new proxy */ @SuppressWarnings("unchecked") public <T> T getProxy() { return (T) createAopProxy().getProxy(); }
/** * Create a {@link MetadataAwareAspectInstanceFactory} for the supplied aspect type. If the aspect type * has no per clause, then a {@link SingletonMetadataAwareAspectInstanceFactory} is returned, otherwise * a {@link PrototypeAspectInstanceFactory} is returned. */ private MetadataAwareAspectInstanceFactory createAspectInstanceFactory( AspectMetadata am, Class<?> aspectClass, String aspectName) { MetadataAwareAspectInstanceFactory instanceFactory; if (am.getAjType().getPerClause().getKind() == PerClauseKind.SINGLETON) { // Create a shared aspect instance. Object instance = getSingletonAspectInstance(aspectClass); instanceFactory = new SingletonMetadataAwareAspectInstanceFactory(instance, aspectName); } else { // Create a factory for independent aspect instances. instanceFactory = new SimpleMetadataAwareAspectInstanceFactory(aspectClass, aspectName); } return instanceFactory; }
@Test(expected = IllegalArgumentException.class) public void testWithInstanceWithNonAspect() throws Exception { AspectJProxyFactory pf = new AspectJProxyFactory(); pf.addAspect(new TestBean()); }
/** * Add an aspect of the supplied type to the end of the advice chain. * @param aspectClass the AspectJ aspect class */ public void addAspect(Class<?> aspectClass) { String aspectName = aspectClass.getName(); AspectMetadata am = createAspectMetadata(aspectClass, aspectName); MetadataAwareAspectInstanceFactory instanceFactory = createAspectInstanceFactory(am, aspectClass, aspectName); addAdvisorsFromAspectInstanceFactory(instanceFactory); }
/** * Create a new AspectJProxyFactory. * <p>Will proxy all interfaces that the given target implements. * @param target the target object to be proxied */ public AspectJProxyFactory(Object target) { Assert.notNull(target, "Target object must not be null"); setInterfaces(ClassUtils.getAllInterfaces(target)); setTarget(target); }
/** * Add the supplied aspect instance to the chain. The type of the aspect instance * supplied must be a singleton aspect. True singleton lifecycle is not honoured when * using this method - the caller is responsible for managing the lifecycle of any * aspects added in this way. * @param aspectInstance the AspectJ aspect instance */ public void addAspect(Object aspectInstance) { Class<?> aspectClass = aspectInstance.getClass(); String aspectName = aspectClass.getName(); AspectMetadata am = createAspectMetadata(aspectClass, aspectName); if (am.getAjType().getPerClause().getKind() != PerClauseKind.SINGLETON) { throw new IllegalArgumentException( "Aspect class [" + aspectClass.getName() + "] does not define a singleton aspect"); } addAdvisorsFromAspectInstanceFactory( new SingletonMetadataAwareAspectInstanceFactory(aspectInstance, aspectName)); }
/** * Create a new proxy according to the settings in this factory. * <p>Can be called repeatedly. Effect will vary if we've added * or removed interfaces. Can add and remove interceptors. * <p>Uses the given class loader (if necessary for proxy creation). * @param classLoader the class loader to create the proxy with * @return the new proxy */ @SuppressWarnings("unchecked") public <T> T getProxy(ClassLoader classLoader) { return (T) createAopProxy().getProxy(classLoader); }
/** * Create a {@link MetadataAwareAspectInstanceFactory} for the supplied aspect type. If the aspect type * has no per clause, then a {@link SingletonMetadataAwareAspectInstanceFactory} is returned, otherwise * a {@link PrototypeAspectInstanceFactory} is returned. */ private MetadataAwareAspectInstanceFactory createAspectInstanceFactory( AspectMetadata am, Class<?> aspectClass, String aspectName) { MetadataAwareAspectInstanceFactory instanceFactory; if (am.getAjType().getPerClause().getKind() == PerClauseKind.SINGLETON) { // Create a shared aspect instance. Object instance = getSingletonAspectInstance(aspectClass); instanceFactory = new SingletonMetadataAwareAspectInstanceFactory(instance, aspectName); } else { // Create a factory for independent aspect instances. instanceFactory = new SimpleMetadataAwareAspectInstanceFactory(aspectClass, aspectName); } return instanceFactory; }
@Test(expected = IllegalStateException.class) public void testAnnotationArgumentNameBinding() { TransactionalBean tb = new TransactionalBean(); AspectJProxyFactory proxyFactory = new AspectJProxyFactory(tb); proxyFactory.addAspect(PointcutWithAnnotationArgument.class); ITransactionalBean proxiedTestBean = proxyFactory.getProxy(); proxiedTestBean.doInTransaction(); }
class Example { final Foo foo; Example(Foo original) { AspectJProxyFactory factory = new AspectJProxyFactory(); factory.setTarget(original); factory.addAspect(FooAspect.class); foo = (Foo) factory.getProxy(); } @Aspect static class FooAspect { @Before("execution(Foo.doBar())") Object beforeDoBar() { // My own activity } }
@Test(expected = IllegalArgumentException.class) public void testWithNonAspect() { AspectJProxyFactory proxyFactory = new AspectJProxyFactory(new TestBean()); proxyFactory.addAspect(TestBean.class); }
/** * Add an aspect of the supplied type to the end of the advice chain. * @param aspectClass the AspectJ aspect class */ public void addAspect(Class<?> aspectClass) { String aspectName = aspectClass.getName(); AspectMetadata am = createAspectMetadata(aspectClass, aspectName); MetadataAwareAspectInstanceFactory instanceFactory = createAspectInstanceFactory(am, aspectClass, aspectName); addAdvisorsFromAspectInstanceFactory(instanceFactory); }