Skip to content

Commit

Permalink
Include JNI registration in reflection metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
loicottet committed Jan 23, 2025
1 parent 6aca80d commit 321fcae
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
import java.lang.reflect.Proxy;
import java.util.Arrays;

import org.graalvm.nativeimage.ImageSingletons;
import org.graalvm.nativeimage.impl.ConfigurationCondition;
import org.graalvm.nativeimage.impl.RuntimeJNIAccessSupport;
import org.graalvm.nativeimage.impl.RuntimeReflectionSupport;
import org.graalvm.nativeimage.impl.RuntimeSerializationSupport;

Expand Down Expand Up @@ -57,6 +59,7 @@ public void registerType(ConfigurationCondition condition, Class<?> type) {
if (Proxy.isProxyClass(type)) {
proxyRegistry.accept(condition, Arrays.stream(type.getInterfaces()).map(Class::getTypeName).toList());
}
ImageSingletons.lookup(RuntimeJNIAccessSupport.class).register(condition, type);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ public static RegistryAdapter create(ReflectionRegistry registry, ProxyRegistry
@Override
public void registerType(ConfigurationCondition condition, Class<?> type) {
registry.register(condition, type);
if (!(registry instanceof RuntimeReflectionSupport)) {
ImageSingletons.lookup(RuntimeReflectionSupport.class).register(condition, type);
}
}

@Override
Expand Down Expand Up @@ -175,21 +178,33 @@ public void registerDeclaredFields(ConfigurationCondition condition, boolean que
@Override
public void registerPublicMethods(ConfigurationCondition condition, boolean queriedOnly, Class<?> type) {
registry.register(condition, queriedOnly, type.getMethods());
if (!queriedOnly && !(registry instanceof RuntimeReflectionSupport)) {
ImageSingletons.lookup(RuntimeReflectionSupport.class).register(condition, false, type.getMethods());
}
}

@Override
public void registerDeclaredMethods(ConfigurationCondition condition, boolean queriedOnly, Class<?> type) {
registry.register(condition, queriedOnly, type.getDeclaredMethods());
if (!queriedOnly && !(registry instanceof RuntimeReflectionSupport)) {
ImageSingletons.lookup(RuntimeReflectionSupport.class).register(condition, false, type.getDeclaredMethods());
}
}

@Override
public void registerPublicConstructors(ConfigurationCondition condition, boolean queriedOnly, Class<?> type) {
registry.register(condition, queriedOnly, type.getConstructors());
if (!queriedOnly && !(registry instanceof RuntimeReflectionSupport)) {
ImageSingletons.lookup(RuntimeReflectionSupport.class).register(condition, false, type.getConstructors());
}
}

@Override
public void registerDeclaredConstructors(ConfigurationCondition condition, boolean queriedOnly, Class<?> type) {
registry.register(condition, queriedOnly, type.getDeclaredConstructors());
if (!queriedOnly && !(registry instanceof RuntimeReflectionSupport)) {
ImageSingletons.lookup(RuntimeReflectionSupport.class).register(condition, false, type.getDeclaredConstructors());
}
}

@Override
Expand Down Expand Up @@ -289,6 +304,9 @@ static Class<?>[] getParameterTypes(List<Class<?>> methodParameterTypes) {

private void registerExecutable(ConfigurationCondition condition, boolean queriedOnly, Executable... executable) {
registry.register(condition, queriedOnly, executable);
if (!queriedOnly && !(registry instanceof RuntimeReflectionSupport)) {
ImageSingletons.lookup(RuntimeReflectionSupport.class).register(condition, false, executable);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,11 +408,7 @@ private static JNIAccessibleClass addClass(Class<?> classObj, DuringAnalysisAcce
}
return JNIReflectionDictionary.singleton().addClassIfAbsent(classObj, c -> {
AnalysisType analysisClass = access.getMetaAccess().lookupJavaType(classObj);
if (analysisClass.isInterface() || (analysisClass.isInstanceClass() && analysisClass.isAbstract())) {
analysisClass.registerAsReachable("is accessed via JNI");
} else {
analysisClass.registerAsInstantiated("is accessed via JNI");
}
analysisClass.registerAsReachable("is accessed via JNI");
return new JNIAccessibleClass(classObj);
});
}
Expand Down

0 comments on commit 321fcae

Please sign in to comment.