< prev index next >

src/jdk.jlink/share/classes/jdk/tools/jlink/internal/ResourcePoolManager.java

Print this page
*** 64,24 ***
          } catch (RuntimeException re) {
              throw new RuntimeException("module info cannot be read for " + mod.name(), re);
          }
      }
  
-     /**
-      * Returns true if a resource is located in a named package.
-      */
-     public static boolean isNamedPackageResource(String name) {
-         int index = name.lastIndexOf("/");
-         if (index == -1) {
-             return false;
-         } else {
-             String pn = name.substring(0, index).replace('/', '.');
-             return Checks.isPackageName(pn);
-         }
-     }
- 
      static class ResourcePoolModuleImpl implements ResourcePoolModule {
  
          final Map<String, ResourcePoolEntry> moduleContent = new LinkedHashMap<>();
          // lazily initialized
          private ModuleDescriptor descriptor;
          private ModuleTarget target;
--- 64,12 ---
          } catch (RuntimeException re) {
              throw new RuntimeException("module info cannot be read for " + mod.name(), re);
          }
      }
  
      static class ResourcePoolModuleImpl implements ResourcePoolModule {
+         private static final String PREVIEW_PREFIX = "META-INF/preview/";
  
          final Map<String, ResourcePoolEntry> moduleContent = new LinkedHashMap<>();
          // lazily initialized
          private ModuleDescriptor descriptor;
          private ModuleTarget target;

*** 130,20 ***
  
          @Override
          public Set<String> packages() {
              Set<String> pkgs = new HashSet<>();
              moduleContent.values().stream()
!                 .filter(m -> m.type() == ResourcePoolEntry.Type.CLASS_OR_RESOURCE)
!                 .forEach(res -> {
-                     String name = ImageFileCreator.resourceName(res.path());
-                     if (isNamedPackageResource(name)) {
-                         String pkg = ImageFileCreator.toPackage(name);
-                         if (!pkg.isEmpty()) {
-                             pkgs.add(pkg);
-                         }
-                     }
-                 });
              return pkgs;
          }
  
          @Override
          public String toString() {
--- 118,12 ---
  
          @Override
          public Set<String> packages() {
              Set<String> pkgs = new HashSet<>();
              moduleContent.values().stream()
!                     .filter(m -> m.type() == ResourcePoolEntry.Type.CLASS_OR_RESOURCE)
!                     .forEach(res -> inferPackageName(res).ifPresent(pkgs::add));
              return pkgs;
          }
  
          @Override
          public String toString() {

*** 157,10 ***
--- 137,43 ---
  
          @Override
          public int entryCount() {
              return moduleContent.values().size();
          }
+ 
+         /**
+          * Returns a valid non-empty package name, inferred from a resource pool
+          * entry's path.
+          *
+          * <p>If the resource pool entry is for a preview resource (i.e. with
+          * path {@code "/mod-name/META-INF/preview/pkg-path/resource-name"})
+          * the package name is the non-preview name based on {@code "pkg-path"}.
+          *
+          * @return the inferred package name, or {@link Optional#empty() empty}
+          *     if no name could be inferred.
+          */
+         private static Optional<String> inferPackageName(ResourcePoolEntry res) {
+             // Expect entry paths to be "/mod-name/pkg-path/resource-name", but
+             // may also get "/mod-name/META-INF/preview/pkg-path/resource-name"
+             String name = res.path();
+             if (name.charAt(0) == '/') {
+                 int pkgStart = name.indexOf('/', 1) + 1;
+                 int pkgEnd = name.lastIndexOf('/');
+                 if (pkgStart > 0 && pkgEnd > pkgStart) {
+                     String pkgPath = name.substring(pkgStart, pkgEnd);
+                     // Handle preview paths by removing the prefix.
+                     if (pkgPath.startsWith(PREVIEW_PREFIX)) {
+                         pkgPath = pkgPath.substring(PREVIEW_PREFIX.length());
+                     }
+                     String pkgName = pkgPath.replace('/', '.');
+                     if (Checks.isPackageName(pkgName)) {
+                         return Optional.of(pkgName);
+                     }
+                 }
+             }
+             return Optional.empty();
+         }
      }
  
      public class ResourcePoolImpl implements ResourcePool {
          @Override
          public ResourcePoolModuleView moduleView() {
< prev index next >