Example usage for org.aspectj.lang.reflect PerClauseKind SINGLETON

List of usage examples for org.aspectj.lang.reflect PerClauseKind SINGLETON

Introduction

In this page you can find the example usage for org.aspectj.lang.reflect PerClauseKind SINGLETON.

Prototype

PerClauseKind SINGLETON

To view the source code for org.aspectj.lang.reflect PerClauseKind SINGLETON.

Click Source Link

Usage

From source file:im.tym.wraop.impl.AspectJWrapperFactorySpi.java

License:Apache License

@Override
public synchronized boolean addAspect(Object aspect) {
    if (super.addAspect(aspect)) {
        return true;
    }//from w  ww . j  a va2 s .  co  m
    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));
}

From source file:org.springframework.aop.aspectj.annotation.AspectJProxyFactory.java

License:Apache License

/**
 * 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.//from w  w w.j a va2s.co  m
 * @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));
}

From source file:org.springframework.aop.aspectj.annotation.AspectJProxyFactory.java

License:Apache License

/**
 * 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.
 *//* www. ja va  2  s .  c  om*/
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;
}

From source file:org.springframework.aop.aspectj.annotation.AspectMetadataTests.java

License:Apache License

@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());
}

From source file:org.springframework.aop.aspectj.annotation.BeanFactoryAspectJAdvisorsBuilder.java

License:Apache License

/**
 * Look for AspectJ-annotated aspect beans in the current bean factory,
 * and return to a list of Spring AOP Advisors representing them.
 * <p>Creates a Spring Advisor for each AspectJ advice method.
 * @return the list of {@link org.springframework.aop.Advisor} beans
 * @see #isEligibleBean//from  ww w .  j  a  v  a  2s  . c  o  m
 */
public List<Advisor> buildAspectJAdvisors() {
    List<String> aspectNames = null;

    synchronized (this) {
        aspectNames = this.aspectBeanNames;
        if (aspectNames == null) {
            List<Advisor> advisors = new LinkedList<Advisor>();
            aspectNames = new LinkedList<String>();
            String[] beanNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(this.beanFactory,
                    Object.class, true, false);
            for (String beanName : beanNames) {
                if (!isEligibleBean(beanName)) {
                    continue;
                }
                // We must be careful not to instantiate beans eagerly as in this
                // case they would be cached by the Spring container but would not
                // have been weaved
                Class<?> beanType = this.beanFactory.getType(beanName);
                if (beanType == null) {
                    continue;
                }
                if (this.advisorFactory.isAspect(beanType)) {
                    aspectNames.add(beanName);
                    AspectMetadata amd = new AspectMetadata(beanType, beanName);
                    if (amd.getAjType().getPerClause().getKind() == PerClauseKind.SINGLETON) {
                        MetadataAwareAspectInstanceFactory factory = new BeanFactoryAspectInstanceFactory(
                                this.beanFactory, beanName);
                        List<Advisor> classAdvisors = this.advisorFactory.getAdvisors(factory);
                        if (this.beanFactory.isSingleton(beanName)) {
                            this.advisorsCache.put(beanName, classAdvisors);
                        } else {
                            this.aspectFactoryCache.put(beanName, factory);
                        }
                        advisors.addAll(classAdvisors);
                    } else {
                        // Per target or per this.
                        if (this.beanFactory.isSingleton(beanName)) {
                            throw new IllegalArgumentException("Bean with name '" + beanName
                                    + "' is a singleton, but aspect instantiation model is not singleton");
                        }
                        MetadataAwareAspectInstanceFactory factory = new PrototypeAspectInstanceFactory(
                                this.beanFactory, beanName);
                        this.aspectFactoryCache.put(beanName, factory);
                        advisors.addAll(this.advisorFactory.getAdvisors(factory));
                    }
                }
            }
            this.aspectBeanNames = aspectNames;
            return advisors;
        }
    }

    if (aspectNames.isEmpty()) {
        return Collections.emptyList();
    }
    List<Advisor> advisors = new LinkedList<Advisor>();
    for (String aspectName : aspectNames) {
        List<Advisor> cachedAdvisors = this.advisorsCache.get(aspectName);
        if (cachedAdvisors != null) {
            advisors.addAll(cachedAdvisors);
        } else {
            MetadataAwareAspectInstanceFactory factory = this.aspectFactoryCache.get(aspectName);
            advisors.addAll(this.advisorFactory.getAdvisors(factory));
        }
    }
    return advisors;
}

From source file:org.springframework.aop.aspectj.annotation.InstantiationModelAwarePointcutAdvisorImpl.java

License:Apache License

/**
 * 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.
 *///from ww w.  j av  a 2 s .c  o m
@Override
public boolean isPerInstance() {
    return (getAspectMetadata().getAjType().getPerClause().getKind() != PerClauseKind.SINGLETON);
}