|
| 1 | +package com.oracle.svm.hosted; |
| 2 | + |
| 3 | +import java.lang.reflect.Executable; |
| 4 | +import java.lang.reflect.Field; |
| 5 | +import java.util.Arrays; |
| 6 | + |
| 7 | +import org.graalvm.nativeimage.hosted.Feature; |
| 8 | +import org.graalvm.nativeimage.hosted.ReflectionDynamicAccess; |
| 9 | +import org.graalvm.nativeimage.hosted.RegistrationCondition; |
| 10 | + |
| 11 | +import com.oracle.svm.core.util.UserError; |
| 12 | + |
| 13 | +/** |
| 14 | + * Instance of this class is used to register classes, methods, and fields for reflection, |
| 15 | + * serialization and JNI access at runtime. It can only be created at |
| 16 | + * {@link Feature#afterRegistration} via {@link Feature.AfterRegistrationAccess}. |
| 17 | + */ |
| 18 | +public final class ReflectionDynamicAccessImpl implements ReflectionDynamicAccess { |
| 19 | + |
| 20 | + private static boolean afterRegistrationFinished; |
| 21 | + private static InternalReflectionDynamicAccessImpl rdaInstance; |
| 22 | + |
| 23 | + public ReflectionDynamicAccessImpl() { |
| 24 | + rdaInstance = new InternalReflectionDynamicAccessImpl(); |
| 25 | + afterRegistrationFinished = false; |
| 26 | + } |
| 27 | + |
| 28 | + public static void setAfterRegistrationFinished() { |
| 29 | + afterRegistrationFinished = true; |
| 30 | + } |
| 31 | + |
| 32 | + @Override |
| 33 | + public void register(RegistrationCondition condition, Class<?>... classes) { |
| 34 | + UserError.guarantee(!afterRegistrationFinished, "There shouldn't be a registration for runtime access after afterRegistration period. You tried to register: %s", |
| 35 | + Arrays.toString(classes)); |
| 36 | + rdaInstance.register(condition, classes); |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public void registerClassLookup(RegistrationCondition condition, String className) { |
| 41 | + UserError.guarantee(!afterRegistrationFinished, "There shouldn't be a registration for runtime access after afterRegistration period. You tried to register: %s", |
| 42 | + className); |
| 43 | + rdaInstance.registerClassLookup(condition, className); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public void register(RegistrationCondition condition, Executable... methods) { |
| 48 | + UserError.guarantee(!afterRegistrationFinished, "There shouldn't be a registration for runtime access after afterRegistration period. You tried to register: %s", |
| 49 | + Arrays.toString(methods)); |
| 50 | + rdaInstance.register(condition, methods); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public void register(RegistrationCondition condition, Field... fields) { |
| 55 | + UserError.guarantee(!afterRegistrationFinished, "There shouldn't be a registration for runtime access after afterRegistration period. You tried to register: %s", |
| 56 | + Arrays.toString(fields)); |
| 57 | + rdaInstance.register(condition, fields); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public void registerForSerialization(RegistrationCondition condition, Class<?>... classes) { |
| 62 | + UserError.guarantee(!afterRegistrationFinished, "There shouldn't be a registration for runtime access after afterRegistration period. You tried to register: %s", |
| 63 | + Arrays.toString(classes)); |
| 64 | + rdaInstance.registerForSerialization(condition, classes); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public void registerForJNIAccess(RegistrationCondition condition, Class<?>... classes) { |
| 69 | + UserError.guarantee(!afterRegistrationFinished, "There shouldn't be a registration for runtime access after afterRegistration period. You tried to register: %s", |
| 70 | + Arrays.toString(classes)); |
| 71 | + rdaInstance.registerForJNIAccess(condition, classes); |
| 72 | + } |
| 73 | +} |
0 commit comments