36 * jdk.compiler |
36 * jdk.compiler |
37 * @run build tests.* |
37 * @run build tests.* |
38 * @run main StringSharingPluginTest |
38 * @run main StringSharingPluginTest |
39 */ |
39 */ |
40 |
40 |
|
41 import java.io.IOException; |
|
42 import java.io.UncheckedIOException; |
41 import java.nio.ByteBuffer; |
43 import java.nio.ByteBuffer; |
42 import java.nio.ByteOrder; |
44 import java.nio.ByteOrder; |
43 import java.nio.file.Files; |
45 import java.nio.file.Files; |
44 import java.nio.file.Path; |
46 import java.nio.file.Path; |
45 import java.util.Arrays; |
47 import java.util.Arrays; |
48 import java.util.Map; |
50 import java.util.Map; |
49 import java.util.function.Consumer; |
51 import java.util.function.Consumer; |
50 |
52 |
51 import jdk.internal.jimage.decompressor.CompressedResourceHeader; |
53 import jdk.internal.jimage.decompressor.CompressedResourceHeader; |
52 import jdk.internal.jimage.decompressor.StringSharingDecompressor; |
54 import jdk.internal.jimage.decompressor.StringSharingDecompressor; |
53 import jdk.tools.jlink.internal.PoolImpl; |
55 import jdk.tools.jlink.internal.ModulePoolImpl; |
54 import jdk.tools.jlink.internal.StringTable; |
56 import jdk.tools.jlink.internal.StringTable; |
55 import jdk.tools.jlink.internal.plugins.StringSharingPlugin; |
57 import jdk.tools.jlink.internal.plugins.StringSharingPlugin; |
56 import jdk.tools.jlink.plugin.Pool; |
58 import jdk.tools.jlink.plugin.ModuleEntry; |
57 import jdk.tools.jlink.plugin.Pool.ModuleData; |
59 import jdk.tools.jlink.plugin.ModulePool; |
58 import jdk.tools.jlink.plugin.TransformerPlugin; |
60 import jdk.tools.jlink.plugin.TransformerPlugin; |
59 import tests.Helper; |
61 import tests.Helper; |
60 import tests.JImageValidator; |
62 import tests.JImageValidator; |
61 |
63 |
62 public class StringSharingPluginTest { |
64 public class StringSharingPluginTest { |
76 helper.getJmodSrcDir(), helper.getJmodClassesDir(), "composite2", classes); |
78 helper.getJmodSrcDir(), helper.getJmodClassesDir(), "composite2", classes); |
77 |
79 |
78 Map<String, Integer> map = new HashMap<>(); |
80 Map<String, Integer> map = new HashMap<>(); |
79 Map<Integer, String> reversedMap = new HashMap<>(); |
81 Map<Integer, String> reversedMap = new HashMap<>(); |
80 |
82 |
81 PoolImpl resources = new PoolImpl(ByteOrder.nativeOrder(), new StringTable() { |
83 ModulePoolImpl resources = new ModulePoolImpl(ByteOrder.nativeOrder(), new StringTable() { |
82 @Override |
84 @Override |
83 public int addString(String str) { |
85 public int addString(String str) { |
84 Integer id = map.get(str); |
86 Integer id = map.get(str); |
85 if (id == null) { |
87 if (id == null) { |
86 id = strID; |
88 id = strID; |
102 && !p.toString().endsWith("module-info.class")) { |
104 && !p.toString().endsWith("module-info.class")) { |
103 try { |
105 try { |
104 byte[] content = Files.readAllBytes(p); |
106 byte[] content = Files.readAllBytes(p); |
105 String path = p.toString().replace('\\', '/'); |
107 String path = p.toString().replace('\\', '/'); |
106 path = path.substring("/modules".length()); |
108 path = path.substring("/modules".length()); |
107 ModuleData res = Pool.newResource(path, content); |
109 ModuleEntry res = ModuleEntry.create(path, content); |
108 resources.add(res); |
110 resources.add(res); |
109 } catch (Exception ex) { |
111 } catch (Exception ex) { |
110 throw new RuntimeException(ex); |
112 throw new RuntimeException(ex); |
111 } |
113 } |
112 } |
114 } |
113 }; |
115 }; |
114 try (java.util.stream.Stream<Path> stream = Files.walk(compiledClasses)) { |
116 try (java.util.stream.Stream<Path> stream = Files.walk(compiledClasses)) { |
115 stream.forEach(c); |
117 stream.forEach(c); |
116 } |
118 } |
117 TransformerPlugin plugin = new StringSharingPlugin(); |
119 TransformerPlugin plugin = new StringSharingPlugin(); |
118 PoolImpl result = new PoolImpl(resources.getByteOrder(), resources.getStringTable()); |
120 ModulePoolImpl result = new ModulePoolImpl(resources.getByteOrder(), resources.getStringTable()); |
119 plugin.visit(resources, result); |
121 plugin.visit(resources, result); |
120 |
122 |
121 if (result.isEmpty()) { |
123 if (result.isEmpty()) { |
122 throw new AssertionError("No result"); |
124 throw new AssertionError("No result"); |
123 } |
125 } |
124 |
126 |
125 for (ModuleData res : result.getContent()) { |
127 result.entries().forEach(res -> { |
126 if (res.getPath().endsWith(".class")) { |
128 if (res.getPath().endsWith(".class")) { |
127 byte[] uncompacted = StringSharingDecompressor.normalize(reversedMap::get, res.getBytes(), |
129 try { |
|
130 byte[] uncompacted = StringSharingDecompressor.normalize(reversedMap::get, res.getBytes(), |
128 CompressedResourceHeader.getSize()); |
131 CompressedResourceHeader.getSize()); |
129 JImageValidator.readClass(uncompacted); |
132 JImageValidator.readClass(uncompacted); |
|
133 } catch (IOException exp) { |
|
134 throw new UncheckedIOException(exp); |
|
135 } |
130 } |
136 } |
131 } |
137 }); |
132 } |
138 } |
133 } |
139 } |