Skip to content

Commit

Permalink
8263968: CDS: java/lang/ModuleLayer.EMPTY_LAYER should be singleton
Browse files Browse the repository at this point in the history
Reviewed-by: iklam, dholmes, alanb, redestad
  • Loading branch information
Aleksei Voitylov authored and cl4es committed Mar 24, 2021
1 parent 3aee5ad commit 133a63b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/hotspot/share/memory/heapShared.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ static ArchivableStaticFieldInfo closed_archive_subgraph_entry_fields[] = {
static ArchivableStaticFieldInfo open_archive_subgraph_entry_fields[] = {
{"jdk/internal/module/ArchivedModuleGraph", "archivedModuleGraph"},
{"java/util/ImmutableCollections", "archivedObjects"},
{"java/lang/ModuleLayer", "EMPTY_LAYER"},
{"java/lang/module/Configuration", "EMPTY_CONFIGURATION"},
{"jdk/internal/math/FDBigInteger", "archivedCaches"},
};
Expand Down
14 changes: 11 additions & 3 deletions src/java.base/share/classes/java/lang/ModuleLayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
import jdk.internal.loader.Loader;
import jdk.internal.loader.LoaderPool;
import jdk.internal.module.ServicesCatalog;
import jdk.internal.misc.CDS;
import jdk.internal.vm.annotation.Stable;
import sun.security.util.SecurityConstants;


Expand Down Expand Up @@ -147,9 +149,15 @@

public final class ModuleLayer {

// the empty layer
private static final ModuleLayer EMPTY_LAYER
= new ModuleLayer(Configuration.empty(), List.of(), null);
// the empty layer (may be initialized from the CDS archive)
private static @Stable ModuleLayer EMPTY_LAYER;
static {
CDS.initializeFromArchive(ModuleLayer.class);
if (EMPTY_LAYER == null) {
// create a new empty layer if there is no archived version.
EMPTY_LAYER = new ModuleLayer(Configuration.empty(), List.of(), null);
}
}

// the configuration from which this layer was created
private final Configuration cf;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public static void main(String args[]) throws Exception {
checkModuleDescriptors(expectArchivedDescriptors);
checkConfiguration(expectArchivedConfiguration);
checkEmptyConfiguration(expectArchivedConfiguration);
checkEmptyLayer();
}

private static void checkModuleDescriptors(boolean expectArchivedDescriptors) {
Expand Down Expand Up @@ -139,4 +140,13 @@ private static void checkConfiguration(boolean expectArchivedConfiguration) {
}
}
}

private static void checkEmptyLayer() {
// ModuleLayer.EMPTY_FIELD returned by empty() method is singleton.
// Check that with CDS there is still a single instance of EMPTY_LAYER
// and boot() layer parent is THE empty layer.
if (ModuleLayer.empty() != ModuleLayer.boot().parents().get(0)) {
throw new RuntimeException("FAILED. Empty module layer is not singleton");
}
}
}

0 comments on commit 133a63b

Please sign in to comment.