Java Code Examples for org.springframework.aop.framework.AopConfigException

The following examples show how to use org.springframework.aop.framework.AopConfigException. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@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.");
	}
}
 
Example 2
@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.");
	}
}
 
Example 3
@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.");
	}
}
 
Example 4
@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.");
	}
}
 
Example 5
/**
 * @param cacheInterceptor
 *          Advice that caches the returned values of intercepted methods.
 * @throws AopConfigException
 *           if the <code>CacheFlushAttributeSource</code> of
 *           <code>cacheInterceptor</code> is <code>null</code>.
 */
public FlushingAttributeSourceAdvisor(
    MetadataFlushingInterceptor cacheInterceptor) {

  super(cacheInterceptor);

  FlushingAttributeSource tempAttributeSource = cacheInterceptor
      .getFlushingAttributeSource();

  if (tempAttributeSource == null) {
    throw new AopConfigException(
        "Cannot construct a CacheFlushAttributeSourceAdvisor using a "
            + "CacheFlushInterceptor that has no CacheFlushAttributeSource configured");
  }

  cacheFlushAttributeSource = tempAttributeSource;
}
 
Example 6
private Object wrapExecutor(Object bean) {
	Executor executor = (Executor) bean;
	boolean methodFinal = anyFinalMethods(executor, Executor.class);
	boolean classFinal = Modifier.isFinal(bean.getClass().getModifiers());
	boolean cglibProxy = !methodFinal && !classFinal;
	try {
		return createProxy(bean, cglibProxy,
				new ExecutorMethodInterceptor<>(executor, this.beanFactory));
	}
	catch (AopConfigException ex) {
		if (cglibProxy) {
			if (log.isDebugEnabled()) {
				log.debug(
						"Exception occurred while trying to create a proxy, falling back to JDK proxy",
						ex);
			}
			return createProxy(bean, false,
					new ExecutorMethodInterceptor<>(executor, this.beanFactory));
		}
		throw ex;
	}
}
 
Example 7
@Test
public void should_fallback_to_default_implementation_when_exception_thrown()
		throws Exception {
	ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
	ExecutorBeanPostProcessor bpp = new ExecutorBeanPostProcessor(this.beanFactory) {

		@Override
		Object createProxy(Object bean, boolean cglibProxy, Advice advice) {
			throw new AopConfigException("foo");
		}

	};

	Object wrappedService = bpp.postProcessAfterInitialization(service, "foo");

	then(wrappedService).isInstanceOf(TraceableScheduledExecutorService.class);
	service.shutdown();
}
 
Example 8
Source Project: elasticactors   Source File: ExecutorBeanPostProcessor.java    License: Apache License 2.0 6 votes vote down vote up
private Object wrapExecutor(Object bean) {
    Executor executor = (Executor) bean;
    boolean methodFinal = anyFinalMethods(executor, Executor.class);
    boolean classFinal = Modifier.isFinal(bean.getClass().getModifiers());
    boolean cglibProxy = !methodFinal && !classFinal;
    try {
        return createProxy(bean, cglibProxy, new ExecutorMethodInterceptor<>(executor));
    } catch (AopConfigException ex) {
        if (cglibProxy) {
            if (log.isDebugEnabled()) {
                log.debug(
                        "Exception occurred while trying to create a proxy, falling back to "
                                + "JDK proxy",
                        ex);
            }
            return createProxy(bean, false, new ExecutorMethodInterceptor<>(executor));
        }
        throw ex;
    }
}
 
Example 9
@Test
public void testRejectsPerCflowAspect() {
	try {
		getFixture().getAdvisors(
				new SingletonMetadataAwareAspectInstanceFactory(new PerCflowAspect(), "someBean"));
		fail("Cannot accept cflow");
	}
	catch (AopConfigException ex) {
		assertTrue(ex.getMessage().contains("PERCFLOW"));
	}
}
 
