Allow @Aspect classes to extend concrete @Aspect classes
Closes gh-29830
This commit is contained in:
parent
e4b25ab480
commit
c3d123fef7
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2022 the original author or authors.
|
* Copyright 2002-2023 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -20,7 +20,6 @@ import java.lang.annotation.Annotation;
|
||||||
import java.lang.reflect.Constructor;
|
import java.lang.reflect.Constructor;
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.lang.reflect.Modifier;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.StringTokenizer;
|
import java.util.StringTokenizer;
|
||||||
|
|
||||||
|
|
@ -52,6 +51,7 @@ import org.springframework.lang.Nullable;
|
||||||
* @author Rod Johnson
|
* @author Rod Johnson
|
||||||
* @author Adrian Colyer
|
* @author Adrian Colyer
|
||||||
* @author Juergen Hoeller
|
* @author Juergen Hoeller
|
||||||
|
* @author Sam Brannen
|
||||||
* @since 2.0
|
* @since 2.0
|
||||||
*/
|
*/
|
||||||
public abstract class AbstractAspectJAdvisorFactory implements AspectJAdvisorFactory {
|
public abstract class AbstractAspectJAdvisorFactory implements AspectJAdvisorFactory {
|
||||||
|
|
@ -101,14 +101,6 @@ public abstract class AbstractAspectJAdvisorFactory implements AspectJAdvisorFac
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void validate(Class<?> aspectClass) throws AopConfigException {
|
public void validate(Class<?> aspectClass) throws AopConfigException {
|
||||||
// If the parent has the annotation and isn't abstract it's an error
|
|
||||||
Class<?> superclass = aspectClass.getSuperclass();
|
|
||||||
if (superclass.getAnnotation(Aspect.class) != null &&
|
|
||||||
!Modifier.isAbstract(superclass.getModifiers())) {
|
|
||||||
throw new AopConfigException("[" + aspectClass.getName() + "] cannot extend concrete aspect [" +
|
|
||||||
superclass.getName() + "]");
|
|
||||||
}
|
|
||||||
|
|
||||||
AjType<?> ajType = AjTypeSystem.getAjType(aspectClass);
|
AjType<?> ajType = AjTypeSystem.getAjType(aspectClass);
|
||||||
if (!ajType.isAspect()) {
|
if (!ajType.isAspect()) {
|
||||||
throw new NotAnAtAspectException(aspectClass);
|
throw new NotAnAtAspectException(aspectClass);
|
||||||
|
|
|
||||||
|
|
@ -482,6 +482,19 @@ abstract class AbstractAspectJAdvisorFactoryTests {
|
||||||
assertThat(aspect.invocations).containsExactly("around - start", "before", "after throwing", "after", "around - end");
|
assertThat(aspect.invocations).containsExactly("around - start", "before", "after throwing", "after", "around - end");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void nonAbstractParentAspect() throws Exception {
|
||||||
|
IncrementingAspect aspect = new IncrementingAspect();
|
||||||
|
|
||||||
|
// Precondition:
|
||||||
|
assertThat(Modifier.isAbstract(aspect.getClass().getSuperclass().getModifiers())).isFalse();
|
||||||
|
|
||||||
|
List<Advisor> advisors = getAdvisorFactory().getAdvisors(aspectInstanceFactory(aspect, "incrementingAspect"));
|
||||||
|
|
||||||
|
ITestBean proxy = createProxy(new TestBean("Jane", 42), ITestBean.class, advisors);
|
||||||
|
assertThat(proxy.getAge()).isEqualTo(86); // (42 + 1) * 2
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void failureWithoutExplicitDeclarePrecedence() {
|
void failureWithoutExplicitDeclarePrecedence() {
|
||||||
TestBean target = new TestBean();
|
TestBean target = new TestBean();
|
||||||
|
|
@ -748,6 +761,26 @@ abstract class AbstractAspectJAdvisorFactoryTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Aspect
|
||||||
|
class DoublingAspect {
|
||||||
|
|
||||||
|
@Around("execution(* getAge())")
|
||||||
|
public Object doubleAge(ProceedingJoinPoint pjp) throws Throwable {
|
||||||
|
return ((int) pjp.proceed()) * 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Aspect
|
||||||
|
class IncrementingAspect extends DoublingAspect {
|
||||||
|
|
||||||
|
@Around("execution(* getAge())")
|
||||||
|
public int incrementAge(ProceedingJoinPoint pjp) throws Throwable {
|
||||||
|
return ((int) pjp.proceed()) + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Aspect
|
@Aspect
|
||||||
private static class InvocationTrackingAspect {
|
private static class InvocationTrackingAspect {
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue