Skip to content
This repository was archived by the owner on Feb 3, 2023. It is now read-only.

Commit 5375fda

Browse files
author
Alexander Scherbatiy
committed
8263968: CDS: java/lang/ModuleLayer.EMPTY_LAYER should be singleton
Backport-of: 133a63b4a1a2ef2e4a51fdf9edac073078692f39
1 parent 75ed034 commit 5375fda

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

src/hotspot/share/memory/heapShared.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ static ArchivableStaticFieldInfo closed_archive_subgraph_entry_fields[] = {
9292
static ArchivableStaticFieldInfo open_archive_subgraph_entry_fields[] = {
9393
{"jdk/internal/module/ArchivedModuleGraph", "archivedModuleGraph"},
9494
{"java/util/ImmutableCollections", "archivedObjects"},
95+
{"java/lang/ModuleLayer", "EMPTY_LAYER"},
9596
{"java/lang/module/Configuration", "EMPTY_CONFIGURATION"},
9697
{"jdk/internal/math/FDBigInteger", "archivedCaches"},
9798
};

src/java.base/share/classes/java/lang/ModuleLayer.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
import jdk.internal.loader.Loader;
4949
import jdk.internal.loader.LoaderPool;
5050
import jdk.internal.module.ServicesCatalog;
51+
import jdk.internal.misc.CDS;
52+
import jdk.internal.vm.annotation.Stable;
5153
import sun.security.util.SecurityConstants;
5254

5355

@@ -147,9 +149,15 @@
147149

148150
public final class ModuleLayer {
149151

150-
// the empty layer
151-
private static final ModuleLayer EMPTY_LAYER
152-
= new ModuleLayer(Configuration.empty(), List.of(), null);
152+
// the empty layer (may be initialized from the CDS archive)
153+
private static @Stable ModuleLayer EMPTY_LAYER;
154+
static {
155+
CDS.initializeFromArchive(ModuleLayer.class);
156+
if (EMPTY_LAYER == null) {
157+
// create a new empty layer if there is no archived version.
158+
EMPTY_LAYER = new ModuleLayer(Configuration.empty(), List.of(), null);
159+
}
160+
}
153161

154162
// the configuration from which this layer was created
155163
private final Configuration cf;

test/hotspot/jtreg/runtime/cds/appcds/cacheObject/CheckArchivedModuleApp.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public static void main(String args[]) throws Exception {
6161
checkModuleDescriptors(expectArchivedDescriptors);
6262
checkConfiguration(expectArchivedConfiguration);
6363
checkEmptyConfiguration(expectArchivedConfiguration);
64+
checkEmptyLayer();
6465
}
6566

6667
private static void checkModuleDescriptors(boolean expectArchivedDescriptors) {
@@ -139,4 +140,13 @@ private static void checkConfiguration(boolean expectArchivedConfiguration) {
139140
}
140141
}
141142
}
143+
144+
private static void checkEmptyLayer() {
145+
// ModuleLayer.EMPTY_FIELD returned by empty() method is singleton.
146+
// Check that with CDS there is still a single instance of EMPTY_LAYER
147+
// and boot() layer parent is THE empty layer.
148+
if (ModuleLayer.empty() != ModuleLayer.boot().parents().get(0)) {
149+
throw new RuntimeException("FAILED. Empty module layer is not singleton");
150+
}
151+
}
142152
}

0 commit comments

Comments
 (0)