Example 10
@Test
public void testRejectsPerCflowBelowAspect() {
	try {
		getFixture().getAdvisors(
				new SingletonMetadataAwareAspectInstanceFactory(new PerCflowBelowAspect(), "someBean"));
		fail("Cannot accept cflowbelow");
	}
	catch (AopConfigException ex) {
		assertTrue(ex.getMessage().contains("PERCFLOWBELOW"));
	}
}
 
Example 11
@Test
public void testRejectsPerCflowAspect() {
	try {
		getFixture().getAdvisors(
				new SingletonMetadataAwareAspectInstanceFactory(new PerCflowAspect(), "someBean"));
		fail("Cannot accept cflow");
	}
	catch (AopConfigException ex) {
		assertTrue(ex.getMessage().contains("PERCFLOW"));
	}
}
 
Example 12
@Test
public void testRejectsPerCflowBelowAspect() {
	try {
		getFixture().getAdvisors(
				new SingletonMetadataAwareAspectInstanceFactory(new PerCflowBelowAspect(), "someBean"));
		fail("Cannot accept cflowbelow");
	}
	catch (AopConfigException ex) {
		assertTrue(ex.getMessage().contains("PERCFLOWBELOW"));
	}
}
 
Example 13
@Test
public void testRejectsPerCflowAspect() {
	try {
		getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(new PerCflowAspect(),"someBean"));
		fail("Cannot accept cflow");
	}
	catch (AopConfigException ex) {
		assertTrue(ex.getMessage().indexOf("PERCFLOW") != -1);
	}
}
 
Example 14
@Test
public void testRejectsPerCflowBelowAspect() {
	try {
		getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(new PerCflowBelowAspect(),"someBean"));
		fail("Cannot accept cflowbelow");
	}
	catch (AopConfigException ex) {
		assertTrue(ex.getMessage().indexOf("PERCFLOWBELOW") != -1);
	}
}
 
Example 15
public CachingAttributeSourceAdvisor(MetadataCachingInterceptor i) {
  super(i);
  CachingAttributeSource source = i.getCachingAttributeSource();
  if (source == null)
    throw new AopConfigException("<" + i.getClass().getName() + "> has no <"
        + CachingAttributeSource.class.getName() + "> configured");
  cachingAttributeSource = source;
}
 
Example 16
/**
 * Construct a <code>CachingModelSourceAdvisor</code>.
 * 
 * @param interceptor
 *          Advice that caches the returned value of intercepted methods.
 * @throws AopConfigException
 *           if the <code>CachingAttributeSource</code> of
 *           <code>cacheInterceptor</code> is <code>null</code>.
 */
public CachingModelSourceAdvisor(NameMatchCachingInterceptor interceptor) {
  super(interceptor);

  CachingModelSource tempSource = interceptor.getCachingModelSource();

  if (tempSource == null) {
    throw new AopConfigException("<" + interceptor.getClass().getName()
        + "> has no <" + CachingModelSource.class.getName() + "> configured");
  }

  cachingModelSource = tempSource;
}
 
Example 17
/**
 * @param interceptor
 *          Advice that caches the returned value of intercepted methods.
 * @throws AopConfigException
 *           if the <code>FlushingAttributeSource</code> of
 *           <code>cacheInterceptor</code> is <code>null</code>.
 */
public FlushingModelSourceAdvisor(NameMatchFlushingInterceptor interceptor) {
  super(interceptor);

  FlushingModelSource tempSource = interceptor.getFlushingModelSource();

  if (tempSource == null) {
    throw new AopConfigException("<" + interceptor.getClass().getName()
        + "> has no <" + FlushingModelSource.class.getName() + "> configured");
  }

  flushingModelSource = tempSource;
}
 
Example 18
/**
 * Creates the proxy for target object. This method is invoked by a
 * BeanFactory after it has set all bean properties supplied.
 * 
 * @throws IllegalStateException
 *           if target is <code>null</code>.
 * @throws AopConfigException
 *           if the proxy interfaces or proxyTargetClass are not set and the
 *           target type is <code>org.springframework.aop.TargetSource</code>.
 */
