Skip to content

Commit 270f3c0

Browse files
committed
🧩 @RegisterForReflection example on Quarkus
1 parent 9b3677b commit 270f3c0

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package me.nzuguem.quarkusnative;
2+
3+
import io.quarkus.runtime.annotations.RegisterForReflection;
4+
5+
@RegisterForReflection
6+
public class Hello {
7+
8+
// This piece of code must be evaluated during the initialisation phase at build-time
9+
// The consequence is that System.currentTimeMillis() will be frozen in the resulting native image: This shows the use cases to be avoided with native images
10+
public final static String MESSAGE = "Hello from Reflection - " + System.currentTimeMillis();
11+
12+
}

‎03-quarkus/src/main/java/me/nzuguem/quarkusnative/HelloController.java‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.io.BufferedReader;
1111
import java.io.IOException;
1212
import java.io.InputStreamReader;
13+
import java.util.List;
1314
import java.util.stream.Collectors;
1415

1516
import io.quarkus.logging.Log;
@@ -51,4 +52,25 @@ public Response hello() {
5152
return Response.serverError().build();
5253
}
5354
}
55+
56+
@GET
57+
@Path("from/reflection")
58+
@Produces(MediaType.TEXT_PLAIN)
59+
public Response helloFromReflection() {
60+
61+
try {
62+
var className = List.<String>of("me", "nzuguem", "quarkusnative", "Hello")
63+
.stream().collect(Collectors.joining("."));
64+
65+
var message = (String) Class.forName(className).getDeclaredField("MESSAGE").get(String.class);
66+
67+
return Response.ok(message).build();
68+
69+
} catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | ClassNotFoundException exception) {
70+
71+
Log.error(exception.getMessage(), exception);
72+
73+
return Response.serverError().build();
74+
}
75+
}
5476
}

0 commit comments

Comments
 (0)