Skip to content

Commit

Permalink
Introduce reason field in ReflectiveFieldBuildItem
Browse files Browse the repository at this point in the history
  • Loading branch information
zakkak committed Aug 1, 2024
1 parent 62e6ed9 commit e622dc9
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,24 @@ public final class ReflectiveFieldBuildItem extends MultiBuildItem {

final String declaringClass;
final String name;
final String reason;

public ReflectiveFieldBuildItem(FieldInfo field) {
public ReflectiveFieldBuildItem(String reason, FieldInfo field) {
this.reason = reason;
this.name = field.name();
this.declaringClass = field.declaringClass().name().toString();
}

public ReflectiveFieldBuildItem(FieldInfo field) {
this(null, field);
}

public ReflectiveFieldBuildItem(Field field) {
this(null, field);
}

public ReflectiveFieldBuildItem(String reason, Field field) {
this.reason = reason;
this.name = field.getName();
this.declaringClass = field.getDeclaringClass().getName();
}
Expand All @@ -28,4 +39,8 @@ public String getDeclaringClass() {
public String getName() {
return name;
}

public String getReason() {
return reason;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Objects;
import java.util.stream.Collectors;

import org.jboss.jandex.MethodInfo;

Expand Down Expand Up @@ -58,7 +57,7 @@ public ReflectiveMethodBuildItem(String reason, String declaringClass, String na
}

public ReflectiveMethodBuildItem(boolean queryOnly, String declaringClass, String name,
String... params) {
String... params) {
this(null, queryOnly, declaringClass, name, params);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,13 @@ void generateReflectConfig(BuildProducer<GeneratedResourceBuildItem> reflectConf
for (ServiceProviderBuildItem i : serviceProviderBuildItems) {
for (String provider : i.providers()) {
// Register the nullary constructor
addReflectiveMethod(reflectiveClasses, new ReflectiveMethodBuildItem("Class registered as provider", provider, "<init>", new String[0]));
addReflectiveMethod(reflectiveClasses,
new ReflectiveMethodBuildItem("Class registered as provider", provider, "<init>", new String[0]));
// Register public provider() method for lookkup to avoid throwing a MissingReflectionRegistrationError at run time.
// See ServiceLoader#loadProvider and ServiceLoader#findStaticProviderMethod.
addReflectiveMethod(reflectiveClasses,
new ReflectiveMethodBuildItem("Class registered as provider", true, provider, "provider", new String[0]));
new ReflectiveMethodBuildItem("Class registered as provider", true, provider, "provider",
new String[0]));
}
}

Expand Down Expand Up @@ -239,6 +241,13 @@ public void addReflectiveField(Map<String, ReflectionInfo> reflectiveClasses, Re
reflectiveClasses.put(cl, existing = new ReflectionInfo());
}
existing.fieldSet.add(fieldInfo.getName());
String reason = fieldInfo.getReason();
if (reason != null) {
if (existing.reasons == null) {
existing.reasons = new HashSet<>();
}
existing.reasons.add(reason);
}
}

static final class ReflectionInfo {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ public void registerMethod(MethodInfo methodInfo) {

@Override
public void registerField(FieldInfo fieldInfo) {
reflectiveFields.produce(new ReflectiveFieldBuildItem(fieldInfo));
reflectiveFields.produce(new ReflectiveFieldBuildItem(getClass().getName(), fieldInfo));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ public void build(
for (AnnotationInstance annotation : annotationInstances) {
if (annotation.target().kind() == AnnotationTarget.Kind.FIELD) {
contributeClass(classNamesToBeValidated, indexView, annotation.target().asField().declaringClass().name());
reflectiveFields.produce(new ReflectiveFieldBuildItem(annotation.target().asField()));
reflectiveFields.produce(new ReflectiveFieldBuildItem(getClass().getName(), annotation.target().asField()));
contributeClassMarkedForCascadingValidation(classNamesToBeValidated, indexView, consideredAnnotation,
annotation.target().asField().type());
} else if (annotation.target().kind() == AnnotationTarget.Kind.METHOD) {
Expand Down Expand Up @@ -528,7 +528,7 @@ public void build(
AnnotationTarget enclosingTarget = annotation.target().asType().enclosingTarget();
if (enclosingTarget.kind() == AnnotationTarget.Kind.FIELD) {
contributeClass(classNamesToBeValidated, indexView, enclosingTarget.asField().declaringClass().name());
reflectiveFields.produce(new ReflectiveFieldBuildItem(enclosingTarget.asField()));
reflectiveFields.produce(new ReflectiveFieldBuildItem(getClass().getName(), enclosingTarget.asField()));
if (annotation.target().asType().target() != null) {
contributeClassMarkedForCascadingValidation(classNamesToBeValidated, indexView,
consideredAnnotation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,8 @@ void register(
// make sure we register the constructors and methods marked with @JsonCreator for reflection
for (AnnotationInstance creatorInstance : index.getAnnotations(JSON_CREATOR)) {
if (METHOD == creatorInstance.target().kind()) {
reflectiveMethod.produce(new ReflectiveMethodBuildItem(getClass().getName(), creatorInstance.target().asMethod()));
reflectiveMethod
.produce(new ReflectiveMethodBuildItem(getClass().getName(), creatorInstance.target().asMethod()));
}
}

Expand Down

0 comments on commit e622dc9

Please sign in to comment.