Skip to content

Commit d59aa18

Browse files
committed
Introduce FailureAnalyzersCodeContributor
Due to a Spring Boot 2.7 change that force using reflection. See spring-atticgh-1541
1 parent db50c21 commit d59aa18

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

spring-aot/src/main/java/org/springframework/aot/factories/FactoriesCodeContributors.java

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class FactoriesCodeContributors {
4040
this.contributors = Arrays.asList(new IgnoredFactoriesCodeContributor(),
4141
new TestExecutionListenerFactoriesCodeContributor(),
4242
new TestAutoConfigurationFactoriesCodeContributor(aotOptions),
43+
new FailureAnalyzersCodeContributor(),
4344
new NoArgConstructorFactoriesCodeContributor(),
4445
new PrivateFactoriesCodeContributor(),
4546
new DefaultFactoriesCodeContributor(aotOptions));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.springframework.aot.factories;
2+
3+
import com.squareup.javapoet.ClassName;
4+
5+
import org.springframework.aot.build.context.BuildContext;
6+
import org.springframework.boot.diagnostics.FailureAnalyzer;
7+
import org.springframework.nativex.hint.TypeAccess;
8+
9+
/**
10+
* Contributor for {@code FailureAnalyzer} that need to be handled via reflection as of Spring Boot 2.7.
11+
*/
12+
public class FailureAnalyzersCodeContributor implements FactoriesCodeContributor {
13+
14+
@Override
15+
public boolean canContribute(SpringFactory springFactory) {
16+
return springFactory.getFactoryType() == FailureAnalyzer.class;
17+
}
18+
19+
@Override
20+
public void contribute(SpringFactory factory, CodeGenerator code, BuildContext context) {
21+
ClassName factoryTypeClass = ClassName.bestGuess(factory.getFactoryType().getCanonicalName());
22+
code.writeToStaticBlock(builder -> {
23+
builder.addStatement("names.add($T.class, $S)", factoryTypeClass,
24+
factory.getFactory().getCanonicalName());
25+
});
26+
generateReflectionMetadata(factory.getFactory().getName(), context);
27+
}
28+
29+
private void generateReflectionMetadata(String factoryClassName, BuildContext context) {
30+
org.springframework.nativex.domain.reflect.ClassDescriptor factoryDescriptor = org.springframework.nativex.domain.reflect.ClassDescriptor.of(factoryClassName);
31+
factoryDescriptor.setAccess(TypeAccess.DECLARED_CONSTRUCTORS);
32+
context.describeReflection(reflect -> reflect.add(factoryDescriptor));
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.springframework.aot.factories;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import org.springframework.aot.factories.fixtures.TestFactory;
6+
import org.springframework.boot.diagnostics.FailureAnalyzer;
7+
8+
import static org.assertj.core.api.Assertions.assertThat;
9+
10+
/**
11+
* @author Sebastien Deleuze
12+
*/
13+
public class FailureAnalyzersCodeContributorTests {
14+
15+
private final FailureAnalyzersCodeContributor factoriesCodeContributor = new FailureAnalyzersCodeContributor();
16+
17+
@Test
18+
void canContributeFactoryTypesToFailureAnalyzer() {
19+
assertThat(factoriesCodeContributor.canContribute(createSpringFactory(FailureAnalyzer.class, Object.class.getName()))).isTrue();
20+
}
21+
22+
@Test
23+
void cantContributeFactoryTypes() {
24+
assertThat(factoriesCodeContributor.canContribute(createSpringFactory(TestFactory.class, Object.class.getName()))).isFalse();
25+
}
26+
27+
private static SpringFactory createSpringFactory(Class<?> factoryType, String factoryImplementation) {
28+
return SpringFactory.resolve(factoryType.getName(), factoryImplementation, IgnoredFactoriesCodeContributorTests.class.getClassLoader());
29+
}
30+
}

0 commit comments

Comments
 (0)