public void afterPropertiesSet() throws IllegalStateException,
    AopConfigException {
  cachingInterceptor.afterPropertiesSet();
  flushingInterceptor.afterPropertiesSet();

  if (target == null) {
    throw new IllegalStateException("Property 'target' is required");
  }

  ProxyFactory proxyFactory = new ProxyFactory();
  proxyFactory.addAdvisor(new CachingModelSourceAdvisor(cachingInterceptor));

  if (hasFlushingModels) {
    proxyFactory.addAdvisor(new FlushingModelSourceAdvisor(
        flushingInterceptor));
  }

  proxyFactory.copyFrom(this);

  TargetSource targetSource = createTargetSource(target);
  proxyFactory.setTargetSource(targetSource);

  if (proxyInterfaces != null) {
    proxyFactory.setInterfaces(proxyInterfaces);
  } else if (!isProxyTargetClass()) {
    if (target instanceof TargetSource) {
      throw new AopConfigException(
          "Either 'proxyInterfaces' or 'proxyTargetClass' is required "
              + "when using a TargetSource as 'target'");
    }

    // rely on AOP infrastructure to tell us what interfaces to proxy
    proxyFactory.setInterfaces(ClassUtils.getAllInterfaces(target));
  }

  proxy = proxyFactory.getProxy();
}
 
Example 19
@Test
public void should_throw_exception_when_it_is_not_possible_to_create_any_proxy_for_ThreadPoolTaskExecutor()
		throws Exception {
	ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
	ExecutorBeanPostProcessor bpp = new ExecutorBeanPostProcessor(this.beanFactory) {
		@Override
		Object createThreadPoolTaskExecutorProxy(Object bean, boolean cglibProxy,
				ThreadPoolTaskExecutor executor) {
			throw new AopConfigException("foo");
		}
	};

	thenThrownBy(() -> bpp.postProcessAfterInitialization(taskExecutor, "foo"))
			.isInstanceOf(AopConfigException.class).hasMessage("foo");
}
 
Example 20
@Test
public void should_fallback_to_sleuth_impl_when_it_is_not_possible_to_create_any_proxy_for_ExecutorService()
		throws Exception {
	ExecutorService service = BDDMockito.mock(ExecutorService.class);
	ExecutorBeanPostProcessor bpp = new ExecutorBeanPostProcessor(this.beanFactory) {
		@Override
		Object getObject(ProxyFactoryBean factory) {
			throw new AopConfigException("foo");
		}
	};

	Object o = bpp.postProcessAfterInitialization(service, "foo");

	then(o).isInstanceOf(TraceableExecutorService.class);
}
 
Example 21
Source Project: spring-analysis-note   Source File: AspectMetadata.java    License: MIT License 4 votes vote down vote up
/**
 * Create a new AspectMetadata instance for the given aspect class.
 * @param aspectClass the aspect class
 * @param aspectName the name of the aspect
 */
public AspectMetadata(Class<?> aspectClass, String aspectName) {
	this.aspectName = aspectName;

	Class<?> currClass = aspectClass;
	AjType<?> ajType = null;
	while (currClass != Object.class) {
		AjType<?> ajTypeToCheck = AjTypeSystem.getAjType(currClass);
		if (ajTypeToCheck.isAspect()) {
			ajType = ajTypeToCheck;
			break;
		}
		currClass = currClass.getSuperclass();
	}
	if (ajType == null) {
		throw new IllegalArgumentException("Class '" + aspectClass.getName() + "' is not an @AspectJ aspect");
	}
	if (ajType.getDeclarePrecedence().length > 0) {
		throw new IllegalArgumentException("DeclarePrecendence not presently supported in Spring AOP");
	}
	this.aspectClass = ajType.getJavaClass();
	this.ajType = ajType;

	switch (this.ajType.getPerClause().getKind()) {
		case SINGLETON:
			this.perClausePointcut = Pointcut.TRUE;
			return;
		case PERTARGET:
		case PERTHIS:
			AspectJExpressionPointcut ajexp = new AspectJExpressionPointcut();
			ajexp.setLocation(aspectClass.getName());
			ajexp.setExpression(findPerClause(aspectClass));
			ajexp.setPointcutDeclarationScope(aspectClass);
			this.perClausePointcut = ajexp;
			return;
		case PERTYPEWITHIN:
			// Works with a type pattern
			this.perClausePointcut = new ComposablePointcut(new TypePatternClassFilter(findPerClause(aspectClass)));
			return;
		default:
			throw new AopConfigException(
					"PerClause " + ajType.getPerClause().getKind() + " not supported by Spring AOP for " + aspectClass);
	}
}
 
