Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8341143

Allow patching module loaded into a dynamic ModuleLayer

XMLWordPrintable

      A DESCRIPTION OF THE PROBLEM :
      Maybe I'm wrong, but as of JDK 23, it is not possible to patch a module loaded using a dynamic ModuleLayer.
      The --patch-module option only allows patching modules that are available at boot time.
      The module API does not offer any function like addPatch().

      To handle this case, as a workaround, I am currently patching the java.base module with the following class:
      package javapatch.lang.module;

      import java.lang.module.ModuleFinder;
      import java.lang.module.ModuleReference;
      import java.nio.file.Path;
      import java.util.List;
      import java.util.Map;
      import java.util.Objects;
      import java.util.Optional;
      import java.util.Set;

      import jdk.internal.module.ModulePath;
      import jdk.internal.module.ModulePatcher;

      public class PatchedModuleFinder {
          public static ModuleFinder of(Map<String, List<String>> patches, Path... entries) {
              // special case zero entries
              if (entries.length == 0) {
                  return new ModuleFinder() {
                      @Override
                      public Optional<ModuleReference> find(String name) {
                          Objects.requireNonNull(name);
                          return Optional.empty();
                      }

                      @Override
                      public Set<ModuleReference> findAll() {
                          return Set.of();
                      }
                  };
              }

              return ModulePath.of(new ModulePatcher(patches), entries);
          }
      }

      The function PatchedModuleFinder.of() is a modified copy of ModuleFinder.of(), which takes an additional argument containing the map of patches to apply to the modules found by the finder.
      It only instantiates a ModulePatcher and provides it to ModulePath.of().

      This patch seems to work (at least for now), and I think adding this static function to ModuleFinder would be a great addition (at least for me), as it would allow configuring a dynamic ModuleLayer with the complete feature set available to the boot layer.

      As I'm not familiar with the intricacies of JPMS, there might be a good reason for not allowing patching in a dynamic ModuleLayer.

      I hope you will find this simple change worthy of modifying the module API.
      Best regards,
      Pascal


            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: