author | alanb |
Fri, 07 Apr 2017 08:05:54 +0000 | |
changeset 44545 | 83b611b88ac8 |
parent 43185 | d75d9ff8d4e7 |
permissions | -rw-r--r-- |
36511 | 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 |
|
43185 | 33 |
* jdk.jlink/jdk.tools.jlink.plugin |
36511 | 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 |
||
38320 | 42 |
import java.io.IOException; |
43 |
import java.io.UncheckedIOException; |
|
36511 | 44 |
import java.nio.ByteBuffer; |
45 |
import java.nio.ByteOrder; |
|
46 |
import java.nio.file.Files; |
|
47 |
import java.nio.file.Path; |
|
48 |
import java.util.Arrays; |
|
49 |
import java.util.HashMap; |
|
50 |
import java.util.List; |
|
51 |
import java.util.Map; |
|
52 |
import java.util.function.Consumer; |
|
53 |
||
54 |
import jdk.internal.jimage.decompressor.CompressedResourceHeader; |
|
55 |
import jdk.internal.jimage.decompressor.StringSharingDecompressor; |
|
39834
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39321
diff
changeset
|
56 |
import jdk.tools.jlink.internal.ResourcePoolManager; |
36511 | 57 |
import jdk.tools.jlink.internal.StringTable; |
58 |
import jdk.tools.jlink.internal.plugins.StringSharingPlugin; |
|
39834
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39321
diff
changeset
|
59 |
import jdk.tools.jlink.plugin.ResourcePoolEntry; |
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39321
diff
changeset
|
60 |
import jdk.tools.jlink.plugin.ResourcePool; |
39321
c60f34e8c057
8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents:
39151
diff
changeset
|
61 |
import jdk.tools.jlink.plugin.Plugin; |
36511 | 62 |
import tests.Helper; |
63 |
import tests.JImageValidator; |
|
64 |
||
65 |
public class StringSharingPluginTest { |
|
66 |
||
67 |
private static int strID = 1; |
|
68 |
||
69 |
public static void main(String[] args) throws Exception { |
|
70 |
// JPRT not yet ready for jmods |
|
71 |
Helper helper = Helper.newHelper(); |
|
72 |
if (helper == null) { |
|
73 |
System.err.println("Test not run, NO jmods directory"); |
|
74 |
return; |
|
75 |
} |
|
76 |
||
77 |
List<String> classes = Arrays.asList("toto.Main", "toto.com.foo.bar.X"); |
|
78 |
Path compiledClasses = helper.generateModuleCompiledClasses( |
|
79 |
helper.getJmodSrcDir(), helper.getJmodClassesDir(), "composite2", classes); |
|
80 |
||
81 |
Map<String, Integer> map = new HashMap<>(); |
|
82 |
Map<Integer, String> reversedMap = new HashMap<>(); |
|
83 |
||
39834
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39321
diff
changeset
|
84 |
ResourcePoolManager resources = new ResourcePoolManager(ByteOrder.nativeOrder(), new StringTable() { |
36511 | 85 |
@Override |
86 |
public int addString(String str) { |
|
87 |
Integer id = map.get(str); |
|
88 |
if (id == null) { |
|
89 |
id = strID; |
|
90 |
map.put(str, id); |
|
91 |
reversedMap.put(id, str); |
|
92 |
strID += 1; |
|
93 |
} |
|
94 |
return id; |
|
95 |
} |
|
96 |
||
97 |
@Override |
|
98 |
public String getString(int id) { |
|
99 |
throw new UnsupportedOperationException("Not supported yet."); |
|
100 |
} |
|
101 |
}); |
|
102 |
Consumer<Path> c = (p) -> { |
|
103 |
// take only the .class resources. |
|
104 |
if (Files.isRegularFile(p) && p.toString().endsWith(".class") |
|
105 |
&& !p.toString().endsWith("module-info.class")) { |
|
106 |
try { |
|
107 |
byte[] content = Files.readAllBytes(p); |
|
108 |
String path = p.toString().replace('\\', '/'); |
|
109 |
path = path.substring("/modules".length()); |
|
39151
34455cc82f5e
8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
38320
diff
changeset
|
110 |
if (path.charAt(0) != '/') { |
34455cc82f5e
8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
38320
diff
changeset
|
111 |
path = "/" + path; |
34455cc82f5e
8147794: Jlink's ModuleEntry.stream can't be consumed more than once and ModuleEntry content should be read only if needed
sundar
parents:
38320
diff
changeset
|
112 |
} |
39834
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39321
diff
changeset
|
113 |
ResourcePoolEntry res = ResourcePoolEntry.create(path, content); |
36511 | 114 |
resources.add(res); |
115 |
} catch (Exception ex) { |
|
116 |
throw new RuntimeException(ex); |
|
117 |
} |
|
118 |
} |
|
119 |
}; |
|
120 |
try (java.util.stream.Stream<Path> stream = Files.walk(compiledClasses)) { |
|
121 |
stream.forEach(c); |
|
122 |
} |
|
39321
c60f34e8c057
8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents:
39151
diff
changeset
|
123 |
Plugin plugin = new StringSharingPlugin(); |
39834
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39321
diff
changeset
|
124 |
ResourcePoolManager resultMgr = new ResourcePoolManager(resources.byteOrder(), resources.getStringTable()); |
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39321
diff
changeset
|
125 |
ResourcePool result = plugin.transform(resources.resourcePool(), resultMgr.resourcePoolBuilder()); |
36511 | 126 |
|
127 |
if (result.isEmpty()) { |
|
128 |
throw new AssertionError("No result"); |
|
129 |
} |
|
130 |
||
38320 | 131 |
result.entries().forEach(res -> { |
39834
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39321
diff
changeset
|
132 |
if (res.path().endsWith(".class")) { |
38320 | 133 |
try { |
39834
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39321
diff
changeset
|
134 |
byte[] uncompacted = StringSharingDecompressor.normalize(reversedMap::get, res.contentBytes(), |
36511 | 135 |
CompressedResourceHeader.getSize()); |
38320 | 136 |
JImageValidator.readClass(uncompacted); |
137 |
} catch (IOException exp) { |
|
138 |
throw new UncheckedIOException(exp); |
|
139 |
} |
|
36511 | 140 |
} |
38320 | 141 |
}); |
36511 | 142 |
} |
143 |
} |