Interface ResourceAccess


public interface ResourceAccess
This interface is used to register Java resources and ResourceBundles that should be accessible at run time. An instance of this interface is acquired via Feature.AfterRegistrationAccess.getResourceAccess().

All methods in ResourceAccess require a AccessCondition as their first parameter. Resources will be registered for run-time access only if the specified condition is satisfied.

How to use

ResourceAccess should only be used during Feature.afterRegistration(Feature.AfterRegistrationAccess). Attempts to register metadata in any other phase of the Feature API will result in an error.

Example:

@Override
public void afterRegistration(AfterRegistrationAccess access) {
    ResourceAccess resources = access.getResourceAccess();
    AccessCondition condition = AccessCondition.typeReached(ConditionType.class);

    resources.register(condition, "example/**");

    resources.register(AccessCondition.unconditional(), "example/directory/concreteResource");

    resources.register(condition, "example/concreteResourceStar\\*");

    try {
        ResourceBundle bundle = ResourceBundle.getBundle("bundlePath1");
        resources.registerResourceBundle(condition, bundle);
    } catch (MissingResourceException e) {
        // Log or handle the missing resource
    }
}
Since:
25.0.1
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    register(AccessCondition condition, Module module, String glob)
    Registers resources matching the specified glob in the provided module for run-time access, if the condition is satisfied.
    default void
    register(AccessCondition condition, String glob)
    Registers resources matching the specified glob from the classpath for run-time access, if the condition is satisfied.
    void
    Registers the provided ResourceBundles for run-time access, if the condition is satisfied.
  • Method Details

    • register

      void register(AccessCondition condition, Module module, String glob)
      Registers resources matching the specified glob in the provided module for run-time access, if the condition is satisfied.

      The glob uses a subset of glob-pattern rules for specifying resources.

      There are several rules to be observed when specifying a resource path:

      • The native-image tool supports only star (*) and globstar (**) wildcard patterns.
        • By definition, star can match any number of any characters on one level while globstar can match any number of characters at any level.
        • If there is a need to treat a star literally (without special meaning), it can be escaped using \ (for example, \*).
      • In the glob, a level represents a part of the pattern separated with /.
      • When writing glob patterns the following rules must be observed:
        • Glob cannot be empty (for example, "")
        • Glob cannot end with a trailing slash ('/') (for example, "foo/bar/")
        • Glob cannot contain more than two consecutive (non-escaped) * characters on one level (for example, "foo/***/")
        • Glob cannot contain empty levels (for example, "foo//bar")
        • Glob cannot contain two consecutive globstar wildcards (example, "foo/**/**")
        • Glob cannot have other content on the same level as globstar wildcard (for example, "foo/**bar/x")

      Given the following project structure:

      app/src/main/resources/{Resource0.txt, Resource1.txt}
      

      You can:

      • Include all resources with glob **/Resource*.txt
      • Include Resource0.txt with glob **/Resource0.txt
      • Include Resource0.txt and Resource1.txt with globs **/Resource0.txt and **/Resource1.txt
      Parameters:
      condition - the condition that must be satisfied to register the target resource
      module - the Java module instance that contains target resources. If the provided value is null or an unnamed module, resources are looked up on the classpath instead.
      glob - the glob that is matched against all resources
      Since:
      25.0.1
    • register

      default void register(AccessCondition condition, String glob)
      Registers resources matching the specified glob from the classpath for run-time access, if the condition is satisfied.

      The glob uses a subset of glob-pattern rules for specifying resources.

      There are several rules to be observed when specifying a resource path:

      • The native-image tool supports only star (*) and globstar (**) wildcard patterns.
        • By definition, star can match any number of any characters on one level while globstar can match any number of characters at any level.
        • If there is a need to treat a star literally (without special meaning), it can be escaped using \ (for example, \*).
      • In the glob, a level represents a part of the pattern separated with /.
      • When writing glob patterns the following rules must be observed:
        • Glob cannot be empty (for example, "")
        • Glob cannot end with a trailing slash ('/') (for example, "foo/bar/")
        • Glob cannot contain more than two consecutive (non-escaped) * characters on one level (for example, "foo/***/")
        • Glob cannot contain empty levels (for example, "foo//bar")
        • Glob cannot contain two consecutive globstar wildcards (example, "foo/**/**")
        • Glob cannot have other content on the same level as globstar wildcard (for example, "foo/**bar/x")
      Given the following project structure:
      app/src/main/resources/{Resource0.txt, Resource1.txt}
      

      You can:

      • Include all resources with glob **/Resource*.txt
      • Include Resource0.txt with glob **/Resource0.txt
      • Include Resource0.txt and Resource1.txt} with globs **/Resource0.txt and **/Resource1.txt
      Parameters:
      condition - the condition that must be satisfied to register the target resource
      glob - the glob that is matched against all resources
      Since:
      25.0.1
    • registerResourceBundle

      void registerResourceBundle(AccessCondition condition, ResourceBundle... bundles)
      Registers the provided ResourceBundles for run-time access, if the condition is satisfied. Each ResourceBundle must be obtained using any version of the ResourceBundle.getBundle(String) method. A bundle created in any other way will result in an error.

      Recommended use to avoid crashes during image building:

      @Override
      public void afterRegistration(AfterRegistrationAccess access) {
          ResourceAccess resources = access.getResourceAccess();
          AccessCondition condition = AccessCondition.typeReached(ConditionType.class);
          try {
              ResourceBundle bundle1 = ResourceBundle.getBundle("bundlePath1");
              ResourceBundle bundle2 = ResourceBundle.getBundle("bundlePath2");
              resources.registerResourceBundle(condition, bundle1, bundle2);
          } catch (MissingResourceException e) {
              // Log or handle the missing resource
          }
      }
      
      Since:
      25.0.1