From a9a252d1f13acc5adf61bf661c977064d2222847 Mon Sep 17 00:00:00 2001 From: Foivos Zakkak Date: Thu, 23 May 2024 14:41:24 +0300 Subject: [PATCH] Export reasons in reflect-config.json file --- .../quarkus/deployment/pkg/NativeConfig.java | 6 ++++ .../steps/NativeImageReflectConfigStep.java | 32 ++++++++++++++++--- .../deployment/pkg/TestNativeConfig.java | 5 +++ 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/core/deployment/src/main/java/io/quarkus/deployment/pkg/NativeConfig.java b/core/deployment/src/main/java/io/quarkus/deployment/pkg/NativeConfig.java index c4e4c476fbb431..e76f3882385110 100644 --- a/core/deployment/src/main/java/io/quarkus/deployment/pkg/NativeConfig.java +++ b/core/deployment/src/main/java/io/quarkus/deployment/pkg/NativeConfig.java @@ -486,6 +486,12 @@ interface Debug { @WithDefault("false") boolean enableDashboardDump(); + /** + * Include a reasons entries in the generated json configuration files. + */ + @WithDefault("false") + boolean includeReasonsInConfigFiles(); + /** * Configure native executable compression using UPX. */ diff --git a/core/deployment/src/main/java/io/quarkus/deployment/steps/NativeImageReflectConfigStep.java b/core/deployment/src/main/java/io/quarkus/deployment/steps/NativeImageReflectConfigStep.java index 3adc5ce47502c3..dba30328368411 100644 --- a/core/deployment/src/main/java/io/quarkus/deployment/steps/NativeImageReflectConfigStep.java +++ b/core/deployment/src/main/java/io/quarkus/deployment/steps/NativeImageReflectConfigStep.java @@ -3,6 +3,7 @@ import java.io.IOException; import java.io.StringWriter; import java.nio.charset.StandardCharsets; +import java.util.ArrayList; import java.util.HashSet; import java.util.LinkedHashMap; import java.util.List; @@ -21,12 +22,14 @@ import io.quarkus.deployment.builditem.nativeimage.ReflectiveFieldBuildItem; import io.quarkus.deployment.builditem.nativeimage.ReflectiveMethodBuildItem; import io.quarkus.deployment.builditem.nativeimage.ServiceProviderBuildItem; +import io.quarkus.deployment.pkg.NativeConfig; import io.quarkus.deployment.pkg.steps.NativeOrNativeSourcesBuild; public class NativeImageReflectConfigStep { @BuildStep(onlyIf = NativeOrNativeSourcesBuild.class) void generateReflectConfig(BuildProducer reflectConfig, + NativeConfig nativeConfig, List reflectiveMethods, List reflectiveFields, List reflectiveClassBuildItems, @@ -42,7 +45,8 @@ void generateReflectConfig(BuildProducer reflectConf for (ReflectiveClassBuildItem i : reflectiveClassBuildItems) { addReflectiveClass(reflectiveClasses, forcedNonWeakClasses, i.isConstructors(), i.isQueryConstructors(), i.isMethods(), i.isQueryMethods(), i.isFields(), i.isClasses(), - i.isWeak(), i.isSerialization(), i.isUnsafeAllocated(), i.getClassNames().toArray(new String[0])); + i.isWeak(), i.isSerialization(), i.isUnsafeAllocated(), i.getReason(), + i.getClassNames().toArray(new String[0])); } for (ReflectiveFieldBuildItem i : reflectiveFields) { addReflectiveField(reflectiveClasses, i); @@ -127,6 +131,13 @@ void generateReflectConfig(BuildProducer reflectConf if (info.unsafeAllocated) { json.put("unsafeAllocated", true); } + if (nativeConfig.includeReasonsInConfigFiles() && info.reasons != null) { + JsonArrayBuilder reasonsArray = Json.array(); + for (String reason : info.reasons) { + reasonsArray.add(reason); + } + json.put("reasons", reasonsArray); + } root.add(json); } @@ -173,13 +184,13 @@ public void addReflectiveMethod(Map reflectiveClasses, R public void addReflectiveClass(Map reflectiveClasses, Set forcedNonWeakClasses, boolean constructors, boolean queryConstructors, boolean method, boolean queryMethods, boolean fields, boolean classes, boolean weak, boolean serialization, boolean unsafeAllocated, - String... className) { + String reason, String... className) { for (String cl : className) { ReflectionInfo existing = reflectiveClasses.get(cl); if (existing == null) { String typeReachable = (!forcedNonWeakClasses.contains(cl) && weak) ? cl : null; reflectiveClasses.put(cl, new ReflectionInfo(constructors, queryConstructors, method, queryMethods, fields, - classes, typeReachable, serialization, unsafeAllocated)); + classes, typeReachable, serialization, unsafeAllocated, reason)); } else { if (constructors) { existing.constructors = true; @@ -205,6 +216,12 @@ public void addReflectiveClass(Map reflectiveClasses, Se if (unsafeAllocated) { existing.unsafeAllocated = true; } + if (reason != null) { + if (existing.reasons == null) { + existing.reasons = new ArrayList<>(); + } + existing.reasons.add(reason); + } } } } @@ -227,6 +244,7 @@ static final class ReflectionInfo { boolean classes; boolean serialization; boolean unsafeAllocated; + List reasons = null; String typeReachable; Set fieldSet = new HashSet<>(); Set methodSet = new HashSet<>(); @@ -234,12 +252,12 @@ static final class ReflectionInfo { Set ctorSet = new HashSet<>(); private ReflectionInfo() { - this(false, false, false, false, false, false, null, false, false); + this(false, false, false, false, false, false, null, false, false, null); } private ReflectionInfo(boolean constructors, boolean queryConstructors, boolean methods, boolean queryMethods, boolean fields, boolean classes, String typeReachable, - boolean serialization, boolean unsafeAllocated) { + boolean serialization, boolean unsafeAllocated, String reason) { this.methods = methods; this.queryMethods = queryMethods; this.fields = fields; @@ -249,6 +267,10 @@ private ReflectionInfo(boolean constructors, boolean queryConstructors, boolean this.queryConstructors = queryConstructors; this.serialization = serialization; this.unsafeAllocated = unsafeAllocated; + if (reason != null) { + reasons = new ArrayList<>(); + reasons.add(reason); + } } } diff --git a/core/deployment/src/test/java/io/quarkus/deployment/pkg/TestNativeConfig.java b/core/deployment/src/test/java/io/quarkus/deployment/pkg/TestNativeConfig.java index 4c276528b24949..3550674b52e3e8 100644 --- a/core/deployment/src/test/java/io/quarkus/deployment/pkg/TestNativeConfig.java +++ b/core/deployment/src/test/java/io/quarkus/deployment/pkg/TestNativeConfig.java @@ -225,6 +225,11 @@ public boolean enableDashboardDump() { return false; } + @Override + public boolean includeReasonsInConfigFiles() { + return false; + } + @Override public Compression compression() { return null;