hotspot/test/testlibrary/ClassFileInstaller.java
author redestad
Mon, 18 Apr 2016 16:48:12 +0200
changeset 37370 4dcebdd2bbc0
parent 36508 5f9eee6b383b
child 37263 08dec586ed5c
permissions -rw-r--r--
8154454: Fix compilation issue in PlainSocketImpl Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35572
diff changeset
    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
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35572
diff changeset
    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
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35572
diff changeset
    45
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35572
diff changeset
    46
    public static void writeClassToDisk(String className) throws Exception {
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35572
diff changeset
    47
        writeClassToDisk(className, "");
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35572
diff changeset
    48
    }
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35572
diff changeset
    49
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35572
diff changeset
    50
    public static void writeClassToDisk(String className, String prependPath) throws Exception {
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35572
diff changeset
    51
        ClassLoader cl = ClassFileInstaller.class.getClassLoader();
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35572
diff changeset
    52
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35572
diff changeset
    53
        // Convert dotted class name to a path to a class file
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35572
diff changeset
    54
        String pathName = className.replace('.', '/').concat(".class");
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35572
diff changeset
    55
        InputStream is = cl.getResourceAsStream(pathName);
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35572
diff changeset
    56
        if (prependPath.length() > 0) {
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35572
diff changeset
    57
            pathName = prependPath + "/" + pathName;
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35572
diff changeset
    58
        }
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35572
diff changeset
    59
        writeToDisk(pathName, is);
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35572
diff changeset
    60
    }
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35572
diff changeset
    61
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35572
diff changeset
    62
    public static void writeClassToDisk(String className, byte[] bytecode) throws Exception {
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35572
diff changeset
    63
        writeClassToDisk(className, bytecode, "");
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35572
diff changeset
    64
    }
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35572
diff changeset
    65
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35572
diff changeset
    66
    public static void writeClassToDisk(String className, byte[] bytecode, String prependPath) throws Exception {
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35572
diff changeset
    67
        // Convert dotted class name to a path to a class file
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35572
diff changeset
    68
        String pathName = className.replace('.', '/').concat(".class");
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35572
diff changeset
    69
        if (prependPath.length() > 0) {
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35572
diff changeset
    70
            pathName = prependPath + "/" + pathName;
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35572
diff changeset
    71
        }
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35572
diff changeset
    72
        writeToDisk(pathName, new ByteArrayInputStream(bytecode));
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35572
diff changeset
    73
    }
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35572
diff changeset
    74
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35572
diff changeset
    75
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35572
diff changeset
    76
    private static void writeToDisk(String pathName, InputStream is) throws Exception {
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35572
diff changeset
    77
        // Create the class file's package directory
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35572
diff changeset
    78
        Path p = Paths.get(pathName);
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35572
diff changeset
    79
        if (pathName.contains("/")) {
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35572
diff changeset
    80
            Files.createDirectories(p.getParent());
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35572
diff changeset
    81
        }
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35572
diff changeset
    82
        // Create the class file
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35572
diff changeset
    83
        Files.copy(is, p, StandardCopyOption.REPLACE_EXISTING);
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35572
diff changeset
    84
    }
15793
4867678e3517 8006753: fix failed for JDK-8002415 White box testing API for HotSpot
mgerdin
parents:
diff changeset
    85
}