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 StripDebugPlugin |
|
27 |
* @author Jean-Francois Denise |
|
28 |
* @library ../../lib |
|
29 |
* @build tests.* |
|
30 |
* @modules java.base/jdk.internal.jimage |
|
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.jimage |
35 |
* jdk.jlink/jdk.tools.jmod |
|
36 |
* jdk.jdeps/com.sun.tools.classfile |
|
37 |
* jdk.compiler |
|
38 |
* @run main StripDebugPluginTest |
|
39 |
*/ |
|
40 |
||
41 |
import java.io.ByteArrayInputStream; |
|
42 |
import java.io.IOException; |
|
43 |
import java.nio.file.Files; |
|
44 |
import java.nio.file.Path; |
|
45 |
import java.util.ArrayList; |
|
46 |
import java.util.Arrays; |
|
47 |
import java.util.Iterator; |
|
48 |
import java.util.List; |
|
49 |
import java.util.stream.Stream; |
|
50 |
||
51 |
import com.sun.tools.classfile.Attribute; |
|
52 |
import com.sun.tools.classfile.ClassFile; |
|
53 |
import com.sun.tools.classfile.Code_attribute; |
|
54 |
import com.sun.tools.classfile.ConstantPoolException; |
|
55 |
import com.sun.tools.classfile.Method; |
|
56 |
import java.util.HashMap; |
|
57 |
import java.util.Map; |
|
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
|
58 |
import jdk.tools.jlink.internal.ResourcePoolManager; |
36511 | 59 |
import jdk.tools.jlink.internal.plugins.StripDebugPlugin; |
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
|
60 |
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
|
61 |
import jdk.tools.jlink.plugin.ResourcePool; |
39321
c60f34e8c057
8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents:
38320
diff
changeset
|
62 |
import jdk.tools.jlink.plugin.Plugin; |
36511 | 63 |
import tests.Helper; |
64 |
||
65 |
public class StripDebugPluginTest { |
|
66 |
public static void main(String[] args) throws Exception { |
|
67 |
new StripDebugPluginTest().test(); |
|
68 |
} |
|
69 |
||
70 |
public void test() throws Exception { |
|
71 |
// JPRT not yet ready for jmods |
|
72 |
Helper helper = Helper.newHelper(); |
|
73 |
if (helper == null) { |
|
74 |
System.err.println("Test not run, NO jmods directory"); |
|
75 |
return; |
|
76 |
} |
|
77 |
||
78 |
List<String> classes = Arrays.asList("toto.Main", "toto.com.foo.bar.X"); |
|
79 |
Path moduleFile = helper.generateModuleCompiledClasses( |
|
80 |
helper.getJmodSrcDir(), helper.getJmodClassesDir(), "leaf1", classes); |
|
81 |
Path moduleInfo = moduleFile.resolve("module-info.class"); |
|
82 |
||
83 |
// Classes have been compiled in debug. |
|
84 |
List<Path> covered = new ArrayList<>(); |
|
85 |
byte[] infoContent = Files.readAllBytes(moduleInfo); |
|
86 |
try (Stream<Path> stream = Files.walk(moduleFile)) { |
|
87 |
for (Iterator<Path> iterator = stream.iterator(); iterator.hasNext(); ) { |
|
88 |
Path p = iterator.next(); |
|
89 |
if (Files.isRegularFile(p) && p.toString().endsWith(".class")) { |
|
90 |
byte[] content = Files.readAllBytes(p); |
|
91 |
String path = "/" + helper.getJmodClassesDir().relativize(p).toString(); |
|
92 |
String moduleInfoPath = path + "/module-info.class"; |
|
93 |
check(path, content, moduleInfoPath, infoContent); |
|
94 |
covered.add(p); |
|
95 |
} |
|
96 |
} |
|
97 |
} |
|
98 |
if (covered.isEmpty()) { |
|
99 |
throw new AssertionError("No class to compress"); |
|
100 |
} else { |
|
101 |
System.err.println("removed debug attributes from " |
|
102 |
+ covered.size() + " classes"); |
|
103 |
} |
|
104 |
} |
|
105 |
||
106 |
private void check(String path, byte[] content, String infoPath, byte[] moduleInfo) throws Exception { |
|
107 |
path = path.replace('\\', '/'); |
|
108 |
StripDebugPlugin debug = new StripDebugPlugin(); |
|
109 |
debug.configure(new HashMap<>()); |
|
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
|
110 |
ResourcePoolEntry result1 = stripDebug(debug, ResourcePoolEntry.create(path,content), path, infoPath, moduleInfo); |
36511 | 111 |
|
112 |
if (!path.endsWith("module-info.class")) { |
|
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 |
if (result1.contentLength() >= content.length) { |
36511 | 114 |
throw new AssertionError("Class size not reduced, debug info not " |
115 |
+ "removed for " + path); |
|
116 |
} |
|
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
|
117 |
checkDebugAttributes(result1.contentBytes()); |
36511 | 118 |
} |
119 |
||
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
|
120 |
ResourcePoolEntry result2 = stripDebug(debug, result1, path, infoPath, moduleInfo); |
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
|
121 |
if (result1.contentLength() != result2.contentLength()) { |
36511 | 122 |
throw new AssertionError("removing debug info twice reduces class size of " |
123 |
+ path); |
|
124 |
} |
|
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
|
125 |
checkDebugAttributes(result1.contentBytes()); |
36511 | 126 |
} |
127 |
||
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
|
128 |
private ResourcePoolEntry stripDebug(Plugin debug, ResourcePoolEntry classResource, |
36511 | 129 |
String path, String infoPath, byte[] moduleInfo) throws Exception { |
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
|
130 |
ResourcePoolManager resources = new ResourcePoolManager(); |
36511 | 131 |
resources.add(classResource); |
132 |
if (!path.endsWith("module-info.class")) { |
|
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
|
133 |
ResourcePoolEntry res2 = ResourcePoolEntry.create(infoPath, moduleInfo); |
36511 | 134 |
resources.add(res2); |
135 |
} |
|
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
|
136 |
ResourcePoolManager results = new ResourcePoolManager(); |
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
|
137 |
ResourcePool resPool = debug.transform(resources.resourcePool(), |
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
|
138 |
results.resourcePoolBuilder()); |
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
|
139 |
System.out.println(classResource.path()); |
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
|
140 |
|
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
|
141 |
return resPool.findEntry(classResource.path()).get(); |
36511 | 142 |
} |
143 |
||
144 |
private void checkDebugAttributes(byte[] strippedClassFile) throws IOException, ConstantPoolException { |
|
145 |
ClassFile classFile = ClassFile.read(new ByteArrayInputStream(strippedClassFile)); |
|
146 |
String[] debugAttributes = new String[]{ |
|
147 |
Attribute.LineNumberTable, |
|
148 |
Attribute.LocalVariableTable, |
|
149 |
Attribute.LocalVariableTypeTable |
|
150 |
}; |
|
151 |
for (Method method : classFile.methods) { |
|
152 |
String methodName = method.getName(classFile.constant_pool); |
|
153 |
Code_attribute code = (Code_attribute) method.attributes.get(Attribute.Code); |
|
154 |
for (String attr : debugAttributes) { |
|
155 |
if (code.attributes.get(attr) != null) { |
|
156 |
throw new AssertionError("Debug attribute was not removed: " + attr + |
|
157 |
" from method " + classFile.getName() + "#" + methodName); |
|
158 |
} |
|
159 |
} |
|
160 |
} |
|
161 |
} |
|
162 |
} |