Skip to content

Commit

Permalink
Export reasons in reflect-config.json file
Browse files Browse the repository at this point in the history
  • Loading branch information
zakkak committed Jul 26, 2024
1 parent e4b5c0d commit a9a252d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<GeneratedResourceBuildItem> reflectConfig,
NativeConfig nativeConfig,
List<ReflectiveMethodBuildItem> reflectiveMethods,
List<ReflectiveFieldBuildItem> reflectiveFields,
List<ReflectiveClassBuildItem> reflectiveClassBuildItems,
Expand All @@ -42,7 +45,8 @@ void generateReflectConfig(BuildProducer<GeneratedResourceBuildItem> 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);
Expand Down Expand Up @@ -127,6 +131,13 @@ void generateReflectConfig(BuildProducer<GeneratedResourceBuildItem> 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);
}
Expand Down Expand Up @@ -173,13 +184,13 @@ public void addReflectiveMethod(Map<String, ReflectionInfo> reflectiveClasses, R
public void addReflectiveClass(Map<String, ReflectionInfo> reflectiveClasses, Set<String> 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;
Expand All @@ -205,6 +216,12 @@ public void addReflectiveClass(Map<String, ReflectionInfo> reflectiveClasses, Se
if (unsafeAllocated) {
existing.unsafeAllocated = true;
}
if (reason != null) {
if (existing.reasons == null) {
existing.reasons = new ArrayList<>();
}
existing.reasons.add(reason);
}
}
}
}
Expand All @@ -227,19 +244,20 @@ static final class ReflectionInfo {
boolean classes;
boolean serialization;
boolean unsafeAllocated;
List<String> reasons = null;
String typeReachable;
Set<String> fieldSet = new HashSet<>();
Set<ReflectiveMethodBuildItem> methodSet = new HashSet<>();
Set<ReflectiveMethodBuildItem> queriedMethodSet = new HashSet<>();
Set<ReflectiveMethodBuildItem> 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;
Expand All @@ -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);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,11 @@ public boolean enableDashboardDump() {
return false;
}

@Override
public boolean includeReasonsInConfigFiles() {
return false;
}

@Override
public Compression compression() {
return null;
Expand Down

0 comments on commit a9a252d

Please sign in to comment.