From c3b397c7d04c940220e4dbdd4eb50c9e05e1eb61 Mon Sep 17 00:00:00 2001 From: academey Date: Fri, 18 Jul 2025 17:26:36 +0900 Subject: [PATCH 1/2] Fix GraalVM native compilation issue with Java 22 This commit resolves the native compilation failure when using Java 22 with GraalVM. The issue was caused by SLF4J service providers being initialized at runtime instead of build time. Changes: - Add SLF4J RuntimeHints to SpringAiCoreRuntimeHints for native compilation - Register NOP_FallbackServiceProvider and SubstituteServiceProvider for build-time initialization - Add comprehensive tests for all registered types including SLF4J This fix ensures compatibility with both Java 21 and Java 22 for native image compilation. Fixes #494 Signed-off-by: academey --- .../ai/aot/SpringAiCoreRuntimeHints.java | 13 +++ .../ai/aot/SpringAiCoreRuntimeHintsTests.java | 98 +++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 spring-ai-model/src/test/java/org/springframework/ai/aot/SpringAiCoreRuntimeHintsTests.java diff --git a/spring-ai-model/src/main/java/org/springframework/ai/aot/SpringAiCoreRuntimeHints.java b/spring-ai-model/src/main/java/org/springframework/ai/aot/SpringAiCoreRuntimeHints.java index b7b93ac4757..b3fc05ab5be 100644 --- a/spring-ai-model/src/main/java/org/springframework/ai/aot/SpringAiCoreRuntimeHints.java +++ b/spring-ai-model/src/main/java/org/springframework/ai/aot/SpringAiCoreRuntimeHints.java @@ -18,6 +18,9 @@ import java.util.Set; +import org.slf4j.LoggerFactory; +import org.slf4j.helpers.NOP_FallbackServiceProvider; +import org.slf4j.helpers.SubstituteServiceProvider; import org.springframework.ai.chat.messages.AbstractMessage; import org.springframework.ai.chat.messages.AssistantMessage; import org.springframework.ai.chat.messages.Message; @@ -27,8 +30,10 @@ import org.springframework.ai.chat.messages.UserMessage; import org.springframework.ai.tool.ToolCallback; import org.springframework.ai.tool.definition.ToolDefinition; +import org.springframework.aot.hint.MemberCategory; import org.springframework.aot.hint.RuntimeHints; import org.springframework.aot.hint.RuntimeHintsRegistrar; +import org.springframework.aot.hint.TypeReference; import org.springframework.core.io.ClassPathResource; import org.springframework.lang.NonNull; import org.springframework.lang.Nullable; @@ -54,6 +59,14 @@ public void registerHints(@NonNull RuntimeHints hints, @Nullable ClassLoader cla hints.resources().registerResource(new ClassPathResource(r)); } + // Register SLF4J types for Java 22 native compilation compatibility + var slf4jTypes = Set.of(NOP_FallbackServiceProvider.class, SubstituteServiceProvider.class, + LoggerFactory.class); + for (var c : slf4jTypes) { + hints.reflection().registerType(TypeReference.of(c), MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS, + MemberCategory.INVOKE_PUBLIC_METHODS, MemberCategory.DECLARED_FIELDS); + } + } } diff --git a/spring-ai-model/src/test/java/org/springframework/ai/aot/SpringAiCoreRuntimeHintsTests.java b/spring-ai-model/src/test/java/org/springframework/ai/aot/SpringAiCoreRuntimeHintsTests.java new file mode 100644 index 00000000000..803790784e7 --- /dev/null +++ b/spring-ai-model/src/test/java/org/springframework/ai/aot/SpringAiCoreRuntimeHintsTests.java @@ -0,0 +1,98 @@ +/* + * Copyright 2023-2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.ai.aot; + +import org.junit.jupiter.api.Test; +import org.slf4j.LoggerFactory; +import org.slf4j.helpers.NOP_FallbackServiceProvider; +import org.slf4j.helpers.SubstituteServiceProvider; +import org.springframework.ai.chat.messages.AbstractMessage; +import org.springframework.ai.chat.messages.AssistantMessage; +import org.springframework.ai.chat.messages.Message; +import org.springframework.ai.chat.messages.MessageType; +import org.springframework.ai.chat.messages.SystemMessage; +import org.springframework.ai.chat.messages.ToolResponseMessage; +import org.springframework.ai.chat.messages.UserMessage; +import org.springframework.ai.tool.ToolCallback; +import org.springframework.ai.tool.definition.ToolDefinition; +import org.springframework.aot.hint.RuntimeHints; +import org.springframework.aot.hint.TypeReference; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link SpringAiCoreRuntimeHints}. + * + * @author Christian Tzolov + * @author Hyunjoon Park + */ +class SpringAiCoreRuntimeHintsTests { + + @Test + void registerHints() { + RuntimeHints runtimeHints = new RuntimeHints(); + SpringAiCoreRuntimeHints springAiCoreRuntimeHints = new SpringAiCoreRuntimeHints(); + springAiCoreRuntimeHints.registerHints(runtimeHints, null); + + // Verify chat message types are registered + assertThat(runtimeHints.reflection().typeHints()) + .anySatisfy(typeHint -> assertThat(typeHint.getType()) + .isEqualTo(TypeReference.of(AbstractMessage.class))); + assertThat(runtimeHints.reflection().typeHints()) + .anySatisfy(typeHint -> assertThat(typeHint.getType()) + .isEqualTo(TypeReference.of(AssistantMessage.class))); + assertThat(runtimeHints.reflection().typeHints()) + .anySatisfy(typeHint -> assertThat(typeHint.getType()) + .isEqualTo(TypeReference.of(Message.class))); + assertThat(runtimeHints.reflection().typeHints()) + .anySatisfy(typeHint -> assertThat(typeHint.getType()) + .isEqualTo(TypeReference.of(MessageType.class))); + assertThat(runtimeHints.reflection().typeHints()) + .anySatisfy(typeHint -> assertThat(typeHint.getType()) + .isEqualTo(TypeReference.of(SystemMessage.class))); + assertThat(runtimeHints.reflection().typeHints()) + .anySatisfy(typeHint -> assertThat(typeHint.getType()) + .isEqualTo(TypeReference.of(ToolResponseMessage.class))); + assertThat(runtimeHints.reflection().typeHints()) + .anySatisfy(typeHint -> assertThat(typeHint.getType()) + .isEqualTo(TypeReference.of(UserMessage.class))); + + // Verify tool types are registered + assertThat(runtimeHints.reflection().typeHints()) + .anySatisfy(typeHint -> assertThat(typeHint.getType()) + .isEqualTo(TypeReference.of(ToolCallback.class))); + assertThat(runtimeHints.reflection().typeHints()) + .anySatisfy(typeHint -> assertThat(typeHint.getType()) + .isEqualTo(TypeReference.of(ToolDefinition.class))); + + // Verify SLF4J types are registered for Java 22 compatibility + assertThat(runtimeHints.reflection().typeHints()) + .anySatisfy(typeHint -> assertThat(typeHint.getType()) + .isEqualTo(TypeReference.of(NOP_FallbackServiceProvider.class))); + assertThat(runtimeHints.reflection().typeHints()) + .anySatisfy(typeHint -> assertThat(typeHint.getType()) + .isEqualTo(TypeReference.of(SubstituteServiceProvider.class))); + assertThat(runtimeHints.reflection().typeHints()) + .anySatisfy(typeHint -> assertThat(typeHint.getType()) + .isEqualTo(TypeReference.of(LoggerFactory.class))); + + // Verify resources are registered + assertThat(runtimeHints.resources().resourcePatternHints()) + .anySatisfy(hint -> assertThat(hint.getIncludes()) + .anyMatch(include -> include.getPattern().contains("embedding-model-dimensions.properties"))); + } +} \ No newline at end of file From e2b28703db59833e27c5be2b3a547585fd09fa9c Mon Sep 17 00:00:00 2001 From: academey Date: Fri, 18 Jul 2025 11:27:29 +0900 Subject: [PATCH 2/2] Update copyright year and author in SpringAiCoreRuntimeHintsTests - Update copyright year from 2023-2024 to 2023-2025 - Remove @author Christian Tzolov as per contributor guidelines Signed-off-by: academey --- .../springframework/ai/aot/SpringAiCoreRuntimeHintsTests.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/spring-ai-model/src/test/java/org/springframework/ai/aot/SpringAiCoreRuntimeHintsTests.java b/spring-ai-model/src/test/java/org/springframework/ai/aot/SpringAiCoreRuntimeHintsTests.java index 803790784e7..5ceef08c794 100644 --- a/spring-ai-model/src/test/java/org/springframework/ai/aot/SpringAiCoreRuntimeHintsTests.java +++ b/spring-ai-model/src/test/java/org/springframework/ai/aot/SpringAiCoreRuntimeHintsTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2023-2024 the original author or authors. + * Copyright 2023-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -37,7 +37,6 @@ /** * Tests for {@link SpringAiCoreRuntimeHints}. * - * @author Christian Tzolov * @author Hyunjoon Park */ class SpringAiCoreRuntimeHintsTests {