Skip to content

Commit 583b5f5

Browse files
david-beaumontRoger Riggs
authored andcommitted
8371429: [lworld] Fix package name inference for ResourcePoolManager for preview mode
Reviewed-by: rriggs
1 parent c4030c8 commit 583b5f5

File tree

3 files changed

+141
-195
lines changed

3 files changed

+141
-195
lines changed

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

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -572,62 +572,4 @@ private static ResourcePoolManager createPoolManager(ResourcePool resultResource
572572
resultResources.entries().forEach(resources::add);
573573
return resources;
574574
}
575-
576-
/**
577-
* Helper method that splits a Resource path onto 3 items: module, parent
578-
* and resource name.
579-
*
580-
* @param path
581-
* @return An array containing module, parent and name.
582-
*/
583-
public static String[] splitPath(String path) {
584-
Objects.requireNonNull(path);
585-
String noRoot = path.substring(1);
586-
int pkgStart = noRoot.indexOf("/");
587-
String module = noRoot.substring(0, pkgStart);
588-
List<String> result = new ArrayList<>();
589-
result.add(module);
590-
String pkg = noRoot.substring(pkgStart + 1);
591-
String resName;
592-
int pkgEnd = pkg.lastIndexOf("/");
593-
if (pkgEnd == -1) { // No package.
594-
resName = pkg;
595-
} else {
596-
resName = pkg.substring(pkgEnd + 1);
597-
}
598-
599-
pkg = toPackage(pkg, false);
600-
result.add(pkg);
601-
result.add(resName);
602-
603-
String[] array = new String[result.size()];
604-
return result.toArray(array);
605-
}
606-
607-
/**
608-
* Returns the path of the resource.
609-
*/
610-
public static String resourceName(String path) {
611-
Objects.requireNonNull(path);
612-
String s = path.substring(1);
613-
int index = s.indexOf("/");
614-
return s.substring(index + 1);
615-
}
616-
617-
public static String toPackage(String name) {
618-
return toPackage(name, false);
619-
}
620-
621-
private static String toPackage(String name, boolean log) {
622-
int index = name.lastIndexOf('/');
623-
if (index > 0) {
624-
return name.substring(0, index).replace('/', '.');
625-
} else {
626-
// ## unnamed package
627-
if (log) {
628-
System.err.format("Warning: %s in unnamed package%n", name);
629-
}
630-
return "";
631-
}
632-
}
633575
}

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

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)