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 32 * jdk.jlink/jdk.tools.jlink.internal 33 * jdk.jlink/jdk.tools.jlink.internal.plugins 34 * jdk.jlink/jdk.tools.jmod 35 * jdk.jlink/jdk.tools.jimage 36 * jdk.jdeps/com.sun.tools.classfile 37 * jdk.compiler 38 * @run build tests.* 39 * @run main StringSharingPluginTest 40 */ 41 42 import java.nio.ByteBuffer; 43 import java.nio.ByteOrder; 44 import java.nio.file.Files; 45 import java.nio.file.Path; 46 import java.util.Arrays; 47 import java.util.HashMap; 48 import java.util.List; 49 import java.util.Map; 50 import java.util.function.Consumer; 51 52 import jdk.internal.jimage.decompressor.CompressedResourceHeader; 53 import jdk.internal.jimage.decompressor.StringSharingDecompressor; 54 import jdk.tools.jlink.internal.plugins.StringSharingProvider; 55 import jdk.tools.jlink.internal.ResourcePoolImpl; 56 import jdk.tools.jlink.plugins.ResourcePool; 57 import jdk.tools.jlink.plugins.ResourcePool.Resource; 58 import jdk.tools.jlink.plugins.ResourcePlugin; 59 import jdk.tools.jlink.plugins.StringTable; 60 import tests.Helper; 61 import tests.JImageValidator; 62 63 public class StringSharingPluginTest { 64 65 private static int strID = 1; 66 67 public static void main(String[] args) throws Exception { 68 // JPRT not yet ready for jmods 69 Helper helper = Helper.newHelper(); 70 if (helper == null) { 71 System.err.println("Test not run, NO jmods directory"); 72 return; 73 } 74 75 List<String> classes = Arrays.asList("toto.Main", "toto.com.foo.bar.X"); 76 Path compiledClasses = helper.generateModuleCompiledClasses( 77 helper.getJmodSrcDir(), helper.getJmodClassesDir(), "composite2", classes); 78 ResourcePool resources = new ResourcePoolImpl(ByteOrder.nativeOrder()); 79 Consumer<Path> c = (p) -> { 80 // take only the .class resources. 81 if (Files.isRegularFile(p) && p.toString().endsWith(".class") 82 && !p.toString().endsWith("module-info.class")) { 83 try { 84 byte[] content = Files.readAllBytes(p); 85 String path = p.toString(); 86 path = path.substring("/modules".length()); 87 Resource res = new Resource(path, ByteBuffer.wrap(content)); 88 resources.addResource(res); 89 } catch (Exception ex) { 90 throw new RuntimeException(ex); 91 } 92 } 93 }; 94 try (java.util.stream.Stream<Path> stream = Files.walk(compiledClasses)) { 95 stream.forEach(c); 96 } 97 ResourcePlugin plugin = new StringSharingProvider().newPlugins(null, null)[0]; 98 Map<String, Integer> map = new HashMap<>(); 99 Map<Integer, String> reversedMap = new HashMap<>(); 100 ResourcePoolImpl result = new ResourcePoolImpl(resources.getByteOrder()); 101 plugin.visit(resources, result, new StringTable() { 102 @Override 103 public int addString(String str) { 104 Integer id = map.get(str); 105 if (id == null) { 106 id = strID; 107 map.put(str, id); 108 reversedMap.put(id, str); 109 strID += 1; 110 } 111 return id; 112 } 113 114 @Override 115 public String getString(int id) { 116 throw new UnsupportedOperationException("Not supported yet."); 117 } 118 }); 119 120 if (result.isEmpty()) { 121 throw new AssertionError("No result"); 122 } 123 124 for (Resource res : result.getResources()) { 125 if (res.getPath().endsWith(".class")) { 126 byte[] uncompacted = StringSharingDecompressor.normalize(reversedMap::get, res.getByteArray(), 127 CompressedResourceHeader.getSize()); 128 JImageValidator.readClass(uncompacted); 129 } 130 } 131 } 132 }