Skip to content

Commit 4ff7bbf

Browse files
authored
Merge pull request #42205 from zakkak/2024-05-23-improve-reflective-registration-tracing
Enable comments (reasons) in the generated reflect-config.json and improve hierarchical registration tracing
2 parents c46100d + d5858c0 commit 4ff7bbf

File tree

81 files changed

+703
-389
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+703
-389
lines changed

core/deployment/src/main/java/io/quarkus/deployment/CollectionClassProcessor.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
public class CollectionClassProcessor {
99
@BuildStep
1010
ReflectiveClassBuildItem setupCollectionClasses() {
11-
return ReflectiveClassBuildItem.builder(ArrayList.class.getName(),
12-
HashMap.class.getName(),
13-
HashSet.class.getName(),
14-
LinkedList.class.getName(),
15-
LinkedHashMap.class.getName(),
16-
LinkedHashSet.class.getName(),
17-
TreeMap.class.getName(),
18-
TreeSet.class.getName()).build();
11+
return ReflectiveClassBuildItem.builder(ArrayList.class,
12+
HashMap.class,
13+
HashSet.class,
14+
LinkedList.class,
15+
LinkedHashMap.class,
16+
LinkedHashSet.class,
17+
TreeMap.class,
18+
TreeSet.class).reason(getClass().getName()).build();
1919
}
2020
}

