|
| 1 | +package org.graalvm.nativeimage.hosted; |
| 2 | + |
| 3 | +import java.lang.reflect.Executable; |
| 4 | +import java.lang.reflect.Field; |
| 5 | +import java.util.Arrays; |
| 6 | +import java.util.Locale; |
| 7 | +import java.util.Objects; |
| 8 | + |
| 9 | +import org.graalvm.nativeimage.ImageSingletons; |
| 10 | +import org.graalvm.nativeimage.impl.RuntimeResourceSupport; |
| 11 | + |
| 12 | +/** |
| 13 | + * Dynamic access allows users to register classes, methods, fields that should be available for |
| 14 | + * reflection, serialization and JNI access at runtime, as well as registration of Java resources |
| 15 | + * and ResourceBundles that should be available at runtime. |
| 16 | + * |
| 17 | + * It should only be used at {@link Feature#afterRegistration}. |
| 18 | + */ |
| 19 | +public interface DynamicAccess { |
| 20 | + /** |
| 21 | + * Makes the provided classes available for reflection at run time and all of their accessible |
| 22 | + * members available for reflection queries at run time. A call to {@link Class#forName} for the |
| 23 | + * names of the classes will return the classes at run time. |
| 24 | + * |
| 25 | + * @param condition needs to be satisfied for inclusion of types for reflection at runtime |
| 26 | + */ |
| 27 | + void registerForReflection(InclusionCondition condition, Class<?>... classes); |
| 28 | + |
| 29 | + /** |
| 30 | + * Makes the provided class available for reflection at run time if the condition is satisfied. |
| 31 | + * A call to {@link Class#forName} for the name of the class will return the class (if it |
| 32 | + * exists) or a {@link ClassNotFoundException} at run time. |
| 33 | + */ |
| 34 | + void registerClassLookup(InclusionCondition condition, String className); |
| 35 | + |
| 36 | + /** |
| 37 | + * Makes the provided methods available for reflection at run time if the condition is |
| 38 | + * satisfied. The methods will be returned by {@link Class#getMethod}, |
| 39 | + * {@link Class#getDeclaredMethod(String, Class[])}, and all the other methods on {@link Class} |
| 40 | + * that return a single method. |
| 41 | + */ |
| 42 | + void registerForReflection(InclusionCondition condition, Executable... methods); |
| 43 | + |
| 44 | + /** |
| 45 | + * Makes the provided classes available for serialization and reflection at runtime if the |
| 46 | + * condition is satisfied. |
| 47 | + */ |
| 48 | + void registerForSerialization(InclusionCondition condition, Class<?>... classes); |
| 49 | + |
| 50 | + /** |
| 51 | + * Makes the provided classes available for JNI access at run time if the condition is |
| 52 | + * satisfied. Needed when native code looks up Java classes via <a href= |
| 53 | + * "https://docs.oracle.com/en/java/javase/17/docs/specs/jni/functions.html#findclass">FindClass</a>. |
| 54 | + */ |
| 55 | + void registerForJNIAccess(InclusionCondition condition, Class<?>... classes); |
| 56 | + |
| 57 | + /** |
| 58 | + * Makes the provided methods available for JNI access at run time if the condition is |
| 59 | + * satisfied. Needed when native code looks up Java methods via <a href= |
| 60 | + * "https://docs.oracle.com/en/java/javase/17/docs/specs/jni/functions.html#getmethodid">GetMethodID</a> |
| 61 | + * or <a href= |
| 62 | + * "https://docs.oracle.com/en/java/javase/17/docs/specs/jni/functions.html#getstaticmethodid">GetStaticMethodID</a>. |
| 63 | + */ |
| 64 | + void registerForJNIAccess(InclusionCondition condition, Executable... methods); |
| 65 | + |
| 66 | + /** |
| 67 | + * Makes the provided methods available for JNI access at run time if the condition is |
| 68 | + * satisfied. Needed when native code looks up Java methods via <a href= |
| 69 | + * "https://docs.oracle.com/en/java/javase/17/docs/specs/jni/functions.html#getmethodid">GetMethodID</a> |
| 70 | + * or <a href= |
| 71 | + * "https://docs.oracle.com/en/java/javase/17/docs/specs/jni/functions.html#getstaticmethodid">GetStaticMethodID</a>. |
| 72 | + */ |
| 73 | + void registerForJNIAccess(InclusionCondition condition, Field... fields); |
| 74 | + |
| 75 | + /// TO-DO: Ask what is best for resource registration |
| 76 | + |
| 77 | + /** |
| 78 | + * Make Java resource {@code resourcePath} from {@code module} available at run time. If the |
| 79 | + * given {@code module} is unnamed, the resource is looked up on the classpath instead. |
| 80 | + */ |
| 81 | + public static void addResource(Module module, String resourcePath) { |
| 82 | + Objects.requireNonNull(module); |
| 83 | + Objects.requireNonNull(resourcePath); |
| 84 | + ImageSingletons.lookup(RuntimeResourceSupport.class).addResource(module, resourcePath, "Manually added via RuntimeResourceAccess"); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Inject a Java resource at {@code resourcePath} in {@code module} with the specified |
| 89 | + * {@code resourceContent}. At runtime the resource can be accessed as if it was part of the |
| 90 | + * original application. If the given {@code module} is unnamed, the resource is placed on the |
| 91 | + * classpath instead. |
| 92 | + */ |
| 93 | + public static void addResource(Module module, String resourcePath, byte[] resourceContent) { |
| 94 | + Objects.requireNonNull(module); |
| 95 | + Objects.requireNonNull(resourcePath); |
| 96 | + Objects.requireNonNull(resourceContent); |
| 97 | + ImageSingletons.lookup(RuntimeResourceSupport.class).injectResource(module, resourcePath, resourceContent, "Manually added via RuntimeResourceAccess"); |
| 98 | + ImageSingletons.lookup(RuntimeResourceSupport.class).addCondition(InclusionCondition.alwaysTrue(), module, resourcePath); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Make Java ResourceBundle that is specified by a {@code baseBundleName} and {@code locales} |
| 103 | + * from module {@code module} available at run time. If the given {@code module} is unnamed, the |
| 104 | + * ResourceBundle is looked up on the classpath instead. |
| 105 | + */ |
| 106 | + public static void addResourceBundle(Module module, String baseBundleName, Locale[] locales) { |
| 107 | + Objects.requireNonNull(locales); |
| 108 | + RuntimeResourceSupport.singleton().addResourceBundles(InclusionCondition.alwaysTrue(), |
| 109 | + withModuleName(module, baseBundleName), Arrays.asList(locales)); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Make Java ResourceBundle that is specified by a {@code bundleName} from module {@code module} |
| 114 | + * available at run time. If the given {@code module} is unnamed, the ResourceBundle is looked |
| 115 | + * up on the classpath instead. |
| 116 | + */ |
| 117 | + public static void addResourceBundle(Module module, String bundleName) { |
| 118 | + RuntimeResourceSupport.singleton().addResourceBundles(InclusionCondition.alwaysTrue(), |
| 119 | + withModuleName(module, bundleName)); |
| 120 | + } |
| 121 | + |
| 122 | + private static String withModuleName(Module module, String str) { |
| 123 | + Objects.requireNonNull(module); |
| 124 | + Objects.requireNonNull(str); |
| 125 | + return (module.isNamed() ? module.getName() : "ALL-UNNAMED") + ":" + str; |
| 126 | + } |
| 127 | +} |
0 commit comments