Skip to content

Commit

Permalink
Merge pull request #4 from snicoll-scratches/hint-fix
Browse files Browse the repository at this point in the history
Add reflection hints for field access
  • Loading branch information
alina-yur authored Apr 25, 2024
2 parents feca82f + 3d49489 commit 507bd44
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/main/java/com/example/demo/ReflectionController.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
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")
public String message() {
return getMessage();
}

@Reflective
private String getMessage() {
try {
String className = Arrays.asList("com", "example", "demo", "Message").stream().collect(Collectors.joining("."));
Expand All @@ -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"));
}
}

}

0 comments on commit 507bd44

Please sign in to comment.