core/deployment/src/main/java/io/quarkus/deployment/SecureRandomProcessor.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ public class SecureRandomProcessor {
99
@BuildStep
1010
void registerReflectiveMethods(BuildProducer<ReflectiveMethodBuildItem> reflectiveMethods) {
1111
// Called reflectively through java.security.SecureRandom.SecureRandom()
12-
reflectiveMethods.produce(new ReflectiveMethodBuildItem("sun.security.provider.NativePRNG", "<init>",
12+
reflectiveMethods.produce(new ReflectiveMethodBuildItem(
13+
getClass().getName(),
14+
"sun.security.provider.NativePRNG", "<init>",
1315
java.security.SecureRandomParameters.class));
1416
}
1517

core/deployment/src/main/java/io/quarkus/deployment/builditem/nativeimage/ReflectiveClassBuildItem.java

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public final class ReflectiveClassBuildItem extends MultiBuildItem {
2424
private final boolean weak;
2525
private final boolean serialization;
2626
private final boolean unsafeAllocated;
27+
private final String reason;
2728

2829
public static Builder builder(Class<?>... classes) {
2930
String[] classNames = stream(classes)
@@ -43,10 +44,10 @@ public static Builder builder(String... classNames) {
4344
}
4445

4546
private ReflectiveClassBuildItem(boolean constructors, boolean queryConstructors, boolean methods, boolean queryMethods,
46-
boolean fields, boolean getClasses, boolean weak, boolean serialization,
47-
boolean unsafeAllocated, Class<?>... classes) {
47+
boolean fields, boolean getClasses, boolean weak, boolean serialization, boolean unsafeAllocated, String reason,
48+
Class<?>... classes) {
4849
this(constructors, queryConstructors, methods, queryMethods, fields, getClasses, weak, serialization,
49-
unsafeAllocated, stream(classes).map(Class::getName).toArray(String[]::new));
50+
unsafeAllocated, reason, stream(classes).map(Class::getName).toArray(String[]::new));
5051
}
5152

5253
/**
@@ -64,7 +65,7 @@ public ReflectiveClassBuildItem(boolean methods, boolean fields, Class<?>... cla
6465
*/
6566
@Deprecated(since = "3.0", forRemoval = true)
6667
public ReflectiveClassBuildItem(boolean constructors, boolean methods, boolean fields, Class<?>... classes) {
67-
this(constructors, false, methods, false, fields, false, false, false, false, classes);
68+
this(constructors, false, methods, false, fields, false, false, false, false, null, classes);
6869
}
6970

7071
/**
@@ -118,12 +119,12 @@ public static ReflectiveClassBuildItem serializationClass(String... classNames)
118119
boolean fields, boolean weak, boolean serialization,
119120
boolean unsafeAllocated, String... className) {
120121
this(constructors, queryConstructors, methods, queryMethods, fields, false, weak, serialization, unsafeAllocated,
121-
className);
122+
null, className);
122123
}
123124

124125
ReflectiveClassBuildItem(boolean constructors, boolean queryConstructors, boolean methods, boolean queryMethods,
125126
boolean fields, boolean classes, boolean weak, boolean serialization,
126-
boolean unsafeAllocated, String... className) {
127+
boolean unsafeAllocated, String reason, String... className) {
127128
for (String i : className) {
128129
if (i == null) {
129130
throw new NullPointerException();
@@ -153,6 +154,7 @@ public static ReflectiveClassBuildItem serializationClass(String... classNames)
153154
this.weak = weak;
154155
this.serialization = serialization;
155156
this.unsafeAllocated = unsafeAllocated;
157+
this.reason = reason;
156158
}
157159

158160
public List<String> getClassNames() {
@@ -204,6 +206,10 @@ public boolean isUnsafeAllocated() {
204206
return unsafeAllocated;
205207
}
206208

209+
public String getReason() {
210+
return reason;
211+
}
212+
207213
public static class Builder {
208214
private String[] className;
209215
private boolean constructors = true;
@@ -215,6 +221,7 @@ public static class Builder {
215221
private boolean weak;
216222
private boolean serialization;
217223
private boolean unsafeAllocated;
224+
private String reason;
218225

219226
private Builder() {
220227
}
@@ -341,13 +348,18 @@ public Builder unsafeAllocated(boolean unsafeAllocated) {
341348
return this;
342349
}
343350

351+
public Builder reason(String reason) {
352+
this.reason = reason;
353+
return this;
354+
}
355+
344356
public Builder unsafeAllocated() {
345357
return unsafeAllocated(true);
346358
}
347359

348360
public ReflectiveClassBuildItem build() {
349361
return new ReflectiveClassBuildItem(constructors, queryConstructors, methods, queryMethods, fields, classes, weak,
350-
serialization, unsafeAllocated, className);
362+
serialization, unsafeAllocated, reason, className);
351363
}
352364
}
353365
}

core/deployment/src/main/java/io/quarkus/deployment/builditem/nativeimage/ReflectiveFieldBuildItem.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,24 @@ public final class ReflectiveFieldBuildItem extends MultiBuildItem {
1010

1111
final String declaringClass;
1212
final String name;
13+
final String reason;
1314

14-
public ReflectiveFieldBuildItem(FieldInfo field) {
15+
public ReflectiveFieldBuildItem(String reason, FieldInfo field) {
16+
this.reason = reason;
1517
this.name = field.name();
1618
this.declaringClass = field.declaringClass().name().toString();
1719
}
1820

21+
public ReflectiveFieldBuildItem(FieldInfo field) {
22+
this(null, field);
23+
}
24+
1925
public ReflectiveFieldBuildItem(Field field) {
26+
this(null, field);
27+
}
28+
29+
public ReflectiveFieldBuildItem(String reason, Field field) {
30+
this.reason = reason;
2031
this.name = field.getName();
2132
this.declaringClass = field.getDeclaringClass().getName();
2233
}
@@ -28,4 +39,8 @@ public String getDeclaringClass() {
2839
public String getName() {
2940
return name;
3041
}
42+
43+
public String getReason() {
44+
return reason;
45+
}
3146
}

core/deployment/src/main/java/io/quarkus/deployment/builditem/nativeimage/ReflectiveMethodBuildItem.java

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,50 +14,65 @@ public final class ReflectiveMethodBuildItem extends MultiBuildItem {
1414
final String name;
1515
final String[] params;
1616
final boolean queryOnly;
17+
final String reason;
1718

1819
public ReflectiveMethodBuildItem(MethodInfo methodInfo) {
19-
this(false, methodInfo);
20+
this(null, false, methodInfo);
21+
}
22+
23+
public ReflectiveMethodBuildItem(String reason, MethodInfo methodInfo) {
24+
this(reason, false, methodInfo);
2025
}
2126

2227
public ReflectiveMethodBuildItem(boolean queryOnly, MethodInfo methodInfo) {
23-
String[] params = new String[methodInfo.parametersCount()];
24-
for (int i = 0; i < params.length; ++i) {
25-
params[i] = methodInfo.parameterType(i).name().toString();
26-
}
27-
this.name = methodInfo.name();
28-
this.params = params;
29-
this.declaringClass = methodInfo.declaringClass().name().toString();
30-
this.queryOnly = queryOnly;
28+
this(null, queryOnly, methodInfo);
29+
}
30+
31+
public ReflectiveMethodBuildItem(String reason, boolean queryOnly, MethodInfo methodInfo) {
32+
this(reason, queryOnly, methodInfo.declaringClass().name().toString(), methodInfo.name(),
33+
methodInfo.parameterTypes().stream().map(p -> p.name().toString()).toArray(String[]::new));
3134
}
3235

3336
public ReflectiveMethodBuildItem(Method method) {
3437
this(false, method);
3538
}
3639

3740
public ReflectiveMethodBuildItem(boolean queryOnly, Method method) {
38-
this.params = new String[method.getParameterCount()];
39-
if (method.getParameterCount() > 0) {
40-
Class<?>[] parameterTypes = method.getParameterTypes();
41-
for (int i = 0; i < params.length; ++i) {
42-
params[i] = parameterTypes[i].getName();
43-
}
44-
}
45-
this.name = method.getName();
46-
this.declaringClass = method.getDeclaringClass().getName();
47-
this.queryOnly = queryOnly;
41+
this(null, queryOnly, method);
42+
}
43+
44+
public ReflectiveMethodBuildItem(String reason, boolean queryOnly, Method method) {
45+
this(reason, queryOnly, method.getDeclaringClass().getName(), method.getName(),
46+
Arrays.stream(method.getParameterTypes()).map(Class::getName).toArray(String[]::new));
4847
}
4948

5049
public ReflectiveMethodBuildItem(String declaringClass, String name,
5150
String... params) {
52-
this(false, declaringClass, name, params);
51+
this(null, false, declaringClass, name, params);
52+
}
53+
54+
public ReflectiveMethodBuildItem(String reason, String declaringClass, String name,
55+
String... params) {
56+
this(reason, false, declaringClass, name, params);
5357
}
5458

5559
public ReflectiveMethodBuildItem(boolean queryOnly, String declaringClass, String name,
5660
String... params) {
61+
this(null, queryOnly, declaringClass, name, params);
62+
}
63+
64+
public ReflectiveMethodBuildItem(String reason, boolean queryOnly, String declaringClass, String name,
65+
String... params) {
5766
this.declaringClass = declaringClass;
5867
this.name = name;
5968
this.params = params;
6069
this.queryOnly = queryOnly;
70+
this.reason = reason;
71+
}
72+
73+
public ReflectiveMethodBuildItem(String reason, String declaringClass, String name,
74+
Class<?>... params) {
75+
this(reason, false, declaringClass, name, Arrays.stream(params).map(Class::getName).toArray(String[]::new));
6176
}
6277

6378
public ReflectiveMethodBuildItem(String declaringClass, String name,
@@ -67,13 +82,7 @@ public ReflectiveMethodBuildItem(String declaringClass, String name,
6782

6883
public ReflectiveMethodBuildItem(boolean queryOnly, String declaringClass, String name,
6984
Class<?>... params) {
70-
this.declaringClass = declaringClass;
71-
this.name = name;
72-
this.params = new String[params.length];
73-
for (int i = 0; i < params.length; ++i) {
74-
this.params[i] = params[i].getName();
75-
}
76-
this.queryOnly = queryOnly;
85+
this(null, queryOnly, declaringClass, name, Arrays.stream(params).map(Class::getName).toArray(String[]::new));
7786
}
7887

7988
public String getName() {
@@ -92,6 +101,10 @@ public boolean isQueryOnly() {
92101
return queryOnly;
93102
}
94103

104+
public String getReason() {
105+
return reason;
106+
}
107+
95108
@Override
96109
public boolean equals(Object o) {
97110
if (this == o)

core/deployment/src/main/java/io/quarkus/deployment/configuration/ConfigMappingUtils.java

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,13 @@ private static void processConfigClass(
119119
classBytes));
120120
additionalConstrainedClasses.produce(AdditionalConstrainedClassBuildItem.of(mappingMetadata.getClassName(),
121121
classBytes));
122-
reflectiveClasses.produce(ReflectiveClassBuildItem.builder(mappingMetadata.getClassName()).constructors().build());
123-
reflectiveMethods
124-
.produce(new ReflectiveMethodBuildItem(mappingMetadata.getClassName(), "getDefaults", new String[0]));
125-
reflectiveMethods.produce(new ReflectiveMethodBuildItem(mappingMetadata.getClassName(), "getNames", new String[0]));
122+
reflectiveClasses.produce(ReflectiveClassBuildItem.builder(mappingMetadata.getClassName())
123+
.reason(ConfigMappingUtils.class.getName())
124+
.build());
125+
reflectiveMethods.produce(new ReflectiveMethodBuildItem(ConfigMappingUtils.class.getName(),
126+
mappingMetadata.getClassName(), "getDefaults", new String[0]));
127+
reflectiveMethods.produce(new ReflectiveMethodBuildItem(ConfigMappingUtils.class.getName(),
128+
mappingMetadata.getClassName(), "getNames", new String[0]));
126129

127130
configComponentInterfaces.add(mappingMetadata.getInterfaceType());
128131

@@ -141,6 +144,8 @@ private static void processProperties(
141144
ConfigMappingInterface mapping = ConfigMappingLoader.getConfigMapping(configClass);
142145
for (Property property : mapping.getProperties()) {
143146
Class<?> returnType = property.getMethod().getReturnType();
147+
String reason = ConfigMappingUtils.class.getSimpleName() + " Required to process property "
148+
+ property.getPropertyName();
144149

145150
if (property.hasConvertWith()) {
146151
Class<? extends Converter<?>> convertWith;
@@ -149,39 +154,44 @@ private static void processProperties(
149154
} else {
150155
convertWith = property.asPrimitive().getConvertWith();
151156
}
152-
reflectiveClasses.produce(ReflectiveClassBuildItem.builder(convertWith).build());
157+
reflectiveClasses.produce(ReflectiveClassBuildItem.builder(convertWith).reason(reason).build());
153158
}
154159

155-
registerImplicitConverter(property, reflectiveClasses);
160+
registerImplicitConverter(property, reason, reflectiveClasses);
156161

157162
if (property.isMap()) {
158163
MapProperty mapProperty = property.asMap();
159164
if (mapProperty.hasKeyConvertWith()) {
160-
reflectiveClasses.produce(ReflectiveClassBuildItem.builder(mapProperty.getKeyConvertWith()).build());
165+
reflectiveClasses
166+
.produce(ReflectiveClassBuildItem.builder(mapProperty.getKeyConvertWith()).reason(reason).build());
161167
} else {
162-
reflectiveClasses.produce(ReflectiveClassBuildItem.builder(mapProperty.getKeyRawType()).build());
168+
reflectiveClasses
169+
.produce(ReflectiveClassBuildItem.builder(mapProperty.getKeyRawType()).reason(reason).build());
163170
}
164171

165-
registerImplicitConverter(mapProperty.getValueProperty(), reflectiveClasses);
172+
registerImplicitConverter(mapProperty.getValueProperty(), reason, reflectiveClasses);
166173
}
167174
}
168175
}
169176

170177
private static void registerImplicitConverter(
171178
Property property,
172-
BuildProducer<ReflectiveClassBuildItem> reflectiveClasses) {
179+
String reason, BuildProducer<ReflectiveClassBuildItem> reflectiveClasses) {
173180

174181
if (property.isLeaf() && !property.isOptional()) {
175182
LeafProperty leafProperty = property.asLeaf();
176183
if (leafProperty.hasConvertWith()) {
177-
reflectiveClasses.produce(ReflectiveClassBuildItem.builder(leafProperty.getConvertWith()).build());
184+
reflectiveClasses
185+
.produce(ReflectiveClassBuildItem.builder(leafProperty.getConvertWith()).reason(reason).build());
178186
} else {
179-
reflectiveClasses.produce(ReflectiveClassBuildItem.builder(leafProperty.getValueRawType()).methods().build());
187+
reflectiveClasses
188+
.produce(ReflectiveClassBuildItem.builder(leafProperty.getValueRawType()).reason(reason).methods()
189+
.build());
180190
}
181191
} else if (property.isOptional()) {
182-
registerImplicitConverter(property.asOptional().getNestedProperty(), reflectiveClasses);
192+
registerImplicitConverter(property.asOptional().getNestedProperty(), reason, reflectiveClasses);
183193
} else if (property.isCollection()) {
184-
registerImplicitConverter(property.asCollection().getElement(), reflectiveClasses);
194+
registerImplicitConverter(property.asCollection().getElement(), reason, reflectiveClasses);
185195
}
186196
}
187197

core/deployment/src/main/java/io/quarkus/deployment/logging/LoggingResourceProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ LoggingSetupBuildItem setupLoggingRuntimeInit(RecorderContext context, LoggingSe
290290
if (!discoveredLogComponents.getNameToFilterClass().isEmpty()) {
291291
reflectiveClassBuildItemBuildProducer.produce(
292292
ReflectiveClassBuildItem.builder(discoveredLogComponents.getNameToFilterClass().values().toArray(
293-
EMPTY_STRING_ARRAY)).build());
293+
EMPTY_STRING_ARRAY)).reason(getClass().getName()).build());
294294
serviceProviderBuildItemBuildProducer
295295
.produce(ServiceProviderBuildItem.allProvidersFromClassPath(LogFilterFactory.class.getName()));
296296
}

core/deployment/src/main/java/io/quarkus/deployment/pkg/NativeConfig.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,12 @@ interface Debug {
484484
@WithDefault("false")
485485
boolean enableDashboardDump();
486486

487+
/**
488+
* Include a reasons entries in the generated json configuration files.
489+
*/
490+
@WithDefault("false")
491+
boolean includeReasonsInConfigFiles();
492+
487493
/**
488494
* Configure native executable compression using UPX.
489495
*/

core/deployment/src/main/java/io/quarkus/deployment/steps/ConfigGenerationBuildStep.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ void buildTimeRunTimeConfig(
157157
method.returnValue(configBuilder);
158158
}
159159

160-
reflectiveClass.produce(ReflectiveClassBuildItem.builder(builderClassName).build());
160+
reflectiveClass.produce(ReflectiveClassBuildItem.builder(builderClassName).reason(getClass().getName()).build());
161161
staticInitConfigBuilder.produce(new StaticInitConfigBuilderBuildItem(builderClassName));
162162
runTimeConfigBuilder.produce(new RunTimeConfigBuilderBuildItem(builderClassName));
163163
}

core/deployment/src/main/java/io/quarkus/deployment/steps/MainClassBuildStep.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ private void writeRecordedBytecode(BytecodeRecorderImpl recorder, String fallbac
512512
*/
513513
@BuildStep
514514
ReflectiveClassBuildItem applicationReflection() {
515-
return ReflectiveClassBuildItem.builder(Application.APP_CLASS_NAME).build();
515+
return ReflectiveClassBuildItem.builder(Application.APP_CLASS_NAME).reason("The generated application class").build();
516516
}
517517

518518
/**

0 commit comments

Comments
 (0)