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