@@ -66,20 +66,8 @@ static Attributes readModuleAttributes(ResourcePoolModule mod) {
6666 }
6767 }
6868
69- /**
70- * Returns true if a resource is located in a named package.
71- */
72- public static boolean isNamedPackageResource (String name ) {
73- int index = name .lastIndexOf ("/" );
74- if (index == -1 ) {
75- return false ;
76- } else {
77- String pn = name .substring (0 , index ).replace ('/' , '.' );
78- return Checks .isPackageName (pn );
79- }
80- }
81-
8269 static class ResourcePoolModuleImpl implements ResourcePoolModule {
70+ private static final String PREVIEW_PREFIX = "META-INF/preview/" ;
8371
8472 final Map <String , ResourcePoolEntry > moduleContent = new LinkedHashMap <>();
8573 // lazily initialized
@@ -132,16 +120,8 @@ private void initModuleAttributes() {
132120 public Set <String > packages () {
133121 Set <String > pkgs = new HashSet <>();
134122 moduleContent .values ().stream ()
135- .filter (m -> m .type () == ResourcePoolEntry .Type .CLASS_OR_RESOURCE )
136- .forEach (res -> {
137- String name = ImageFileCreator .resourceName (res .path ());
138- if (isNamedPackageResource (name )) {
139- String pkg = ImageFileCreator .toPackage (name );
140- if (!pkg .isEmpty ()) {
141- pkgs .add (pkg );
142- }
143- }
144- });
123+ .filter (m -> m .type () == ResourcePoolEntry .Type .CLASS_OR_RESOURCE )
124+ .forEach (res -> inferPackageName (res ).ifPresent (pkgs ::add ));
145125 return pkgs ;
146126 }
147127
@@ -159,6 +139,39 @@ public Stream<ResourcePoolEntry> entries() {
159139 public int entryCount () {
160140 return moduleContent .values ().size ();
161141 }
142+
143+ /**
144+ * Returns a valid non-empty package name, inferred from a resource pool
145+ * entry's path.
146+ *
147+ * <p>If the resource pool entry is for a preview resource (i.e. with
148+ * path {@code "/mod-name/META-INF/preview/pkg-path/resource-name"})
149+ * the package name is the non-preview name based on {@code "pkg-path"}.
150+ *
151+ * @return the inferred package name, or {@link Optional#empty() empty}
152+ * if no name could be inferred.
153+ */
154+ private static Optional <String > inferPackageName (ResourcePoolEntry res ) {
155+ // Expect entry paths to be "/mod-name/pkg-path/resource-name", but
156+ // may also get "/mod-name/META-INF/preview/pkg-path/resource-name"
157+ String name = res .path ();
158+ if (name .charAt (0 ) == '/' ) {
159+ int pkgStart = name .indexOf ('/' , 1 ) + 1 ;
160+ int pkgEnd = name .lastIndexOf ('/' );
161+ if (pkgStart > 0 && pkgEnd > pkgStart ) {
162+ String pkgPath = name .substring (pkgStart , pkgEnd );
163+ // Handle preview paths by removing the prefix.
164+ if (pkgPath .startsWith (PREVIEW_PREFIX )) {
165+ pkgPath = pkgPath .substring (PREVIEW_PREFIX .length ());
166+ }
167+ String pkgName = pkgPath .replace ('/' , '.' );
168+ if (Checks .isPackageName (pkgName )) {
169+ return Optional .of (pkgName );
170+ }
171+ }
172+ }
173+ return Optional .empty ();
174+ }
162175 }
163176
164177 public class ResourcePoolImpl implements ResourcePool {
0 commit comments