1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @summary Test StringSharingPluginTest
  27  * @author Jean-Francois Denise
  28  * @library ../../lib
  29  * @modules java.base/jdk.internal.jimage
  30  *          java.base/jdk.internal.jimage.decompressor
  31  *          jdk.jlink/jdk.tools.jlink.internal
  32  *          jdk.jlink/jdk.tools.jlink.internal.plugins
  33  *          jdk.jlink/jdk.tools.jmod
  34  *          jdk.jlink/jdk.tools.jimage
  35  *          jdk.jdeps/com.sun.tools.classfile
  36  *          jdk.compiler
  37  * @run build tests.*
  38  * @run main StringSharingPluginTest
  39  */
  40 
  41 import java.io.IOException;
  42 import java.io.UncheckedIOException;
  43 import java.nio.ByteBuffer;
  44 import java.nio.ByteOrder;
  45 import java.nio.file.Files;
  46 import java.nio.file.Path;
  47 import java.util.Arrays;
  48 import java.util.HashMap;
  49 import java.util.List;
  50 import java.util.Map;
  51 import java.util.function.Consumer;
  52 
  53 import jdk.internal.jimage.decompressor.CompressedResourceHeader;
  54 import jdk.internal.jimage.decompressor.StringSharingDecompressor;
  55 import jdk.tools.jlink.internal.ResourcePoolManager;
  56 import jdk.tools.jlink.internal.StringTable;
  57 import jdk.tools.jlink.internal.plugins.StringSharingPlugin;
  58 import jdk.tools.jlink.plugin.ResourcePoolEntry;
  59 import jdk.tools.jlink.plugin.ResourcePool;
  60 import jdk.tools.jlink.plugin.Plugin;
  61 import tests.Helper;
  62 import tests.JImageValidator;
  63 
  64 public class StringSharingPluginTest {
  65 
  66     private static int strID = 1;
  67 
  68     public static void main(String[] args) throws Exception {
  69         // JPRT not yet ready for jmods
  70         Helper helper = Helper.newHelper();
  71         if (helper == null) {
  72             System.err.println("Test not run, NO jmods directory");
  73             return;
  74         }
  75 
  76         List<String> classes = Arrays.asList("toto.Main", "toto.com.foo.bar.X");
  77         Path compiledClasses = helper.generateModuleCompiledClasses(
  78                 helper.getJmodSrcDir(), helper.getJmodClassesDir(), "composite2", classes);
  79 
  80         Map<String, Integer> map = new HashMap<>();
  81         Map<Integer, String> reversedMap = new HashMap<>();
  82 
  83         ResourcePoolManager resources = new ResourcePoolManager(ByteOrder.nativeOrder(), new StringTable() {
  84             @Override
  85             public int addString(String str) {
  86                 Integer id = map.get(str);
  87                 if (id == null) {
  88                     id = strID;
  89                     map.put(str, id);
  90                     reversedMap.put(id, str);
  91                     strID += 1;
  92                 }
  93                 return id;
  94             }
  95 
  96             @Override
  97             public String getString(int id) {
  98                 throw new UnsupportedOperationException("Not supported yet.");
  99             }
 100         });
 101         Consumer<Path> c = (p) -> {
 102             // take only the .class resources.
 103             if (Files.isRegularFile(p) && p.toString().endsWith(".class")
 104                     && !p.toString().endsWith("module-info.class")) {
 105                 try {
 106                     byte[] content = Files.readAllBytes(p);
 107                     String path = p.toString().replace('\\', '/');
 108                     path = path.substring("/modules".length());
 109                     if (path.charAt(0) != '/') {
 110                         path = "/" + path;
 111                     }
 112                     ResourcePoolEntry res = ResourcePoolEntry.create(path, content);
 113                     resources.add(res);
 114                 } catch (Exception ex) {
 115                     throw new RuntimeException(ex);
 116                 }
 117             }
 118         };
 119         try (java.util.stream.Stream<Path> stream = Files.walk(compiledClasses)) {
 120             stream.forEach(c);
 121         }
 122         Plugin plugin = new StringSharingPlugin();
 123         ResourcePoolManager resultMgr = new ResourcePoolManager(resources.byteOrder(), resources.getStringTable());
 124         ResourcePool result = plugin.transform(resources.resourcePool(), resultMgr.resourcePoolBuilder());
 125 
 126         if (result.isEmpty()) {
 127             throw new AssertionError("No result");
 128         }
 129 
 130         result.entries().forEach(res -> {
 131             if (res.path().endsWith(".class")) {
 132                 try {
 133                     byte[] uncompacted = StringSharingDecompressor.normalize(reversedMap::get, res.contentBytes(),
 134                         CompressedResourceHeader.getSize());
 135                     JImageValidator.readClass(uncompacted);
 136                 } catch (IOException exp) {
 137                     throw new UncheckedIOException(exp);
 138                 }
 139             }
 140         });
 141     }
 142 }