Example 22
Source Project: spring-analysis-note   Source File: AspectJPointcutAdvisorTests.java    License: MIT License 4 votes vote down vote up
@Test(expected = AopConfigException.class)
public void testPerCflowTarget() {
	testIllegalInstantiationModel(AbstractAspectJAdvisorFactoryTests.PerCflowAspect.class);
}
 
Example 23
Source Project: spring-analysis-note   Source File: AspectJPointcutAdvisorTests.java    License: MIT License 4 votes vote down vote up
@Test(expected = AopConfigException.class)
public void testPerCflowBelowTarget() {
	testIllegalInstantiationModel(AbstractAspectJAdvisorFactoryTests.PerCflowBelowAspect.class);
}
 
Example 24
Source Project: spring-analysis-note   Source File: AspectJPointcutAdvisorTests.java    License: MIT License 4 votes vote down vote up
private void testIllegalInstantiationModel(Class<?> c) throws AopConfigException {
	new AspectMetadata(c, "someBean");
}
 
Example 25
Source Project: java-technology-stack   Source File: AspectMetadata.java    License: MIT License 4 votes vote down vote up
/**
 * Create a new AspectMetadata instance for the given aspect class.
 * @param aspectClass the aspect class
 * @param aspectName the name of the aspect
 */
public AspectMetadata(Class<?> aspectClass, String aspectName) {
	this.aspectName = aspectName;

	Class<?> currClass = aspectClass;
	AjType<?> ajType = null;
	while (currClass != Object.class) {
		AjType<?> ajTypeToCheck = AjTypeSystem.getAjType(currClass);
		if (ajTypeToCheck.isAspect()) {
			ajType = ajTypeToCheck;
			break;
		}
		currClass = currClass.getSuperclass();
	}
	if (ajType == null) {
		throw new IllegalArgumentException("Class '" + aspectClass.getName() + "' is not an @AspectJ aspect");
	}
	if (ajType.getDeclarePrecedence().length > 0) {
		throw new IllegalArgumentException("DeclarePrecendence not presently supported in Spring AOP");
	}
	this.aspectClass = ajType.getJavaClass();
	this.ajType = ajType;

	switch (this.ajType.getPerClause().getKind()) {
		case SINGLETON:
			this.perClausePointcut = Pointcut.TRUE;
			return;
		case PERTARGET:
		case PERTHIS:
			AspectJExpressionPointcut ajexp = new AspectJExpressionPointcut();
			ajexp.setLocation(aspectClass.getName());
			ajexp.setExpression(findPerClause(aspectClass));
			ajexp.setPointcutDeclarationScope(aspectClass);
			this.perClausePointcut = ajexp;
			return;
		case PERTYPEWITHIN:
			// Works with a type pattern
			this.perClausePointcut = new ComposablePointcut(new TypePatternClassFilter(findPerClause(aspectClass)));
			return;
		default:
			throw new AopConfigException(
					"PerClause " + ajType.getPerClause().getKind() + " not supported by Spring AOP for " + aspectClass);
	}
}
 
Example 26
@Test(expected = AopConfigException.class)
public void testPerCflowTarget() {
	testIllegalInstantiationModel(AbstractAspectJAdvisorFactoryTests.PerCflowAspect.class);
}
 
Example 27
@Test(expected = AopConfigException.class)
public void testPerCflowBelowTarget() {
	testIllegalInstantiationModel(AbstractAspectJAdvisorFactoryTests.PerCflowBelowAspect.class);
}
 
