/** * 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 = null; 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; }
@Override public void validate(Class<?> aspectClass) throws AopConfigException { // If the parent has the annotation and isn't abstract it's an error if (aspectClass.getSuperclass().getAnnotation(Aspect.class) != null && !Modifier.isAbstract(aspectClass.getSuperclass().getModifiers())) { throw new AopConfigException("[" + aspectClass.getName() + "] cannot extend concrete aspect [" + aspectClass.getSuperclass().getName() + "]"); } AjType<?> ajType = AjTypeSystem.getAjType(aspectClass); if (!ajType.isAspect()) { throw new NotAnAtAspectException(aspectClass); } if (ajType.getPerClause().getKind() == PerClauseKind.PERCFLOW) { throw new AopConfigException(aspectClass.getName() + " uses percflow instantiation model: " + "This is not supported in Spring AOP."); } if (ajType.getPerClause().getKind() == PerClauseKind.PERCFLOWBELOW) { throw new AopConfigException(aspectClass.getName() + " uses percflowbelow instantiation model: " + "This is not supported in Spring AOP."); } }
/** * 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 = null; 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; }
public void validate(Class<?> aspectClass) throws AopConfigException { // If the parent has the annotation and isn't abstract it's an error if (aspectClass.getSuperclass().getAnnotation(Aspect.class) != null && !Modifier.isAbstract(aspectClass.getSuperclass().getModifiers())) { throw new AopConfigException("[" + aspectClass.getName() + "] cannot extend concrete aspect [" + aspectClass.getSuperclass().getName() + "]"); } AjType<?> ajType = AjTypeSystem.getAjType(aspectClass); if (!ajType.isAspect()) { throw new NotAnAtAspectException(aspectClass); } if (ajType.getPerClause().getKind() == PerClauseKind.PERCFLOW) { throw new AopConfigException(aspectClass.getName() + " uses percflow instantiation model: " + "This is not supported in Spring AOP."); } if (ajType.getPerClause().getKind() == PerClauseKind.PERCFLOWBELOW) { throw new AopConfigException(aspectClass.getName() + " uses percflowbelow instantiation model: " + "This is not supported in Spring AOP."); } }
@Override public synchronized boolean addAspect(Object aspect) { if (super.addAspect(aspect)) { return true; } if (!aspect.getClass().isAnnotationPresent(Aspect.class)) { return false; } Class aspectClass = aspect.getClass(); String aspectName = aspectClass.getName(); AspectMetadata am = createAspectMetadata(aspectClass, aspectName); if (am == null) { return false; } if (am.getAjType().getPerClause().getKind() != PerClauseKind.SINGLETON) { throw new IllegalArgumentException( "Aspect class [" + aspectClass.getName() + "] does not define a singleton aspect"); } return addAdvisorsFromAspectInstanceFactory( new SingletonMetadataAwareAspectInstanceFactory(aspect, aspectName)); }
/** * 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 public void testSingletonAspect() { AspectMetadata am = new AspectMetadata(ExceptionAspect.class,"someBean"); assertFalse(am.isPerThisOrPerTarget()); assertSame(Pointcut.TRUE, am.getPerClausePointcut()); assertEquals(PerClauseKind.SINGLETON, am.getAjType().getPerClause().getKind()); }
@Test public void testPerTargetAspect() { AspectMetadata am = new AspectMetadata(PerTargetAspect.class,"someBean"); assertTrue(am.isPerThisOrPerTarget()); assertNotSame(Pointcut.TRUE, am.getPerClausePointcut()); assertEquals(PerClauseKind.PERTARGET, am.getAjType().getPerClause().getKind()); }
@Test public void testPerThisAspect() { AspectMetadata am = new AspectMetadata(PerThisAspect.class,"someBean"); assertTrue(am.isPerThisOrPerTarget()); assertNotSame(Pointcut.TRUE, am.getPerClausePointcut()); assertEquals(PerClauseKind.PERTHIS, am.getAjType().getPerClause().getKind()); }
/** * 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)); }
/** * Return whether the aspect is defined as "perthis" or "pertarget". */ public boolean isPerThisOrPerTarget() { PerClauseKind kind = getAjType().getPerClause().getKind(); return (kind == PerClauseKind.PERTARGET || kind == PerClauseKind.PERTHIS); }
/** * Return whether the aspect is defined as "pertypewithin". */ public boolean isPerTypeWithin() { PerClauseKind kind = getAjType().getPerClause().getKind(); return (kind == PerClauseKind.PERTYPEWITHIN); }
/** * This is only of interest for Spring AOP: AspectJ instantiation semantics * are much richer. In AspectJ terminology, all a return of {@code true} * means here is that the aspect is not a SINGLETON. */ @Override public boolean isPerInstance() { return (getAspectMetadata().getAjType().getPerClause().getKind() != PerClauseKind.SINGLETON); }
/** * This is only of interest for Spring AOP: AspectJ instantiation semantics * are much richer. In AspectJ terminology, all a return of {@code true} * means here is that the aspect is not a SINGLETON. */ public boolean isPerInstance() { return (getAspectMetadata().getAjType().getPerClause().getKind() != PerClauseKind.SINGLETON); }