From 3d494890dbd90fd6e2f70ecd56c2ce8c1d7c0036 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Nicoll?= Date: Thu, 25 Apr 2024 15:06:34 +0200 Subject: [PATCH] Add reflection hints for field access --- .../com/example/demo/ReflectionController.java | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/example/demo/ReflectionController.java b/src/main/java/com/example/demo/ReflectionController.java index 752a493..8bfcb5e 100644 --- a/src/main/java/com/example/demo/ReflectionController.java +++ b/src/main/java/com/example/demo/ReflectionController.java @@ -1,12 +1,17 @@ package com.example.demo; -import org.springframework.aot.hint.annotation.Reflective; +import org.springframework.aot.hint.RuntimeHints; +import org.springframework.aot.hint.RuntimeHintsRegistrar; +import org.springframework.context.annotation.ImportRuntimeHints; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import java.util.Arrays; import java.util.stream.Collectors; +import com.example.demo.ReflectionController.ReflectionControllerHints; + @RestController +@ImportRuntimeHints(ReflectionControllerHints.class) public class ReflectionController { @GetMapping("/reflection") @@ -14,7 +19,6 @@ public String message() { return getMessage(); } - @Reflective private String getMessage() { try { String className = Arrays.asList("com", "example", "demo", "Message").stream().collect(Collectors.joining(".")); @@ -23,4 +27,13 @@ private String getMessage() { return "Got an error: " + e.getMessage(); } } + + static class ReflectionControllerHints implements RuntimeHintsRegistrar { + + @Override + public void registerHints(RuntimeHints hints, ClassLoader classLoader) { + hints.reflection().registerType(Message.class, type -> type.withField("MESSAGE")); + } + } + } \ No newline at end of file