From 3ff1e29a2ae72eb7382a08512152259287b33803 Mon Sep 17 00:00:00 2001 From: Foivos Zakkak Date: Mon, 13 Jan 2025 13:11:10 +0200 Subject: [PATCH 1/2] Add option to register allPublicConstructors for reflection --- .../nativeimage/ReflectiveClassBuildItem.java | 30 ++++++++++++++++--- .../steps/NativeImageReflectConfigStep.java | 5 ++++ 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/core/deployment/src/main/java/io/quarkus/deployment/builditem/nativeimage/ReflectiveClassBuildItem.java b/core/deployment/src/main/java/io/quarkus/deployment/builditem/nativeimage/ReflectiveClassBuildItem.java index 0b3e71bbc6bd7..71333ab273f66 100644 --- a/core/deployment/src/main/java/io/quarkus/deployment/builditem/nativeimage/ReflectiveClassBuildItem.java +++ b/core/deployment/src/main/java/io/quarkus/deployment/builditem/nativeimage/ReflectiveClassBuildItem.java @@ -20,6 +20,7 @@ public final class ReflectiveClassBuildItem extends MultiBuildItem { private final boolean fields; private final boolean classes; private final boolean constructors; + private final boolean publicConstructors; private final boolean queryConstructors; private final boolean weak; private final boolean serialization; @@ -46,7 +47,7 @@ public static Builder builder(String... classNames) { private ReflectiveClassBuildItem(boolean constructors, boolean queryConstructors, boolean methods, boolean queryMethods, boolean fields, boolean getClasses, boolean weak, boolean serialization, boolean unsafeAllocated, String reason, Class... classes) { - this(constructors, queryConstructors, methods, queryMethods, fields, getClasses, weak, serialization, + this(constructors, false, queryConstructors, methods, queryMethods, fields, getClasses, weak, serialization, unsafeAllocated, reason, stream(classes).map(Class::getName).toArray(String[]::new)); } @@ -118,11 +119,12 @@ public static ReflectiveClassBuildItem serializationClass(String... classNames) ReflectiveClassBuildItem(boolean constructors, boolean queryConstructors, boolean methods, boolean queryMethods, boolean fields, boolean weak, boolean serialization, boolean unsafeAllocated, String... className) { - this(constructors, queryConstructors, methods, queryMethods, fields, false, weak, serialization, unsafeAllocated, + this(constructors, false, queryConstructors, methods, queryMethods, fields, false, weak, serialization, unsafeAllocated, null, className); } - ReflectiveClassBuildItem(boolean constructors, boolean queryConstructors, boolean methods, boolean queryMethods, + ReflectiveClassBuildItem(boolean constructors, boolean publicConstructors, boolean queryConstructors, boolean methods, + boolean queryMethods, boolean fields, boolean classes, boolean weak, boolean serialization, boolean unsafeAllocated, String reason, String... className) { for (String i : className) { @@ -143,6 +145,7 @@ public static ReflectiveClassBuildItem serializationClass(String... classNames) this.fields = fields; this.classes = classes; this.constructors = constructors; + this.publicConstructors = publicConstructors; if (constructors && queryConstructors) { Log.warnf( "Both constructors and queryConstructors are set to true for classes: %s. queryConstructors is redundant and will be ignored", @@ -181,6 +184,10 @@ public boolean isConstructors() { return constructors; } + public boolean isPublicConstructors() { + return publicConstructors; + } + public boolean isQueryConstructors() { return queryConstructors; } @@ -213,6 +220,7 @@ public String getReason() { public static class Builder { private String[] className; private boolean constructors = true; + private boolean publicConstructors = false; private boolean queryConstructors; private boolean methods; private boolean queryMethods; @@ -244,6 +252,19 @@ public Builder constructors() { return constructors(true); } + /** + * Configures whether public constructors should be registered for reflection. + * Setting this enables getting all public constructors for the class as well as invoking them reflectively. + */ + public Builder publicConstructors(boolean publicConstructors) { + this.publicConstructors = publicConstructors; + return this; + } + + public Builder publicConstructors() { + return publicConstructors(true); + } + /** * Configures whether constructors should be registered for reflection, for query purposes only. * Setting this enables getting all declared constructors for the class but does not allow invoking them reflectively. @@ -358,7 +379,8 @@ public Builder unsafeAllocated() { } public ReflectiveClassBuildItem build() { - return new ReflectiveClassBuildItem(constructors, queryConstructors, methods, queryMethods, fields, classes, weak, + return new ReflectiveClassBuildItem(constructors, publicConstructors, queryConstructors, methods, queryMethods, + fields, classes, weak, serialization, unsafeAllocated, reason, className); } } 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 efe9a04ac4ddd..9ad81e43a80c4 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 @@ -94,6 +94,9 @@ void generateReflectConfig(BuildProducer reflectConf extractToJsonArray(info.ctorSet, methodsArray); } } + if (info.publicConstructors) { + json.put("allPublicConstructors", true); + } if (info.methods) { json.put("allDeclaredMethods", true); } else { @@ -246,6 +249,7 @@ public void addReflectiveField(Map reflectiveClasses, Re static final class ReflectionInfo { boolean constructors; + boolean publicConstructors; boolean queryConstructors; boolean methods; boolean queryMethods; @@ -270,6 +274,7 @@ private ReflectionInfo(ReflectiveClassBuildItem classBuildItem, String typeReach this.classes = classBuildItem.isClasses(); this.typeReachable = typeReachable; this.constructors = classBuildItem.isConstructors(); + this.publicConstructors = classBuildItem.isPublicConstructors(); this.queryConstructors = classBuildItem.isQueryConstructors(); this.serialization = classBuildItem.isSerialization(); this.unsafeAllocated = classBuildItem.isUnsafeAllocated(); From 81248e73067408608fa6033e78feabddc16507a1 Mon Sep 17 00:00:00 2001 From: Foivos Zakkak Date: Mon, 13 Jan 2025 13:11:37 +0200 Subject: [PATCH 2/2] Register public constructors for Narayana classes --- .../io/quarkus/narayana/jta/deployment/NarayanaJtaProcessor.java | 1 + 1 file changed, 1 insertion(+) diff --git a/extensions/narayana-jta/deployment/src/main/java/io/quarkus/narayana/jta/deployment/NarayanaJtaProcessor.java b/extensions/narayana-jta/deployment/src/main/java/io/quarkus/narayana/jta/deployment/NarayanaJtaProcessor.java index 7938ad8abec60..c821bacf9572f 100644 --- a/extensions/narayana-jta/deployment/src/main/java/io/quarkus/narayana/jta/deployment/NarayanaJtaProcessor.java +++ b/extensions/narayana-jta/deployment/src/main/java/io/quarkus/narayana/jta/deployment/NarayanaJtaProcessor.java @@ -138,6 +138,7 @@ public void build(NarayanaJtaRecorder recorder, JTANodeNameXAResourceOrphanFilter.class, JTAActionStatusServiceXAResourceOrphanFilter.class, ExpiredTransactionStatusManagerScanner.class) + .publicConstructors() .reason(getClass().getName()) .build());