1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.bcel.generic;
21
22 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
23 import static org.junit.jupiter.api.Assertions.assertNotNull;
24 import static org.junit.jupiter.api.Assertions.fail;
25 import static org.junit.jupiter.api.Assumptions.assumeTrue;
26
27 import java.io.File;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.nio.file.FileSystems;
31 import java.nio.file.FileVisitResult;
32 import java.nio.file.Files;
33 import java.nio.file.Path;
34 import java.nio.file.PathMatcher;
35 import java.nio.file.Paths;
36 import java.nio.file.SimpleFileVisitor;
37 import java.nio.file.attribute.BasicFileAttributes;
38 import java.util.Enumeration;
39 import java.util.jar.JarEntry;
40 import java.util.jar.JarFile;
41 import java.util.stream.Stream;
42
43 import org.apache.bcel.classfile.ClassParser;
44 import org.apache.bcel.classfile.Code;
45 import org.apache.bcel.classfile.JavaClass;
46 import org.apache.bcel.classfile.Method;
47 import org.apache.bcel.util.ModularRuntimeImage;
48 import org.apache.commons.lang3.JavaVersion;
49 import org.apache.commons.lang3.SystemUtils;
50 import org.junit.jupiter.api.condition.DisabledOnJre;
51 import org.junit.jupiter.api.condition.JRE;
52 import org.junit.jupiter.params.ParameterizedTest;
53 import org.junit.jupiter.params.provider.MethodSource;
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79 class JdkGenericDumpTest {
80
81 private static final boolean PRINT_CLASS_NAME = Boolean.getBoolean("bcel.print.ClassNames");
82 private static final boolean PRINT_JAR_NAME = Boolean.getBoolean("bcel.print.JarNames");
83
84 private static class ClassParserFilesVisitor extends SimpleFileVisitor<Path> {
85
86 private final PathMatcher matcher;
87
88 @SuppressWarnings("resource")
89 ClassParserFilesVisitor(final String pattern) {
90 matcher = FileSystems.getDefault().getPathMatcher("glob:" + pattern);
91 }
92
93 private void find(final Path path) throws IOException {
94 final Path name = path.getFileName();
95 if (name != null && matcher.matches(name)) {
96 try (InputStream inputStream = Files.newInputStream(path)) {
97 final ClassParser classParser = new ClassParser(inputStream, name.toAbsolutePath().toString());
98 assertNotNull(classParser.parse());
99 }
100 }
101 }
102
103 @Override
104 public FileVisitResult preVisitDirectory(final Path dir, final BasicFileAttributes attrs) throws IOException {
105 find(dir);
106 return FileVisitResult.CONTINUE;
107 }
108
109 @Override
110 public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
111 find(file);
112 return FileVisitResult.CONTINUE;
113 }
114
115 @Override
116 public FileVisitResult visitFileFailed(final Path file, final IOException e) {
117 System.err.println(e);
118 return FileVisitResult.CONTINUE;
119 }
120 }
121
122 private static final char[] hexArray = "0123456789ABCDEF".toCharArray();
123
124 private static String bytesToHex(final byte[] bytes) {
125 final char[] hexChars = new char[bytes.length * 3];
126 int i = 0;
127 for (final byte b : bytes) {
128 final int v = b & 0xFF;
129 hexChars[i++] = hexArray[v >>> 4];
130 hexChars[i++] = hexArray[v & 0x0F];
131 hexChars[i++] = ' ';
132 }
133 return new String(hexChars);
134 }
135
136 static Stream<Path> testJdkModules() {
137
138
139 return Stream.concat(JavaHome.streamModulePath(), Stream.of(Paths.get("src/test/resources/jpms/empty/empty.jmod")));
140 }
141
142 private void compare(final String name, final Method method) {
143
144 final Code code = method.getCode();
145 if (code == null) {
146 return;
147 }
148 final byte[] src = code.getCode();
149 final InstructionList instructionList = new InstructionList(src);
150 final byte[] out = instructionList.getByteCode();
151 if (src.length == out.length) {
152 assertArrayEquals(src, out, () -> name + ": " + method.toString());
153 } else {
154 System.out.println(name + ": " + method.toString() + " " + src.length + " " + out.length);
155 System.out.println(bytesToHex(src));
156 System.out.println(bytesToHex(out));
157 for (final InstructionHandle instructionHandle : instructionList) {
158 System.out.println(instructionHandle.toString(false));
159 }
160 fail("Array comparison failure");
161 }
162 }
163
164 private void testJar(final Path file) throws Exception {
165 if (PRINT_JAR_NAME) {
166 System.out.println(" - " + file);
167 }
168 try (JarFile jar = new JarFile(file.toFile())) {
169 final Enumeration<JarEntry> en = jar.entries();
170 while (en.hasMoreElements()) {
171 final JarEntry jarEntry = en.nextElement();
172 final String name = jarEntry.getName();
173 if (name.endsWith(JavaClass.EXTENSION)) {
174 if (PRINT_CLASS_NAME) {
175 System.out.println(" - " + name);
176 }
177 try (InputStream inputStream = jar.getInputStream(jarEntry)) {
178 final ClassParser classParser = new ClassParser(inputStream, name);
179 final JavaClass javaClass = classParser.parse();
180 for (final Method method : javaClass.getMethods()) {
181 compare(name, method);
182 }
183 }
184 }
185 }
186 }
187 }
188
189 @ParameterizedTest
190 @MethodSource("org.apache.bcel.generic.JavaHome#streamJarPath")
191 void testJdkJars(final Path jarPath) throws Exception {
192 testJar(jarPath);
193 }
194
195 @ParameterizedTest
196 @DisabledOnJre(value = JRE.JAVA_8)
197 @MethodSource
198 void testJdkModules(final Path jmodPath) throws Exception {
199 testJar(jmodPath);
200 }
201
202 @ParameterizedTest
203 @MethodSource("org.apache.bcel.generic.JavaHome#streamJavaHome")
204 void testJreModules(final JavaHome javaHome) throws Exception {
205 assumeTrue(SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_9));
206 try (ModularRuntimeImage mri = javaHome.getModularRuntimeImage()) {
207 for (final Path path : mri.modules()) {
208 Files.walkFileTree(path, new ClassParserFilesVisitor("*.class"));
209 }
210 }
211 }
212
213 }