Skip to content

Commit

Permalink
Refactor ReflectiveClasssBuildItem and NativeImageReflectConfigStep
Browse files Browse the repository at this point in the history
  • Loading branch information
zakkak committed Aug 1, 2024
1 parent 7f35270 commit 48df39e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static java.util.Arrays.stream;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

Expand Down Expand Up @@ -46,40 +45,8 @@ 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, Class<?>... classes) {
List<String> names = new ArrayList<>();
for (Class<?> i : classes) {
if (i == null) {
throw new NullPointerException();
}
names.add(i.getName());
}
this.className = names;
this.methods = methods;
if (methods && queryMethods) {
Log.warnf(
"Both methods and queryMethods are set to true for classes: %s. queryMethods is redundant and will be ignored",
String.join(", ", names));
this.queryMethods = false;
} else {
this.queryMethods = queryMethods;
}
this.fields = fields;
this.classes = getClasses;
this.constructors = constructors;
if (methods && queryMethods) {
Log.warnf(
"Both constructors and queryConstructors are set to true for classes: %s. queryConstructors is redundant and will be ignored",
String.join(", ", names));
this.queryConstructors = false;
} else {
this.queryConstructors = queryConstructors;
}
this.weak = weak;
this.serialization = serialization;
this.unsafeAllocated = unsafeAllocated;
if (weak && serialization) {
throw new RuntimeException("Weak reflection not supported with serialization");
}
this(constructors, queryConstructors, methods, queryMethods, fields, getClasses, weak, serialization,
unsafeAllocated, stream(classes).map(Class::getName).toArray(String[]::new));
}

/**
Expand Down Expand Up @@ -297,8 +264,9 @@ public Builder methods() {
}

/**
* Configures whether methods should be registered for reflection, for query purposes only.
* Setting this enables getting all declared methods for the class but does not allow invoking them reflectively.
* Configures whether declared methods should be registered for reflection, for query purposes only,
* i.e. {@link Class#getDeclaredMethods()}. Setting this enables getting all declared methods for the class but
* does not allow invoking them reflectively.
*/
public Builder queryMethods(boolean queryMethods) {
this.queryMethods = queryMethods;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ void generateReflectConfig(BuildProducer<GeneratedResourceBuildItem> reflectConf
forcedNonWeakClasses.add(nonWeakReflectiveClassBuildItem.getClassName());
}
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]));
addReflectiveClass(reflectiveClasses, forcedNonWeakClasses, i);
}
for (ReflectiveFieldBuildItem i : reflectiveFields) {
addReflectiveField(reflectiveClasses, i);
Expand Down Expand Up @@ -171,38 +169,38 @@ 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) {
for (String cl : className) {
ReflectiveClassBuildItem classBuildItem) {
for (String cl : classBuildItem.getClassNames()) {
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));
String typeReachable = (!forcedNonWeakClasses.contains(cl) && classBuildItem.isWeak()) ? cl : null;
reflectiveClasses.put(cl, new ReflectionInfo(classBuildItem, typeReachable));
} else {
if (constructors) {
if (classBuildItem.isConstructors()) {
existing.constructors = true;
}
if (queryConstructors) {
if (classBuildItem.isQueryConstructors()) {
existing.queryConstructors = true;
}
if (method) {
if (classBuildItem.isMethods()) {
existing.methods = true;
}
if (queryMethods) {
if (classBuildItem.isQueryMethods()) {
existing.queryMethods = true;
}
if (fields) {
if (classBuildItem.isQueryPublicMethods()) {
existing.queryPublicMethods = true;
}
if (classBuildItem.isFields()) {
existing.fields = true;
}
if (classes) {
if (classBuildItem.isClasses()) {
existing.classes = true;
}
if (serialization) {
if (classBuildItem.isSerialization()) {
existing.serialization = true;
}
if (unsafeAllocated) {
if (classBuildItem.isUnsafeAllocated()) {
existing.unsafeAllocated = true;
}
}
Expand Down Expand Up @@ -234,21 +232,18 @@ static final class ReflectionInfo {
Set<ReflectiveMethodBuildItem> ctorSet = new HashSet<>();

private ReflectionInfo() {
this(false, false, false, false, false, false, null, false, false);
}

private ReflectionInfo(boolean constructors, boolean queryConstructors, boolean methods, boolean queryMethods,
boolean fields, boolean classes, String typeReachable,
boolean serialization, boolean unsafeAllocated) {
this.methods = methods;
this.queryMethods = queryMethods;
this.fields = fields;
this.classes = classes;
private ReflectionInfo(ReflectiveClassBuildItem classBuildItem, String typeReachable) {
this.methods = classBuildItem.isMethods();
this.queryMethods = classBuildItem.isQueryMethods();
this.fields = classBuildItem.isFields();
this.classes = classBuildItem.isClasses();
this.typeReachable = typeReachable;
this.constructors = constructors;
this.queryConstructors = queryConstructors;
this.serialization = serialization;
this.unsafeAllocated = unsafeAllocated;
this.constructors = classBuildItem.isConstructors();
this.queryConstructors = classBuildItem.isQueryConstructors();
this.serialization = classBuildItem.isSerialization();
this.unsafeAllocated = classBuildItem.isUnsafeAllocated();
}
}

Expand Down

0 comments on commit 48df39e

Please sign in to comment.