Skip to content

Commit

Permalink
Introduce reason field in ReflectiveMethodBuildItem
Browse files Browse the repository at this point in the history
  • Loading branch information
zakkak committed Aug 21, 2024
1 parent 0a0f160 commit 902fc89
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ public class SecureRandomProcessor {
@BuildStep
void registerReflectiveMethods(BuildProducer<ReflectiveMethodBuildItem> reflectiveMethods) {
// Called reflectively through java.security.SecureRandom.SecureRandom()
reflectiveMethods.produce(new ReflectiveMethodBuildItem("sun.security.provider.NativePRNG", "<init>",
reflectiveMethods.produce(new ReflectiveMethodBuildItem(
getClass().getName(),
"sun.security.provider.NativePRNG", "<init>",
java.security.SecureRandomParameters.class));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Objects;
import java.util.stream.Collectors;

import org.jboss.jandex.MethodInfo;

Expand All @@ -14,50 +15,65 @@ public final class ReflectiveMethodBuildItem extends MultiBuildItem {
final String name;
final String[] params;
final boolean queryOnly;
final String reason;

public ReflectiveMethodBuildItem(MethodInfo methodInfo) {
this(false, methodInfo);
this(null, false, methodInfo);
}

public ReflectiveMethodBuildItem(String reason, MethodInfo methodInfo) {
this(reason, false, methodInfo);
}

public ReflectiveMethodBuildItem(boolean queryOnly, MethodInfo methodInfo) {
String[] params = new String[methodInfo.parametersCount()];
for (int i = 0; i < params.length; ++i) {
params[i] = methodInfo.parameterType(i).name().toString();
}
this.name = methodInfo.name();
this.params = params;
this.declaringClass = methodInfo.declaringClass().name().toString();
this.queryOnly = queryOnly;
this(null, queryOnly, methodInfo);
}

public ReflectiveMethodBuildItem(String reason, boolean queryOnly, MethodInfo methodInfo) {
this(reason, queryOnly, methodInfo.declaringClass().name().toString(), methodInfo.name(),
methodInfo.parameterTypes().stream().map(p -> p.name().toString()).toArray(String[]::new));
}

public ReflectiveMethodBuildItem(Method method) {
this(false, method);
}

public ReflectiveMethodBuildItem(boolean queryOnly, Method method) {
this.params = new String[method.getParameterCount()];
if (method.getParameterCount() > 0) {
Class<?>[] parameterTypes = method.getParameterTypes();
for (int i = 0; i < params.length; ++i) {
params[i] = parameterTypes[i].getName();
}
}
this.name = method.getName();
this.declaringClass = method.getDeclaringClass().getName();
this.queryOnly = queryOnly;
this(null, queryOnly, method);
}

public ReflectiveMethodBuildItem(String reason, boolean queryOnly, Method method) {
this(reason, queryOnly, method.getDeclaringClass().getName(), method.getName(),
Arrays.stream(method.getParameterTypes()).map(Class::getName).toArray(String[]::new));
}

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

public ReflectiveMethodBuildItem(String reason, String declaringClass, String name,
String... params) {
this(reason, false, declaringClass, name, params);
}

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

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

public ReflectiveMethodBuildItem(String reason, String declaringClass, String name,
Class<?>... params) {
this(reason, false, declaringClass, name, Arrays.stream(params).map(Class::getName).toArray(String[]::new));
}

public ReflectiveMethodBuildItem(String declaringClass, String name,
Expand All @@ -67,13 +83,7 @@ public ReflectiveMethodBuildItem(String declaringClass, String name,

public ReflectiveMethodBuildItem(boolean queryOnly, String declaringClass, String name,
Class<?>... params) {
this.declaringClass = declaringClass;
this.name = name;
this.params = new String[params.length];
for (int i = 0; i < params.length; ++i) {
this.params[i] = params[i].getName();
}
this.queryOnly = queryOnly;
this(null, queryOnly, declaringClass, name, Arrays.stream(params).map(Class::getName).toArray(String[]::new));
}

public String getName() {
Expand All @@ -92,6 +102,10 @@ public boolean isQueryOnly() {
return queryOnly;
}

public String getReason() {
return reason;
}

@Override
public boolean equals(Object o) {
if (this == o)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,10 @@ private static void processConfigClass(
reflectiveClasses.produce(ReflectiveClassBuildItem.builder(mappingMetadata.getClassName())
.reason(ConfigMappingUtils.class.getName())
.build());
reflectiveMethods
.produce(new ReflectiveMethodBuildItem(mappingMetadata.getClassName(), "getDefaults", new String[0]));
reflectiveMethods.produce(new ReflectiveMethodBuildItem(mappingMetadata.getClassName(), "getNames", new String[0]));
reflectiveMethods.produce(new ReflectiveMethodBuildItem(ConfigMappingUtils.class.getName(),
mappingMetadata.getClassName(), "getDefaults", new String[0]));
reflectiveMethods.produce(new ReflectiveMethodBuildItem(ConfigMappingUtils.class.getName(),
mappingMetadata.getClassName(), "getNames", new String[0]));

configComponentInterfaces.add(mappingMetadata.getInterfaceType());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ void generateReflectConfig(BuildProducer<GeneratedResourceBuildItem> reflectConf
for (ServiceProviderBuildItem i : serviceProviderBuildItems) {
for (String provider : i.providers()) {
// Register the nullary constructor
addReflectiveMethod(reflectiveClasses, new ReflectiveMethodBuildItem(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(true, provider, "provider", new String[0]));
new ReflectiveMethodBuildItem("Class registered as provider", true, provider, "provider", new String[0]));
}
}

Expand Down Expand Up @@ -175,6 +175,13 @@ public void addReflectiveMethod(Map<String, ReflectionInfo> reflectiveClasses, R
existing.methodSet.add(methodInfo);
}
}
String reason = methodInfo.getReason();
if (reason != null) {
if (existing.reasons == null) {
existing.reasons = new HashSet<>();
}
existing.reasons.add(reason);
}
}

public void addReflectiveClass(Map<String, ReflectionInfo> reflectiveClasses, Set<String> forcedNonWeakClasses,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -538,12 +538,12 @@ public void generateResources(ArcConfig config,

@Override
public void registerMethod(String declaringClass, String name, String... params) {
reflectiveMethods.produce(new ReflectiveMethodBuildItem(declaringClass, name, params));
reflectiveMethods.produce(new ReflectiveMethodBuildItem(getClass().getName(), declaringClass, name, params));
}

@Override
public void registerMethod(MethodInfo methodInfo) {
reflectiveMethods.produce(new ReflectiveMethodBuildItem(methodInfo));
reflectiveMethods.produce(new ReflectiveMethodBuildItem(getClass().getName(), methodInfo));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ private String implementInit(IndexView index, ClassCreator initializer,
chainHandle, methodHandle, bindingsHandle, forwardingFunc);

// Needed when running on native image
reflectiveMethods.produce(new ReflectiveMethodBuildItem(method));
reflectiveMethods.produce(new ReflectiveMethodBuildItem(getClass().getName(), method));

// Call InterceptedStaticMethods.register()
init.invokeStaticMethod(INTERCEPTED_STATIC_METHODS_REGISTER, init.load(interceptedStaticMethod.getHash()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,8 @@ public void build(
} else if (annotation.target().kind() == AnnotationTarget.Kind.METHOD) {
contributeClass(classNamesToBeValidated, indexView, annotation.target().asMethod().declaringClass().name());
// we need to register the method for reflection as it could be a getter
reflectiveMethods.produce(new ReflectiveMethodBuildItem(annotation.target().asMethod()));
reflectiveMethods
.produce(new ReflectiveMethodBuildItem(getClass().getName(), annotation.target().asMethod()));
contributeClassMarkedForCascadingValidation(classNamesToBeValidated, indexView, consideredAnnotation,
annotation.target().asMethod().returnType());
contributeMethodsWithInheritedValidation(methodsWithInheritedValidation, indexView,
Expand Down Expand Up @@ -535,7 +536,8 @@ public void build(
}
} else if (enclosingTarget.kind() == AnnotationTarget.Kind.METHOD) {
contributeClass(classNamesToBeValidated, indexView, enclosingTarget.asMethod().declaringClass().name());
reflectiveMethods.produce(new ReflectiveMethodBuildItem(enclosingTarget.asMethod()));
reflectiveMethods
.produce(new ReflectiveMethodBuildItem(getClass().getName(), enclosingTarget.asMethod()));
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,7 @@ 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(creatorInstance.target().asMethod()));
reflectiveMethod.produce(new ReflectiveMethodBuildItem(getClass().getName(), creatorInstance.target().asMethod()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ void build(BuildProducer<ReflectiveClassBuildItem> reflectiveClass,
ReflectiveClassBuildItem.builder("java.lang.String").build());

// Necessary for Yasson versions using MethodHandles (2.0+)
reflectiveMethod.produce(new ReflectiveMethodBuildItem("jdk.internal.misc.Unsafe", "putReference", Object.class,
reflectiveMethod.produce(new ReflectiveMethodBuildItem(
getClass().getName(),
"jdk.internal.misc.Unsafe", "putReference", Object.class,
long.class, Object.class));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public void build(BuildProducer<AnnotationsTransformerBuildItem> annotationsTran
clazz.methods()
.stream()
.filter(it -> fallbackMethod.equals(it.name()))
.forEach(it -> reflectiveMethod.produce(new ReflectiveMethodBuildItem(it)));
.forEach(it -> reflectiveMethod.produce(new ReflectiveMethodBuildItem(getClass().getName(), it)));

DotName superClass = clazz.superName();
if (superClass != null && !DotNames.OBJECT.equals(superClass)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ private ResultHandle populateConfigObject(ClassLoader classLoader, ClassInfo con
continue;
}
if (method.parameterType(0).name().equals(DotNames.STRING)) {
reflectiveMethods.produce(new ReflectiveMethodBuildItem(method));
reflectiveMethods.produce(new ReflectiveMethodBuildItem(getClass().getName(), method));
break;
}
}
Expand Down

0 comments on commit 902fc89

Please sign in to comment.