Skip to content

Commit 8858134

Browse files
jimage writer changes to support preview mode.
* Remove TODOs now jimage version is bumped * jimage writer changes to support preview mode.
1 parent 936b22d commit 8858134

File tree

12 files changed

+1763
-1137
lines changed

12 files changed

+1763
-1137
lines changed

src/java.base/share/classes/jdk/internal/jimage/ImageLocation.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -442,13 +442,7 @@ public LocationType getType() {
442442
case ImageStrings.EMPTY_STRING_OFFSET:
443443
// Only 2 choices, either the "/modules" or "/packages" root.
444444
assert isRootDir() : "Invalid root directory: " + getFullName();
445-
446-
// Temporary logic to handle package root classification until new
447-
// image reader code is committed which sets FLAGS_IS_PACKAGE_ROOT.
448-
// Base name is "/packages" or "/modules" (NOT "packages" and "modules").
449-
// TODO: Uncomment the FLAGS_IS_PACKAGE_ROOT test below.
450-
// return (getFlags() & FLAGS_IS_PACKAGE_ROOT) != 0
451-
return getBase().charAt(1) == 'p'
445+
return (getFlags() & FLAGS_IS_PACKAGE_ROOT) != 0
452446
? LocationType.PACKAGES_ROOT
453447
: LocationType.MODULES_ROOT;
454448
default:

src/java.base/share/classes/jdk/internal/jimage/ModuleReference.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -192,14 +192,7 @@ public static Iterator<Integer> readNameOffsets(
192192
int nextIdx(int idx) {
193193
for (; idx < bufferSize; idx += 2) {
194194
// If any of the test flags are set, include this entry.
195-
196-
// Temporarily allow for *neither* flag to be set. This is what would
197-
// be written by a 1.0 version of the jimage flag, and indicates a
198-
// normal resource without a preview version.
199-
// TODO: Remove the zero-check below once image writer code is updated.
200-
int previewFlags =
201-
buffer.get(idx) & (FLAGS_HAS_NORMAL_VERSION | FLAGS_HAS_PREVIEW_VERSION);
202-
if (previewFlags == 0 || (previewFlags & testFlags) != 0) {
195+
if ((buffer.get(idx) & testFlags) != 0) {
203196
return idx;
204197
} else if (!includeNormal) {
205198
// Preview entries are first in the offset buffer, so we

src/java.base/share/native/libjimage/imageFile.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -319,12 +319,7 @@ bool ImageFileReader::open() {
319319
!read_at((u1*)&_header, header_size, 0) ||
320320
_header.magic(_endian) != IMAGE_MAGIC ||
321321
_header.major_version(_endian) != MAJOR_VERSION ||
322-
// Temporarily, we allow either version (1.1 or 1.0) of the file to
323-
// be read so this code can be committed before image writing changes
324-
// for preview mode. Preview mode changes do not modify any structure,
325-
// so a 1.0 file will look like a jimage without any preview resources.
326-
// TODO: Restore equality check for MINOR_VERSION.
327-
_header.minor_version(_endian) > MINOR_VERSION) {
322+
_header.minor_version(_endian) != MINOR_VERSION) {
328323
close();
329324
return false;
330325
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2014, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2014, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -76,10 +76,10 @@ public String getString(int offset) {
7676
}
7777

7878
public void addLocation(String fullname, long contentOffset,
79-
long compressedSize, long uncompressedSize) {
79+
long compressedSize, long uncompressedSize, int previewFlags) {
8080
ImageLocationWriter location =
8181
ImageLocationWriter.newLocation(fullname, strings,
82-
contentOffset, compressedSize, uncompressedSize);
82+
contentOffset, compressedSize, uncompressedSize, previewFlags);
8383
input.add(location);
8484
length++;
8585
}

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

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2014, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2014, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -49,6 +49,7 @@
4949
import java.util.stream.Collectors;
5050
import java.util.stream.Stream;
5151

52+
import jdk.internal.jimage.ImageLocation;
5253
import jdk.tools.jlink.internal.Archive.Entry;
5354
import jdk.tools.jlink.internal.Archive.Entry.EntryType;
5455
import jdk.tools.jlink.internal.JRTArchive.ResourceFileEntry;
@@ -227,31 +228,8 @@ private static ResourcePool generateJImage(ResourcePoolManager allContent,
227228
DataOutputStream out,
228229
boolean generateRuntimeImage
229230
) throws IOException {
230-
ResourcePool resultResources;
231-
try {
232-
resultResources = pluginSupport.visitResources(allContent);
233-
if (generateRuntimeImage) {
234-
// Keep track of non-modules resources for linking from a run-time image
235-
resultResources = addNonClassResourcesTrackFiles(resultResources,
236-
writer);
237-
// Generate the diff between the input resources from packaged
238-
// modules in 'allContent' to the plugin- or otherwise
239-
// generated-content in 'resultResources'
240-
resultResources = addResourceDiffFiles(allContent.resourcePool(),
241-
resultResources,
242-
writer);
243-
}
244-
} catch (PluginException pe) {
245-
if (JlinkTask.DEBUG) {
246-
pe.printStackTrace();
247-
}
248-
throw pe;
249-
} catch (Exception ex) {
250-
if (JlinkTask.DEBUG) {
251-
ex.printStackTrace();
252-
}
253-
throw new IOException(ex);
254-
}
231+
ResourcePool resultResources =
232+
getResourcePool(allContent, writer, pluginSupport, generateRuntimeImage);
255233
Set<String> duplicates = new HashSet<>();
256234
long[] offset = new long[1];
257235

@@ -282,8 +260,10 @@ private static ResourcePool generateJImage(ResourcePoolManager allContent,
282260
offset[0] += onFileSize;
283261
return;
284262
}
263+
int locFlags = ImageLocation.getFlags(
264+
res.path(), p -> resultResources.findEntry(p).isPresent());
285265
duplicates.add(path);
286-
writer.addLocation(path, offset[0], compressedSize, uncompressedSize);
266+
writer.addLocation(path, offset[0], compressedSize, uncompressedSize, locFlags);
287267
paths.add(path);
288268
offset[0] += onFileSize;
289269
}
@@ -307,6 +287,40 @@ private static ResourcePool generateJImage(ResourcePoolManager allContent,
307287
return resultResources;
308288
}
309289

290+
private static ResourcePool getResourcePool(
291+
ResourcePoolManager allContent,
292+
BasicImageWriter writer,
293+
ImagePluginStack pluginSupport,
294+
boolean generateRuntimeImage)
295+
throws IOException {
296+
ResourcePool resultResources;
297+
try {
298+
resultResources = pluginSupport.visitResources(allContent);
299+
if (generateRuntimeImage) {
300+
// Keep track of non-modules resources for linking from a run-time image
301+
resultResources = addNonClassResourcesTrackFiles(resultResources,
302+
writer);
303+
// Generate the diff between the input resources from packaged
304+
// modules in 'allContent' to the plugin- or otherwise
305+
// generated-content in 'resultResources'
306+
resultResources = addResourceDiffFiles(allContent.resourcePool(),
307+
resultResources,
308+
writer);
309+
}
310+
} catch (PluginException pe) {
311+
if (JlinkTask.DEBUG) {
312+
pe.printStackTrace();
313+
}
314+
throw pe;
315+
} catch (Exception ex) {
316+
if (JlinkTask.DEBUG) {
317+
ex.printStackTrace();
318+
}
319+
throw new IOException(ex);
320+
}
321+
return resultResources;
322+
}
323+
310324
/**
311325
* Support for creating a runtime suitable for linking from the run-time
312326
* image.

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2014, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -54,8 +54,8 @@ private ImageLocationWriter addAttribute(int kind, String value) {
5454
}
5555

5656
static ImageLocationWriter newLocation(String fullName,
57-
ImageStringsWriter strings,
58-
long contentOffset, long compressedSize, long uncompressedSize) {
57+
ImageStringsWriter strings,
58+
long contentOffset, long compressedSize, long uncompressedSize, int previewFlags) {
5959
String moduleName = "";
6060
String parentName = "";
6161
String baseName;
@@ -90,13 +90,14 @@ static ImageLocationWriter newLocation(String fullName,
9090
}
9191

9292
return new ImageLocationWriter(strings)
93-
.addAttribute(ATTRIBUTE_MODULE, moduleName)
94-
.addAttribute(ATTRIBUTE_PARENT, parentName)
95-
.addAttribute(ATTRIBUTE_BASE, baseName)
96-
.addAttribute(ATTRIBUTE_EXTENSION, extensionName)
97-
.addAttribute(ATTRIBUTE_OFFSET, contentOffset)
98-
.addAttribute(ATTRIBUTE_COMPRESSED, compressedSize)
99-
.addAttribute(ATTRIBUTE_UNCOMPRESSED, uncompressedSize);
93+
.addAttribute(ATTRIBUTE_MODULE, moduleName)
94+
.addAttribute(ATTRIBUTE_PARENT, parentName)
95+
.addAttribute(ATTRIBUTE_BASE, baseName)
96+
.addAttribute(ATTRIBUTE_EXTENSION, extensionName)
97+
.addAttribute(ATTRIBUTE_OFFSET, contentOffset)
98+
.addAttribute(ATTRIBUTE_COMPRESSED, compressedSize)
99+
.addAttribute(ATTRIBUTE_UNCOMPRESSED, uncompressedSize)
100+
.addAttribute(ATTRIBUTE_PREVIEW_FLAGS, previewFlags);
100101
}
101102

102103
@Override

0 commit comments

Comments
 (0)