Skip to content

[GR-65003] Fix reflection parser when JNI is disabled #11210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,13 @@ protected void parseClass(EconomicMap<String, Object> data) {
return;
}

boolean jniAccessible = checkOption(ConfigurationParserOption.JNI_PARSER);
/*
* Even if primitives cannot be queried through Class.forName, they can be registered to
* allow getDeclaredMethods() and similar bulk queries at run time.
*/
C condition = conditionResult.get();
TypeResult<T> result = delegate.resolveType(condition, typeDescriptor, true);
TypeResult<T> result = delegate.resolveType(condition, typeDescriptor, true, jniAccessible);
if (!result.isPresent()) {
handleMissingElement("Could not resolve " + typeDescriptor + " for reflection configuration.", result.getException());
return;
Expand All @@ -97,7 +98,6 @@ protected void parseClass(EconomicMap<String, Object> data) {
T clazz = result.get();
delegate.registerType(conditionResult.get(), clazz);

boolean jniAccessible = checkOption(ConfigurationParserOption.JNI_PARSER);
registerIfNotDefault(data, false, clazz, "allDeclaredConstructors", () -> delegate.registerDeclaredConstructors(condition, false, jniAccessible, clazz));
registerIfNotDefault(data, false, clazz, "allPublicConstructors", () -> delegate.registerPublicConstructors(condition, false, jniAccessible, clazz));
registerIfNotDefault(data, false, clazz, "allDeclaredMethods", () -> delegate.registerDeclaredMethods(condition, false, jniAccessible, clazz));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,14 @@ protected void parseClassArray(List<Object> classes) {

protected abstract void parseClass(EconomicMap<String, Object> data);

protected boolean registerIfNotDefault(EconomicMap<String, Object> data, boolean defaultValue, T clazz, String propertyName, Runnable register) {
protected void registerIfNotDefault(EconomicMap<String, Object> data, boolean defaultValue, T clazz, String propertyName, Runnable register) {
if (data.containsKey(propertyName) ? asBoolean(data.get(propertyName), propertyName) : defaultValue) {
try {
register.run();
return true;
} catch (LinkageError e) {
handleMissingElement("Could not register " + delegate.getTypeName(clazz) + ": " + propertyName + " for reflection.", e);
}
}
return false;
}

protected void parseFields(C condition, List<Object> fields, T clazz, boolean jniAccessible) {
Expand Down Expand Up @@ -165,7 +163,7 @@ private List<T> parseMethodParameters(T clazz, String methodName, List<Object> t
List<T> result = new ArrayList<>();
for (Object type : types) {
String typeName = asString(type, "types");
TypeResult<T> typeResult = delegate.resolveType(conditionResolver.alwaysTrue(), NamedConfigurationTypeDescriptor.fromJSONName(typeName), true);
TypeResult<T> typeResult = delegate.resolveType(conditionResolver.alwaysTrue(), NamedConfigurationTypeDescriptor.fromJSONName(typeName), true, false);
if (!typeResult.isPresent()) {
handleMissingElement("Could not register method " + formatMethod(clazz, methodName) + " for reflection.", typeResult.getException());
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

public interface ReflectionConfigurationParserDelegate<C, T> {

TypeResult<T> resolveType(C condition, ConfigurationTypeDescriptor typeDescriptor, boolean allowPrimitives);
TypeResult<T> resolveType(C condition, ConfigurationTypeDescriptor typeDescriptor, boolean allowPrimitives, boolean jniAccessible);

void registerType(C condition, T type);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,13 @@ protected void parseClass(EconomicMap<String, Object> data) {
}
C condition = conditionResult.get();

boolean jniParser = checkOption(ConfigurationParserOption.JNI_PARSER);
boolean typeJniAccessible = jniParser || data.get("jniAccessible") == Boolean.TRUE;
/*
* Even if primitives cannot be queried through Class.forName, they can be registered to
* allow getDeclaredMethods() and similar bulk queries at run time.
*/
TypeResult<T> result = delegate.resolveType(condition, type.get(), true);
TypeResult<T> result = delegate.resolveType(condition, type.get(), true, typeJniAccessible);
if (!result.isPresent()) {
handleMissingElement("Could not resolve " + type.get() + " for reflection configuration.", result.getException());
return;
Expand All @@ -85,7 +87,6 @@ protected void parseClass(EconomicMap<String, Object> data) {
T clazz = result.get();
delegate.registerType(conditionResult.get(), clazz);

boolean jniParser = checkOption(ConfigurationParserOption.JNI_PARSER);
delegate.registerDeclaredClasses(queryCondition, clazz);
delegate.registerPublicClasses(queryCondition, clazz);
if (!jniParser) {
Expand All @@ -101,12 +102,9 @@ protected void parseClass(EconomicMap<String, Object> data) {
delegate.registerDeclaredFields(queryCondition, true, jniParser, clazz);
delegate.registerPublicFields(queryCondition, true, jniParser, clazz);

boolean typeJniAccessible;
if (jniParser) {
typeJniAccessible = true;
} else {
if (!jniParser) {
registerIfNotDefault(data, false, clazz, "serializable", () -> delegate.registerAsSerializable(condition, clazz));
typeJniAccessible = registerIfNotDefault(data, false, clazz, "jniAccessible", () -> delegate.registerAsJniAccessed(condition, clazz));
registerIfNotDefault(data, false, clazz, "jniAccessible", () -> delegate.registerAsJniAccessed(condition, clazz));
}

registerIfNotDefault(data, false, clazz, "allDeclaredConstructors", () -> delegate.registerDeclaredConstructors(condition, false, typeJniAccessible, clazz));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public ParserConfigurationAdapter(TypeConfiguration configuration) {
}

@Override
public TypeResult<ConfigurationType> resolveType(UnresolvedConfigurationCondition condition, ConfigurationTypeDescriptor typeDescriptor, boolean allowPrimitives) {
public TypeResult<ConfigurationType> resolveType(UnresolvedConfigurationCondition condition, ConfigurationTypeDescriptor typeDescriptor, boolean allowPrimitives, boolean jniAccessible) {
ConfigurationType type = configuration.get(condition, typeDescriptor);
/*
* The type is not immediately set with all elements included. These are added afterwards
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ public void registerType(ConfigurationCondition condition, Class<?> type) {
}

@Override
public TypeResult<Class<?>> resolveType(ConfigurationCondition condition, ConfigurationTypeDescriptor typeDescriptor, boolean allowPrimitives) {
TypeResult<Class<?>> result = super.resolveType(condition, typeDescriptor, allowPrimitives);
public TypeResult<Class<?>> resolveType(ConfigurationCondition condition, ConfigurationTypeDescriptor typeDescriptor, boolean allowPrimitives, boolean jniAccessible) {
TypeResult<Class<?>> result = super.resolveType(condition, typeDescriptor, allowPrimitives, jniAccessible);
if (!result.isPresent() && typeDescriptor instanceof NamedConfigurationTypeDescriptor namedDescriptor) {
Throwable classLookupException = result.getException();
if (classLookupException instanceof LinkageError) {
String reflectionName = ClassNameSupport.typeNameToReflectionName(namedDescriptor.name());
reflectionSupport.registerClassLookupException(condition, reflectionName, classLookupException);
} else if (throwMissingRegistrationErrors() && classLookupException instanceof ClassNotFoundException) {
} else if (throwMissingRegistrationErrors() && jniAccessible & classLookupException instanceof ClassNotFoundException) {
String jniName = ClassNameSupport.typeNameToJNIName(namedDescriptor.name());
jniSupport.registerClassLookup(condition, jniName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void registerType(ConfigurationCondition condition, Class<?> type) {
}

@Override
public TypeResult<Class<?>> resolveType(ConfigurationCondition condition, ConfigurationTypeDescriptor typeDescriptor, boolean allowPrimitives) {
public TypeResult<Class<?>> resolveType(ConfigurationCondition condition, ConfigurationTypeDescriptor typeDescriptor, boolean allowPrimitives, boolean jniAccessible) {
switch (typeDescriptor.getDescriptorType()) {
case NAMED -> {
String reflectionName = ClassNameSupport.typeNameToReflectionName(((NamedConfigurationTypeDescriptor) typeDescriptor).name());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ public void duringSetup(DuringSetupAccess a) {
reflectionData.duringSetup(access.getMetaAccess(), aUniverse);
RuntimeProxyCreationSupport proxyRegistry = ImageSingletons.lookup(RuntimeProxyCreationSupport.class);
RuntimeSerializationSupport<ConfigurationCondition> serializationSupport = RuntimeSerializationSupport.singleton();
RuntimeJNIAccessSupport jniSupport = ImageSingletons.lookup(RuntimeJNIAccessSupport.class);
RuntimeJNIAccessSupport jniSupport = SubstrateOptions.JNI.getValue() ? ImageSingletons.lookup(RuntimeJNIAccessSupport.class) : null;
ReflectionConfigurationParser<ConfigurationCondition, Class<?>> parser = ConfigurationParserUtils.create(ConfigurationFile.REFLECTION, true, conditionResolver, reflectionData, proxyRegistry,
serializationSupport, jniSupport, access.getImageClassLoader());
loadedConfigurations = ConfigurationParserUtils.parseAndRegisterConfigurationsFromCombinedFile(parser, access.getImageClassLoader(), "reflection");
Expand Down
Loading