Example 28
private void testIllegalInstantiationModel(Class<?> c) throws AopConfigException {
	new AspectMetadata(c, "someBean");
}
 
Example 29
Source Project: lams   Source File: AspectMetadata.java    License: GNU General Public License v2.0 4 votes vote down vote up
/**
 * Create a new AspectMetadata instance for the given aspect class.
 * @param aspectClass the aspect class
 * @param aspectName the name of the aspect
 */
public AspectMetadata(Class<?> aspectClass, String aspectName) {
	this.aspectName = aspectName;

	Class<?> currClass = aspectClass;
	AjType<?> ajType = null;
	while (currClass != Object.class) {
		AjType<?> ajTypeToCheck = AjTypeSystem.getAjType(currClass);
		if (ajTypeToCheck.isAspect()) {
			ajType = ajTypeToCheck;
			break;
		}
		currClass = currClass.getSuperclass();
	}
	if (ajType == null) {
		throw new IllegalArgumentException("Class '" + aspectClass.getName() + "' is not an @AspectJ aspect");
	}
	if (ajType.getDeclarePrecedence().length > 0) {
		throw new IllegalArgumentException("DeclarePrecendence not presently supported in Spring AOP");
	}
	this.aspectClass = ajType.getJavaClass();
	this.ajType = ajType;

	switch (this.ajType.getPerClause().getKind()) {
		case SINGLETON:
			this.perClausePointcut = Pointcut.TRUE;
			return;
		case PERTARGET:
		case PERTHIS:
			AspectJExpressionPointcut ajexp = new AspectJExpressionPointcut();
			ajexp.setLocation(aspectClass.getName());
			ajexp.setExpression(findPerClause(aspectClass));
			ajexp.setPointcutDeclarationScope(aspectClass);
			this.perClausePointcut = ajexp;
			return;
		case PERTYPEWITHIN:
			// Works with a type pattern
			this.perClausePointcut = new ComposablePointcut(new TypePatternClassFilter(findPerClause(aspectClass)));
			return;
		default:
			throw new AopConfigException(
					"PerClause " + ajType.getPerClause().getKind() + " not supported by Spring AOP for " + aspectClass);
	}
}
 
Example 30
Source Project: spring4-understanding   Source File: AspectMetadata.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Create a new AspectMetadata instance for the given aspect class.
 * @param aspectClass the aspect class
 * @param aspectName the name of the aspect
 */
public AspectMetadata(Class<?> aspectClass, String aspectName) {
	this.aspectName = aspectName;

	Class<?> currClass = aspectClass;
	AjType<?> ajType = null;
	while (currClass != Object.class) {
		AjType<?> ajTypeToCheck = AjTypeSystem.getAjType(currClass);
		if (ajTypeToCheck.isAspect()) {
			ajType = ajTypeToCheck;
			break;
		}
		currClass = currClass.getSuperclass();
	}
	if (ajType == null) {
		throw new IllegalArgumentException("Class '" + aspectClass.getName() + "' is not an @AspectJ aspect");
	}
	this.ajType = ajType;
	if (this.ajType.getDeclarePrecedence().length > 0) {
		throw new IllegalArgumentException("DeclarePrecendence not presently supported in Spring AOP");
	}

	switch (this.ajType.getPerClause().getKind()) {
		case SINGLETON :
			this.perClausePointcut = Pointcut.TRUE;
			return;
		case PERTARGET : case PERTHIS :
			AspectJExpressionPointcut ajexp = new AspectJExpressionPointcut();
			ajexp.setLocation("@Aspect annotation on " + aspectClass.getName());
			ajexp.setExpression(findPerClause(aspectClass));
			this.perClausePointcut = ajexp;
			return;
		case PERTYPEWITHIN :
			// Works with a type pattern
			this.perClausePointcut = new ComposablePointcut(new TypePatternClassFilter(findPerClause(aspectClass)));
			return;
		default :
			throw new AopConfigException(
					"PerClause " + ajType.getPerClause().getKind() + " not supported by Spring AOP for " + aspectClass);
	}
}