author | redestad |
Mon, 18 Apr 2016 16:48:12 +0200 | |
changeset 37370 | 4dcebdd2bbc0 |
parent 36508 | 5f9eee6b383b |
child 37263 | 08dec586ed5c |
permissions | -rw-r--r-- |
15793
4867678e3517
8006753: fix failed for JDK-8002415 White box testing API for HotSpot
mgerdin
parents:
diff
changeset
|
1 |
/* |
35572
c864053d0405
8147444: compiler/jsr292/NonInlinedCall/RedefineTest.java fails with NullPointerException in ClassFileInstaller
thartmann
parents:
17373
diff
changeset
|
2 |
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. |
15793
4867678e3517
8006753: fix failed for JDK-8002415 White box testing API for HotSpot
mgerdin
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4867678e3517
8006753: fix failed for JDK-8002415 White box testing API for HotSpot
mgerdin
parents:
diff
changeset
|
4 |
* |
4867678e3517
8006753: fix failed for JDK-8002415 White box testing API for HotSpot
mgerdin
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
4867678e3517
8006753: fix failed for JDK-8002415 White box testing API for HotSpot
mgerdin
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
4867678e3517
8006753: fix failed for JDK-8002415 White box testing API for HotSpot
mgerdin
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
4867678e3517
8006753: fix failed for JDK-8002415 White box testing API for HotSpot
mgerdin
parents:
diff
changeset
|
8 |
* |
4867678e3517
8006753: fix failed for JDK-8002415 White box testing API for HotSpot
mgerdin
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
4867678e3517
8006753: fix failed for JDK-8002415 White box testing API for HotSpot
mgerdin
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
4867678e3517
8006753: fix failed for JDK-8002415 White box testing API for HotSpot
mgerdin
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
4867678e3517
8006753: fix failed for JDK-8002415 White box testing API for HotSpot
mgerdin
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
4867678e3517
8006753: fix failed for JDK-8002415 White box testing API for HotSpot
mgerdin
parents:
diff
changeset
|
13 |
* accompanied this code). |
4867678e3517
8006753: fix failed for JDK-8002415 White box testing API for HotSpot
mgerdin
parents:
diff
changeset
|
14 |
* |
4867678e3517
8006753: fix failed for JDK-8002415 White box testing API for HotSpot
mgerdin
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
4867678e3517
8006753: fix failed for JDK-8002415 White box testing API for HotSpot
mgerdin
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
4867678e3517
8006753: fix failed for JDK-8002415 White box testing API for HotSpot
mgerdin
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
4867678e3517
8006753: fix failed for JDK-8002415 White box testing API for HotSpot
mgerdin
parents:
diff
changeset
|
18 |
* |
4867678e3517
8006753: fix failed for JDK-8002415 White box testing API for HotSpot
mgerdin
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
4867678e3517
8006753: fix failed for JDK-8002415 White box testing API for HotSpot
mgerdin
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
4867678e3517
8006753: fix failed for JDK-8002415 White box testing API for HotSpot
mgerdin
parents:
diff
changeset
|
21 |
* questions. |
4867678e3517
8006753: fix failed for JDK-8002415 White box testing API for HotSpot
mgerdin
parents:
diff
changeset
|
22 |
*/ |
4867678e3517
8006753: fix failed for JDK-8002415 White box testing API for HotSpot
mgerdin
parents:
diff
changeset
|
23 |
|
35572
c864053d0405
8147444: compiler/jsr292/NonInlinedCall/RedefineTest.java fails with NullPointerException in ClassFileInstaller
thartmann
parents:
17373
diff
changeset
|
24 |
import java.io.FileNotFoundException; |
15793
4867678e3517
8006753: fix failed for JDK-8002415 White box testing API for HotSpot
mgerdin
parents:
diff
changeset
|
25 |
import java.io.InputStream; |
36508 | 26 |
import java.io.ByteArrayInputStream; |
15793
4867678e3517
8006753: fix failed for JDK-8002415 White box testing API for HotSpot
mgerdin
parents:
diff
changeset
|
27 |
import java.nio.file.Files; |
4867678e3517
8006753: fix failed for JDK-8002415 White box testing API for HotSpot
mgerdin
parents:
diff
changeset
|
28 |
import java.nio.file.Path; |
4867678e3517
8006753: fix failed for JDK-8002415 White box testing API for HotSpot
mgerdin
parents:
diff
changeset
|
29 |
import java.nio.file.Paths; |
4867678e3517
8006753: fix failed for JDK-8002415 White box testing API for HotSpot
mgerdin
parents:
diff
changeset
|
30 |
import java.nio.file.StandardCopyOption; |
4867678e3517
8006753: fix failed for JDK-8002415 White box testing API for HotSpot
mgerdin
parents:
diff
changeset
|
31 |
|
4867678e3517
8006753: fix failed for JDK-8002415 White box testing API for HotSpot
mgerdin
parents:
diff
changeset
|
32 |
/** |
4867678e3517
8006753: fix failed for JDK-8002415 White box testing API for HotSpot
mgerdin
parents:
diff
changeset
|
33 |
* Dump a class file for a class on the class path in the current directory |
4867678e3517
8006753: fix failed for JDK-8002415 White box testing API for HotSpot
mgerdin
parents:
diff
changeset
|
34 |
*/ |
4867678e3517
8006753: fix failed for JDK-8002415 White box testing API for HotSpot
mgerdin
parents:
diff
changeset
|
35 |
public class ClassFileInstaller { |
4867678e3517
8006753: fix failed for JDK-8002415 White box testing API for HotSpot
mgerdin
parents:
diff
changeset
|
36 |
/** |
4867678e3517
8006753: fix failed for JDK-8002415 White box testing API for HotSpot
mgerdin
parents:
diff
changeset
|
37 |
* @param args The names of the classes to dump |
4867678e3517
8006753: fix failed for JDK-8002415 White box testing API for HotSpot
mgerdin
parents:
diff
changeset
|
38 |
* @throws Exception |
4867678e3517
8006753: fix failed for JDK-8002415 White box testing API for HotSpot
mgerdin
parents:
diff
changeset
|
39 |
*/ |
4867678e3517
8006753: fix failed for JDK-8002415 White box testing API for HotSpot
mgerdin
parents:
diff
changeset
|
40 |
public static void main(String... args) throws Exception { |
4867678e3517
8006753: fix failed for JDK-8002415 White box testing API for HotSpot
mgerdin
parents:
diff
changeset
|
41 |
for (String arg : args) { |
36508 | 42 |
writeClassToDisk(arg); |
15793
4867678e3517
8006753: fix failed for JDK-8002415 White box testing API for HotSpot
mgerdin
parents:
diff
changeset
|
43 |
} |
4867678e3517
8006753: fix failed for JDK-8002415 White box testing API for HotSpot
mgerdin
parents:
diff
changeset
|
44 |
} |
36508 | 45 |
|
46 |
public static void writeClassToDisk(String className) throws Exception { |
|
47 |
writeClassToDisk(className, ""); |
|
48 |
} |
|
49 |
||
50 |
public static void writeClassToDisk(String className, String prependPath) throws Exception { |
|
51 |
ClassLoader cl = ClassFileInstaller.class.getClassLoader(); |
|
52 |
||
53 |
// Convert dotted class name to a path to a class file |
|
54 |
String pathName = className.replace('.', '/').concat(".class"); |
|
55 |
InputStream is = cl.getResourceAsStream(pathName); |
|
56 |
if (prependPath.length() > 0) { |
|
57 |
pathName = prependPath + "/" + pathName; |
|
58 |
} |
|
59 |
writeToDisk(pathName, is); |
|
60 |
} |
|
61 |
||
62 |
public static void writeClassToDisk(String className, byte[] bytecode) throws Exception { |
|
63 |
writeClassToDisk(className, bytecode, ""); |
|
64 |
} |
|
65 |
||
66 |
public static void writeClassToDisk(String className, byte[] bytecode, String prependPath) throws Exception { |
|
67 |
// Convert dotted class name to a path to a class file |
|
68 |
String pathName = className.replace('.', '/').concat(".class"); |
|
69 |
if (prependPath.length() > 0) { |
|
70 |
pathName = prependPath + "/" + pathName; |
|
71 |
} |
|
72 |
writeToDisk(pathName, new ByteArrayInputStream(bytecode)); |
|
73 |
} |
|
74 |
||
75 |
||
76 |
private static void writeToDisk(String pathName, InputStream is) throws Exception { |
|
77 |
// Create the class file's package directory |
|
78 |
Path p = Paths.get(pathName); |
|
79 |
if (pathName.contains("/")) { |
|
80 |
Files.createDirectories(p.getParent()); |
|
81 |
} |
|
82 |
// Create the class file |
|
83 |
Files.copy(is, p, StandardCopyOption.REPLACE_EXISTING); |
|
84 |
} |
|
15793
4867678e3517
8006753: fix failed for JDK-8002415 White box testing API for HotSpot
mgerdin
parents:
diff
changeset
|
85 |
} |