Skip to content

Commit 92c3843

Browse files
committed
Add required AspectJ field hints
This commit adds reflection hints on fields for classes compiled by AspectJ. Closes gh-31575
1 parent e12269e commit 92c3843

File tree

4 files changed

+145
-2
lines changed

4 files changed

+145
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ private boolean hasAspectAnnotation(Class<?> clazz) {
8787
* We need to detect this as "code-style" AspectJ aspects should not be
8888
* interpreted by Spring AOP.
8989
*/
90-
private boolean compiledByAjc(Class<?> clazz) {
90+
static boolean compiledByAjc(Class<?> clazz) {
9191
// The AJTypeSystem goes to great lengths to provide a uniform appearance between code-style and
9292
// annotation-style aspects. Therefore there is no 'clean' way to tell them apart. Here we rely on
9393
// an implementation detail of the AspectJ compiler.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2002-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.aop.aspectj.annotation;
18+
19+
import org.springframework.aot.generate.GenerationContext;
20+
import org.springframework.aot.hint.MemberCategory;
21+
import org.springframework.beans.factory.aot.BeanRegistrationAotContribution;
22+
import org.springframework.beans.factory.aot.BeanRegistrationAotProcessor;
23+
import org.springframework.beans.factory.aot.BeanRegistrationCode;
24+
import org.springframework.beans.factory.support.RegisteredBean;
25+
26+
/**
27+
* An AOT {@link BeanRegistrationAotProcessor} that detects the presence of
28+
* classes compiled with AspectJ and adds the related required field hints.
29+
*
30+
* @author Sebastien Deleuze
31+
* @since 6.1
32+
*/
33+
public class AspectJAdvisorBeanRegistrationAotProcessor implements BeanRegistrationAotProcessor {
34+
35+
@Override
36+
public BeanRegistrationAotContribution processAheadOfTime(RegisteredBean registeredBean) {
37+
Class<?> beanClass = registeredBean.getBeanClass();
38+
if (AbstractAspectJAdvisorFactory.compiledByAjc(beanClass)) {
39+
return new AspectJAdvisorContribution(beanClass);
40+
}
41+
return null;
42+
}
43+
44+
private static class AspectJAdvisorContribution implements BeanRegistrationAotContribution {
45+
46+
private final Class<?> beanClass;
47+
48+
public AspectJAdvisorContribution(Class<?> beanClass) {
49+
this.beanClass = beanClass;
50+
}
51+
52+
@Override
53+
public void applyTo(GenerationContext generationContext, BeanRegistrationCode beanRegistrationCode) {
54+
generationContext.getRuntimeHints().reflection().registerType(this.beanClass, MemberCategory.DECLARED_FIELDS);
55+
}
56+
}
57+
58+
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
org.springframework.beans.factory.aot.BeanRegistrationAotProcessor=\
2-
org.springframework.aop.scope.ScopedProxyBeanRegistrationAotProcessor
2+
org.springframework.aop.scope.ScopedProxyBeanRegistrationAotProcessor,\
3+
org.springframework.aop.aspectj.annotation.AspectJAdvisorBeanRegistrationAotProcessor
34

45
org.springframework.beans.factory.aot.BeanFactoryInitializationAotProcessor= \
56
org.springframework.aop.aspectj.annotation.AspectJBeanFactoryInitializationAotProcessor
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright 2002-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.aop.aspectj;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.aop.aspectj.annotation.AspectJAdvisorBeanRegistrationAotProcessor;
22+
import org.springframework.aot.generate.GenerationContext;
23+
import org.springframework.aot.hint.MemberCategory;
24+
import org.springframework.aot.hint.RuntimeHints;
25+
import org.springframework.aot.test.generate.TestGenerationContext;
26+
import org.springframework.beans.factory.aot.BeanRegistrationAotContribution;
27+
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
28+
import org.springframework.beans.factory.support.RegisteredBean;
29+
import org.springframework.beans.factory.support.RootBeanDefinition;
30+
import org.springframework.lang.Nullable;
31+
32+
import static org.assertj.core.api.Assertions.assertThat;
33+
import static org.mockito.Mockito.mock;
34+
import static org.springframework.aot.hint.predicate.RuntimeHintsPredicates.reflection;
35+
36+
/**
37+
* Tests for {@link AspectJAdvisorBeanRegistrationAotProcessor}.
38+
*
39+
* @author Sebastien Deleuze
40+
*/
41+
public class AspectJAdvisorBeanRegistrationAotProcessorTests {
42+
43+
private final GenerationContext generationContext = new TestGenerationContext();
44+
45+
private final RuntimeHints runtimeHints = this.generationContext.getRuntimeHints();
46+
47+
@Test
48+
void shouldProcessesAspectJClass() {
49+
process(AspectJClass.class);
50+
assertThat(reflection().onType(AspectJClass.class).withMemberCategory(MemberCategory.DECLARED_FIELDS))
51+
.accepts(this.runtimeHints);
52+
}
53+
54+
@Test
55+
void shouldSkipRegularClass() {
56+
process(RegularClass.class);
57+
assertThat(this.runtimeHints.reflection().typeHints()).isEmpty();
58+
}
59+
60+
void process(Class<?> beanClass) {
61+
BeanRegistrationAotContribution contribution = createContribution(beanClass);
62+
if (contribution != null) {
63+
contribution.applyTo(this.generationContext, mock());
64+
}
65+
}
66+
67+
@Nullable
68+
private static BeanRegistrationAotContribution createContribution(Class<?> beanClass) {
69+
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
70+
beanFactory.registerBeanDefinition(beanClass.getName(), new RootBeanDefinition(beanClass));
71+
return new AspectJAdvisorBeanRegistrationAotProcessor()
72+
.processAheadOfTime(RegisteredBean.of(beanFactory, beanClass.getName()));
73+
}
74+
75+
76+
static class AspectJClass {
77+
private static java.lang.Throwable ajc$initFailureCause;
78+
}
79+
80+
static class RegularClass {
81+
private static java.lang.Throwable initFailureCause;
82+
}
83+
84+
}

0 commit comments

Comments
 (0)