8132527: jimage tool extract and recreate options are not consistent (no NPE)
authorjfdenise
Tue, 18 Aug 2015 15:46:01 +0200
changeset 32226 786769ae25c0
parent 32225 d360340c4fbf
child 32227 34721a47bc92
8132527: jimage tool extract and recreate options are not consistent (no NPE) Summary: Module metadata original name must be reused when recreating a jimage Reviewed-by: jlaskey
jdk/src/java.base/share/classes/jdk/internal/jimage/ImageFileCreator.java
jdk/src/jdk.dev/share/classes/jdk/tools/jimage/ExtractedImage.java
jdk/test/jdk/internal/jimage/JImageTest.java
--- a/jdk/src/java.base/share/classes/jdk/internal/jimage/ImageFileCreator.java	Mon Aug 17 11:18:31 2015 -0700
+++ b/jdk/src/java.base/share/classes/jdk/internal/jimage/ImageFileCreator.java	Tue Aug 18 15:46:01 2015 +0200
@@ -139,6 +139,7 @@
     }
 
     public static void recreateJimage(Path jimageFile,
+            String jdataName,
             Set<Archive> archives,
             Map<String, Set<String>> modulePackages)
             throws IOException {
@@ -159,12 +160,7 @@
             throw new UnsupportedOperationException("Not supported, no external file "
                     + "in a jimage file");
         }, entriesForModule, order);
-        String fileName = jimageFile.getFileName().toString();
-        if (fileName.endsWith(IMAGE_EXT)) {
-            fileName = fileName.substring(0, fileName.length()
-                    - BasicImageWriter.IMAGE_EXT.length());
-        }
-        generateJImage(jimageFile, fileName, resources, order);
+        generateJImage(jimageFile, jdataName, resources, order);
     }
 
     private void writeImage(String fileName,
--- a/jdk/src/jdk.dev/share/classes/jdk/tools/jimage/ExtractedImage.java	Mon Aug 17 11:18:31 2015 -0700
+++ b/jdk/src/jdk.dev/share/classes/jdk/tools/jimage/ExtractedImage.java	Tue Aug 18 15:46:01 2015 +0200
@@ -164,17 +164,19 @@
     private Set<Archive> archives = new HashSet<>();
     private final PrintWriter log;
     private final boolean verbose;
-
+    private final String jdataName;
     ExtractedImage(Path dirPath, PrintWriter log,
             boolean verbose) throws IOException {
         if (!Files.isDirectory(dirPath)) {
             throw new IOException("Not a directory");
         }
+        List<String> jdataNameHolder = new ArrayList<>();
         Files.walk(dirPath, 1).forEach((p) -> {
             try {
                 if (!dirPath.equals(p)) {
                     String name = getPathName(p);
                     if (name.endsWith(ImageModuleData.META_DATA_EXTENSION)) {
+                        jdataNameHolder.add(p.getFileName().toString());
                         List<String> lines = Files.readAllLines(p);
                         for (Entry<String, List<String>> entry
                                 : ImageModuleDataWriter.toModulePackages(lines).entrySet()) {
@@ -197,11 +199,22 @@
         archives = Collections.unmodifiableSet(archives);
         this.log = log;
         this.verbose = verbose;
+        if (jdataNameHolder.size() != 1) {
+            throw new IOException("Wrong module information");
+        }
+        // The name of the metadata resource must be reused in the recreated jimage
+        String name = jdataNameHolder.get(0);
+        // Extension will be added when recreating the jimage
+        if (name.endsWith(ImageModuleData.META_DATA_EXTENSION)) {
+            name = name.substring(0, name.length()
+                    - ImageModuleData.META_DATA_EXTENSION.length());
+        }
+        jdataName = name;
     }
 
     void recreateJImage(Path path) throws IOException {
 
-        ImageFileCreator.recreateJimage(path, archives, modulePackages);
+        ImageFileCreator.recreateJimage(path, jdataName, archives, modulePackages);
     }
 
     private static String getPathName(Path path) {
--- a/jdk/test/jdk/internal/jimage/JImageTest.java	Mon Aug 17 11:18:31 2015 -0700
+++ b/jdk/test/jdk/internal/jimage/JImageTest.java	Tue Aug 18 15:46:01 2015 +0200
@@ -66,9 +66,10 @@
             String bootimage = bootimagePath.toAbsolutePath().toString();
             String extractDir = Paths.get(".", "extract").toAbsolutePath().toString();
             String recreateImage = Paths.get(".", "recreate.jimage").toAbsolutePath().toString();
-
+            String relativeRecreateImage = Paths.get(".", "recreate2.jimage").toString();
             jimage("extract", "--dir", extractDir, bootimage);
             jimage("recreate", "--dir", extractDir, recreateImage);
+            jimage("recreate", "--dir", extractDir, relativeRecreateImage);
 
             System.out.println("Test successful");
          } else {