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 Jul 29, 2024
1 parent a8b9707 commit 4467a48
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 8 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 @@ -58,11 +58,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 @@ -240,6 +242,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 ArrayList<>();
}
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 @@ -527,7 +527,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

0 comments on commit 4467a48

Please sign in to comment.