Skip to content

Commit 9202c0a

Browse files
committed
Accept ajc-compiled @aspect classes for Spring AOP proxy usage
Closes gh-32793
1 parent 22b6d66 commit 9202c0a

File tree

2 files changed

+17
-32
lines changed

2 files changed

+17
-32
lines changed

spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/AbstractAspectJAdvisorFactory.java

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,7 +18,6 @@
1818

1919
import java.lang.annotation.Annotation;
2020
import java.lang.reflect.Constructor;
21-
import java.lang.reflect.Field;
2221
import java.lang.reflect.Method;
2322
import java.util.Map;
2423
import java.util.StringTokenizer;
@@ -56,8 +55,6 @@
5655
*/
5756
public abstract class AbstractAspectJAdvisorFactory implements AspectJAdvisorFactory {
5857

59-
private static final String AJC_MAGIC = "ajc$";
60-
6158
private static final Class<?>[] ASPECTJ_ANNOTATION_CLASSES = new Class<?>[] {
6259
Pointcut.class, Around.class, Before.class, After.class, AfterReturning.class, AfterThrowing.class};
6360

@@ -68,37 +65,11 @@ public abstract class AbstractAspectJAdvisorFactory implements AspectJAdvisorFac
6865
protected final ParameterNameDiscoverer parameterNameDiscoverer = new AspectJAnnotationParameterNameDiscoverer();
6966

7067

71-
/**
72-
* We consider something to be an AspectJ aspect suitable for use by the Spring AOP system
73-
* if it has the @Aspect annotation, and was not compiled by ajc. The reason for this latter test
74-
* is that aspects written in the code-style (AspectJ language) also have the annotation present
75-
* when compiled by ajc with the -1.5 flag, yet they cannot be consumed by Spring AOP.
76-
*/
7768
@Override
7869
public boolean isAspect(Class<?> clazz) {
79-
return (hasAspectAnnotation(clazz) && !compiledByAjc(clazz));
80-
}
81-
82-
private boolean hasAspectAnnotation(Class<?> clazz) {
8370
return (AnnotationUtils.findAnnotation(clazz, Aspect.class) != null);
8471
}
8572

86-
/**
87-
* We need to detect this as "code-style" AspectJ aspects should not be
88-
* interpreted by Spring AOP.
89-
*/
90-
static boolean compiledByAjc(Class<?> clazz) {
91-
// The AJTypeSystem goes to great lengths to provide a uniform appearance between code-style and
92-
// annotation-style aspects. Therefore there is no 'clean' way to tell them apart. Here we rely on
93-
// an implementation detail of the AspectJ compiler.
94-
for (Field field : clazz.getDeclaredFields()) {
95-
if (field.getName().startsWith(AJC_MAGIC)) {
96-
return true;
97-
}
98-
}
99-
return false;
100-
}
101-
10273
@Override
10374
public void validate(Class<?> aspectClass) throws AopConfigException {
10475
AjType<?> ajType = AjTypeSystem.getAjType(aspectClass);
@@ -115,6 +86,7 @@ public void validate(Class<?> aspectClass) throws AopConfigException {
11586
}
11687
}
11788

89+
11890
/**
11991
* Find and return the first AspectJ annotation on the given method
12092
* (there <i>should</i> only be one anyway...).

spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/AspectJAdvisorBeanRegistrationAotProcessor.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.aop.aspectj.annotation;
1818

19+
import java.lang.reflect.Field;
20+
1921
import org.springframework.aot.generate.GenerationContext;
2022
import org.springframework.aot.hint.MemberCategory;
2123
import org.springframework.beans.factory.aot.BeanRegistrationAotContribution;
@@ -34,6 +36,8 @@
3436
*/
3537
class AspectJAdvisorBeanRegistrationAotProcessor implements BeanRegistrationAotProcessor {
3638

39+
private static final String AJC_MAGIC = "ajc$";
40+
3741
private static final boolean aspectjPresent = ClassUtils.isPresent("org.aspectj.lang.annotation.Pointcut",
3842
AspectJAdvisorBeanRegistrationAotProcessor.class.getClassLoader());
3943

@@ -43,13 +47,22 @@ class AspectJAdvisorBeanRegistrationAotProcessor implements BeanRegistrationAotP
4347
public BeanRegistrationAotContribution processAheadOfTime(RegisteredBean registeredBean) {
4448
if (aspectjPresent) {
4549
Class<?> beanClass = registeredBean.getBeanClass();
46-
if (AbstractAspectJAdvisorFactory.compiledByAjc(beanClass)) {
50+
if (compiledByAjc(beanClass)) {
4751
return new AspectJAdvisorContribution(beanClass);
4852
}
4953
}
5054
return null;
5155
}
5256

57+
private static boolean compiledByAjc(Class<?> clazz) {
58+
for (Field field : clazz.getDeclaredFields()) {
59+
if (field.getName().startsWith(AJC_MAGIC)) {
60+
return true;
61+
}
62+
}
63+
return false;
64+
}
65+
5366

5467
private static class AspectJAdvisorContribution implements BeanRegistrationAotContribution {
5568

0 commit comments

Comments
 (0)