Skip to content
Next Next commit
jimage writer changes to support preview mode.
  • Loading branch information
david-beaumont committed Nov 3, 2025
commit 9d7f5f3e267ae078c7349e070b1cc1f201dd26a3
24 changes: 24 additions & 0 deletions src/java.base/share/classes/jdk/internal/jimage/ImageLocation.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
package jdk.internal.jimage;

import java.nio.ByteBuffer;
import java.util.List;
import java.util.Objects;
import java.util.function.Predicate;

Expand Down Expand Up @@ -159,6 +160,29 @@ public static int getFlags(String name, Predicate<String> hasEntry) {
}
}

/**
* Helper function to calculate package flags for {@code "/packages/xxx"}
* directory entries.
*
* <p>Based on the module references, the flags are:
* <ul>
* <li>{@code FLAGS_HAS_PREVIEW_VERSION} if <em>any</em> referenced
* package has a preview version.
* <li>{@code FLAGS_IS_PREVIEW_ONLY} if <em>all</em> referenced packages
* are preview only.
* </ul>
*
* @return package flags for {@code "/packages/xxx"} directory entries.
*/
public static int getPackageFlags(List<ModuleReference> moduleReferences) {
boolean hasPreviewVersion =
moduleReferences.stream().anyMatch(ModuleReference::hasPreviewVersion);
boolean isPreviewOnly =
moduleReferences.stream().allMatch(ModuleReference::isPreviewOnly);
return (hasPreviewVersion ? ImageLocation.FLAGS_HAS_PREVIEW_VERSION : 0)
| (isPreviewOnly ? ImageLocation.FLAGS_IS_PREVIEW_ONLY : 0);
}

/**
* Tests a non-preview image location's flags to see if it has preview
* content associated with it.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ public final class ModuleReference implements Comparable<ModuleReference> {
/** If set, this package exists in non-preview mode. */
private static final int FLAGS_PKG_HAS_NORMAL_VERSION = 0x2;
/** If set, the associated module has resources (in normal or preview mode). */
// TODO: Make this private again when image writer code is updated.
public static final int FLAGS_PKG_HAS_RESOURCES = 0x4;
private static final int FLAGS_PKG_HAS_RESOURCES = 0x4;

/**
* References are ordered with preview versions first which permits early
Expand Down Expand Up @@ -176,9 +175,9 @@ public static Iterator<Integer> readNameOffsets(
if (bufferSize == 0 || (bufferSize & 0x1) != 0) {
throw new IllegalArgumentException("Invalid buffer size");
}
int testFlags = (includeNormal ? FLAGS_PKG_HAS_NORMAL_VERSION : 0)
int includeMask = (includeNormal ? FLAGS_PKG_HAS_NORMAL_VERSION : 0)
+ (includePreview ? FLAGS_PKG_HAS_PREVIEW_VERSION : 0);
if (testFlags == 0) {
if (includeMask == 0) {
throw new IllegalArgumentException("Invalid flags");
}

Expand All @@ -188,14 +187,7 @@ public static Iterator<Integer> readNameOffsets(
int nextIdx(int idx) {
for (; idx < bufferSize; idx += 2) {
// If any of the test flags are set, include this entry.

// Temporarily allow for *neither* flag to be set. This is what would
// be written by a 1.0 version of the jimage flag, and indicates a
// normal resource without a preview version.
// TODO: Remove the zero-check below once image writer code is updated.
int previewFlags =
buffer.get(idx) & (FLAGS_PKG_HAS_NORMAL_VERSION | FLAGS_PKG_HAS_PREVIEW_VERSION);
if (previewFlags == 0 || (previewFlags & testFlags) != 0) {
if ((buffer.get(idx) & includeMask) != 0) {
return idx;
} else if (!includeNormal) {
// Preview entries are first in the offset buffer, so we
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -29,6 +29,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import jdk.internal.jimage.ImageHeader;
import jdk.internal.jimage.ImageStream;
import jdk.internal.jimage.ImageStringsReader;
Expand Down Expand Up @@ -76,10 +77,10 @@ public String getString(int offset) {
}

public void addLocation(String fullname, long contentOffset,
long compressedSize, long uncompressedSize) {
long compressedSize, long uncompressedSize, int previewFlags) {
ImageLocationWriter location =
ImageLocationWriter.newLocation(fullname, strings,
contentOffset, compressedSize, uncompressedSize);
contentOffset, compressedSize, uncompressedSize, previewFlags);
input.add(location);
length++;
}
Expand All @@ -94,7 +95,7 @@ int getLocationsCount() {

private void generatePerfectHash() {
PerfectHashBuilder<ImageLocationWriter> builder =
new PerfectHashBuilder<>(
new PerfectHashBuilder<>(
PerfectHashBuilder.Entry.class,
PerfectHashBuilder.Bucket.class);

Expand Down
Loading