Skip to content

Commit 9f48115

Browse files
committed
Extend ReflectiveClassBuildItem with queryPublicMethods option
Configures whether declared methods should be registered for reflection, for query purposes only, i.e. {@link Class#getMethods()}. Setting this enables getting all declared methods for the class but does not allow invoking them reflectively. Follow up to quarkusio#42035
1 parent 6d68cc4 commit 9f48115

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

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

+26-5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public final class ReflectiveClassBuildItem extends MultiBuildItem {
1717
private final List<String> className;
1818
private final boolean methods;
1919
private final boolean queryMethods;
20+
private final boolean queryPublicMethods;
2021
private final boolean fields;
2122
private final boolean classes;
2223
private final boolean constructors;
@@ -45,7 +46,7 @@ public static Builder builder(String... classNames) {
4546
private ReflectiveClassBuildItem(boolean constructors, boolean queryConstructors, boolean methods, boolean queryMethods,
4647
boolean fields, boolean getClasses, boolean weak, boolean serialization,
4748
boolean unsafeAllocated, Class<?>... classes) {
48-
this(constructors, queryConstructors, methods, queryMethods, fields, getClasses, weak, serialization,
49+
this(constructors, queryConstructors, methods, queryMethods, false, fields, getClasses, weak, serialization,
4950
unsafeAllocated, stream(classes).map(Class::getName).toArray(String[]::new));
5051
}
5152

@@ -117,12 +118,12 @@ public static ReflectiveClassBuildItem serializationClass(String... classNames)
117118
ReflectiveClassBuildItem(boolean constructors, boolean queryConstructors, boolean methods, boolean queryMethods,
118119
boolean fields, boolean weak, boolean serialization,
119120
boolean unsafeAllocated, String... className) {
120-
this(constructors, queryConstructors, methods, queryMethods, fields, false, weak, serialization, unsafeAllocated,
121+
this(constructors, queryConstructors, methods, queryMethods, false, fields, false, weak, serialization, unsafeAllocated,
121122
className);
122123
}
123124

124125
ReflectiveClassBuildItem(boolean constructors, boolean queryConstructors, boolean methods, boolean queryMethods,
125-
boolean fields, boolean classes, boolean weak, boolean serialization,
126+
boolean queryPublicMethods, boolean fields, boolean classes, boolean weak, boolean serialization,
126127
boolean unsafeAllocated, String... className) {
127128
for (String i : className) {
128129
if (i == null) {
@@ -139,6 +140,7 @@ public static ReflectiveClassBuildItem serializationClass(String... classNames)
139140
} else {
140141
this.queryMethods = queryMethods;
141142
}
143+
this.queryPublicMethods = queryPublicMethods;
142144
this.fields = fields;
143145
this.classes = classes;
144146
this.constructors = constructors;
@@ -167,6 +169,10 @@ public boolean isQueryMethods() {
167169
return queryMethods;
168170
}
169171

172+
public boolean isQueryPublicMethods() {
173+
return queryPublicMethods;
174+
}
175+
170176
public boolean isFields() {
171177
return fields;
172178
}
@@ -210,6 +216,7 @@ public static class Builder {
210216
private boolean queryConstructors;
211217
private boolean methods;
212218
private boolean queryMethods;
219+
private boolean queryPublicMethods;
213220
private boolean fields;
214221
private boolean classes;
215222
private boolean weak;
@@ -277,6 +284,20 @@ public Builder queryMethods() {
277284
return queryMethods(true);
278285
}
279286

287+
/**
288+
* Configures whether declared methods should be registered for reflection, for query purposes only,
289+
* i.e. {@link Class#getMethods()}. Setting this enables getting all declared methods for the class but
290+
* does not allow invoking them reflectively.
291+
*/
292+
public Builder queryPublicMethods(boolean queryPublicMethods) {
293+
this.queryPublicMethods = queryPublicMethods;
294+
return this;
295+
}
296+
297+
public Builder queryPublicMethods() {
298+
return queryPublicMethods(true);
299+
}
300+
280301
/**
281302
* Configures whether fields should be registered for reflection.
282303
* Setting this enables getting all declared fields for the class as well as accessing them reflectively.
@@ -346,8 +367,8 @@ public Builder unsafeAllocated() {
346367
}
347368

348369
public ReflectiveClassBuildItem build() {
349-
return new ReflectiveClassBuildItem(constructors, queryConstructors, methods, queryMethods, fields, classes, weak,
350-
serialization, unsafeAllocated, className);
370+
return new ReflectiveClassBuildItem(constructors, queryConstructors, methods, queryMethods, queryPublicMethods,
371+
fields, classes, weak, serialization, unsafeAllocated, className);
351372
}
352373
}
353374
}

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

+8
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ void generateReflectConfig(BuildProducer<GeneratedResourceBuildItem> reflectConf
103103
extractToJsonArray(info.queriedMethodSet, queriedMethodsArray);
104104
}
105105
}
106+
if (info.queryPublicMethods) {
107+
json.put("queryAllPublicMethods", true);
108+
}
106109
if (!methodsArray.isEmpty()) {
107110
json.put("methods", methodsArray);
108111
}
@@ -188,6 +191,9 @@ public void addReflectiveClass(Map<String, ReflectionInfo> reflectiveClasses, Se
188191
if (classBuildItem.isQueryMethods()) {
189192
existing.queryMethods = true;
190193
}
194+
if (classBuildItem.isQueryPublicMethods()) {
195+
existing.queryPublicMethods = true;
196+
}
191197
if (classBuildItem.isFields()) {
192198
existing.fields = true;
193199
}
@@ -218,6 +224,7 @@ static final class ReflectionInfo {
218224
boolean queryConstructors;
219225
boolean methods;
220226
boolean queryMethods;
227+
boolean queryPublicMethods;
221228
boolean fields;
222229
boolean classes;
223230
boolean serialization;
@@ -234,6 +241,7 @@ private ReflectionInfo() {
234241
private ReflectionInfo(ReflectiveClassBuildItem classBuildItem, String typeReachable) {
235242
this.methods = classBuildItem.isMethods();
236243
this.queryMethods = classBuildItem.isQueryMethods();
244+
this.queryPublicMethods = classBuildItem.isQueryPublicMethods();
237245
this.fields = classBuildItem.isFields();
238246
this.classes = classBuildItem.isClasses();
239247
this.typeReachable = typeReachable;

0 commit comments

Comments
 (0)