test/lib/ClassFileInstaller.java
author ctornqvi
Fri, 19 Aug 2016 10:09:25 -0400
changeset 40614 b5d80754b40e
permissions -rw-r--r--
8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder Reviewed-by: coleenp, gtriantafill, mseledtsov, iignatyev, dholmes, dsamersoff
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
40614
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
     1
/*
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
     4
 *
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
     7
 * published by the Free Software Foundation.
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
     8
 *
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    13
 * accompanied this code).
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    14
 *
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    18
 *
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    21
 * questions.
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    22
 */
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    23
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    24
import java.io.ByteArrayInputStream;
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    25
import java.io.File;
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    26
import java.io.FileInputStream;
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    27
import java.io.FileOutputStream;
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    28
import java.io.FileNotFoundException;
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    29
import java.io.InputStream;
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    30
import java.io.ByteArrayInputStream;
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    31
import java.nio.file.Files;
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    32
import java.nio.file.Path;
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    33
import java.nio.file.Paths;
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    34
import java.nio.file.StandardCopyOption;
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    35
import java.util.zip.ZipEntry;
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    36
import java.util.zip.ZipOutputStream;
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    37
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    38
/**
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    39
 * Dump a class file for a class on the class path in the current directory, or
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    40
 * in the specified JAR file. This class is usually used when you build a class
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    41
 * from a test library, but want to use this class in a sub-process.
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    42
 *
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    43
 * For example, to build the following library class:
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    44
 * test/lib/sun/hotspot/WhiteBox.java
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    45
 *
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    46
 * You would use the following tags:
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    47
 *
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    48
 * @library /test/lib
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    49
 * @build sun.hotspot.WhiteBox
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    50
 *
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    51
 * JTREG would build the class file under
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    52
 * ${JTWork}/classes/test/lib/sun/hotspot/WhiteBox.class
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    53
 *
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    54
 * With you run your main test class using "@run main MyMainClass", JTREG would setup the
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    55
 * -classpath to include "${JTWork}/classes/test/lib/", so MyMainClass would be able to
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    56
 * load the WhiteBox class.
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    57
 *
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    58
 * However, if you run a sub process, and do not wish to use the exact same -classpath,
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    59
 * You can use ClassFileInstaller to ensure that WhiteBox is available in the current
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    60
 * directory of your test:
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    61
 *
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    62
 * @run main ClassFileInstaller sun.hotspot.WhiteBox
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    63
 *
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    64
 * Or, you can use the -jar option to store the class in the specified JAR file. If a relative
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    65
 * path name is given, the JAR file would be relative to the current directory of
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    66
 *
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    67
 * @run main ClassFileInstaller -jar myjar.jar sun.hotspot.WhiteBox
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    68
 */
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    69
public class ClassFileInstaller {
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    70
    /**
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    71
     * You can enable debug tracing of ClassFileInstaller by running JTREG with
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    72
     * jtreg -DClassFileInstaller.debug=true ... <names of tests>
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    73
     */
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    74
    public static boolean DEBUG = Boolean.getBoolean("ClassFileInstaller.debug");
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    75
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    76
    /**
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    77
     * @param args The names of the classes to dump
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    78
     * @throws Exception
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    79
     */
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    80
    public static void main(String... args) throws Exception {
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    81
        if (args.length > 1 && args[0].equals("-jar")) {
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    82
            if (args.length < 2) {
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    83
                throw new RuntimeException("Usage: ClassFileInstaller <options> <classes>\n" +
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    84
                                           "where possible options include:\n" +
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    85
                                           "  -jar <path>             Write to the JAR file <path>");
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    86
            }
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    87
            writeJar(args[1], null, args, 2, args.length);
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    88
        } else {
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    89
            if (DEBUG) {
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    90
                System.out.println("ClassFileInstaller: Writing to " + System.getProperty("user.dir"));
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    91
            }
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    92
            for (String arg : args) {
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    93
                writeClassToDisk(arg);
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    94
            }
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    95
        }
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    96
    }
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    97
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    98
    public static class Manifest {
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
    99
        private InputStream in;
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   100
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   101
        private Manifest(InputStream in) {
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   102
            this.in = in;
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   103
        }
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   104
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   105
        static Manifest fromSourceFile(String fileName) throws Exception {
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   106
            String pathName = System.getProperty("test.src") + File.separator + fileName;
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   107
            return new Manifest(new FileInputStream(pathName));
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   108
        }
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   109
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   110
        // Example:
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   111
        //  String manifest = "Premain-Class: RedefineClassHelper\n" +
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   112
        //                "Can-Redefine-Classes: true\n";
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   113
        //  ClassFileInstaller.writeJar("redefineagent.jar",
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   114
        //    ClassFileInstaller.Manifest.fromString(manifest),
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   115
        //    "RedefineClassHelper");
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   116
        static Manifest fromString(String manifest) throws Exception {
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   117
            return new Manifest(new ByteArrayInputStream(manifest.getBytes()));
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   118
        }
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   119
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   120
        public InputStream getInputStream() {
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   121
            return in;
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   122
        }
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   123
    }
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   124
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   125
    private static void writeJar(String jarFile, Manifest manifest, String classes[], int from, int to) throws Exception {
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   126
        if (DEBUG) {
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   127
            System.out.println("ClassFileInstaller: Writing to " + getJarPath(jarFile));
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   128
        }
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   129
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   130
        (new File(jarFile)).delete();
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   131
        FileOutputStream fos = new FileOutputStream(jarFile);
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   132
        ZipOutputStream zos = new ZipOutputStream(fos);
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   133
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   134
        // The manifest must be the first or second entry. See comments in JarInputStream
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   135
        // constructor and JDK-5046178.
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   136
        if (manifest != null) {
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   137
            writeToDisk(zos, "META-INF/MANIFEST.MF", manifest.getInputStream());
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   138
        }
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   139
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   140
        for (int i=from; i<to; i++) {
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   141
            writeClassToDisk(zos, classes[i]);
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   142
        }
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   143
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   144
        zos.close();
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   145
        fos.close();
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   146
    }
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   147
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   148
    /*
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   149
     * You can call ClassFileInstaller.writeJar() from your main test class instead of
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   150
     * using "@run ClassFileInstaller -jar ...". E.g.,
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   151
     *
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   152
     * String jarPath = ClassFileInstaller.getJarPath("myjar.jar", "sun.hotspot.WhiteBox")
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   153
     *
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   154
     * If you call this API, make sure you build ClassFileInstaller with the following tags:
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   155
     *
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   156
     * @library testlibrary
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   157
     * @build ClassFileInstaller
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   158
     */
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   159
    public static String writeJar(String jarFile, String... classes) throws Exception {
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   160
        writeJar(jarFile, null, classes, 0, classes.length);
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   161
        return getJarPath(jarFile);
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   162
    }
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   163
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   164
    public static String writeJar(String jarFile, Manifest manifest, String... classes) throws Exception {
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   165
        writeJar(jarFile, manifest, classes, 0, classes.length);
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   166
        return getJarPath(jarFile);
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   167
    }
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   168
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   169
    /**
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   170
     * This returns the absolute path to the file specified in "@ClassFileInstaller -jar myjar.jar",
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   171
     * In your test program, instead of using the JAR file name directly:
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   172
     *
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   173
     * String jarPath = "myjar.jar";
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   174
     *
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   175
     * you should call this function, like:
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   176
     *
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   177
     * String jarPath = ClassFileInstaller.getJarPath("myjar.jar")
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   178
     *
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   179
     * The reasons are:
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   180
     * (1) Using absolute path makes it easy to cut-and-paste from the JTR file and rerun your
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   181
     *     test in any directory.
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   182
     * (2) In the future, we may make the JAR file name unique to avoid clobbering
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   183
     *     during parallel JTREG execution.
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   184
     *
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   185
     */
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   186
    public static String getJarPath(String jarFileName) {
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   187
        return new File(jarFileName).getAbsolutePath();
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   188
    }
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   189
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   190
    public static void writeClassToDisk(String className) throws Exception {
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   191
        writeClassToDisk((ZipOutputStream)null, className);
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   192
    }
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   193
    private static void writeClassToDisk(ZipOutputStream zos, String className) throws Exception {
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   194
        writeClassToDisk(zos, className, "");
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   195
    }
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   196
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   197
    public static void writeClassToDisk(String className, String prependPath) throws Exception {
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   198
        writeClassToDisk(null, className, prependPath);
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   199
    }
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   200
    private static void writeClassToDisk(ZipOutputStream zos, String className, String prependPath) throws Exception {
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   201
        ClassLoader cl = ClassFileInstaller.class.getClassLoader();
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   202
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   203
        // Convert dotted class name to a path to a class file
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   204
        String pathName = className.replace('.', '/').concat(".class");
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   205
        InputStream is = cl.getResourceAsStream(pathName);
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   206
        if (is == null) {
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   207
            throw new RuntimeException("Failed to find " + pathName);
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   208
        }
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   209
        if (prependPath.length() > 0) {
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   210
            pathName = prependPath + "/" + pathName;
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   211
        }
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   212
        writeToDisk(zos, pathName, is);
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   213
    }
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   214
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   215
    public static void writeClassToDisk(String className, byte[] bytecode) throws Exception {
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   216
        writeClassToDisk(null, className, bytecode);
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   217
    }
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   218
    private static void writeClassToDisk(ZipOutputStream zos, String className, byte[] bytecode) throws Exception {
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   219
        writeClassToDisk(zos, className, bytecode, "");
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   220
    }
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   221
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   222
    public static void writeClassToDisk(String className, byte[] bytecode, String prependPath) throws Exception {
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   223
        writeClassToDisk(null, className, bytecode, prependPath);
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   224
    }
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   225
    private static void writeClassToDisk(ZipOutputStream zos, String className, byte[] bytecode, String prependPath) throws Exception {
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   226
        // Convert dotted class name to a path to a class file
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   227
        String pathName = className.replace('.', '/').concat(".class");
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   228
        if (prependPath.length() > 0) {
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   229
            pathName = prependPath + "/" + pathName;
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   230
        }
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   231
        writeToDisk(zos, pathName, new ByteArrayInputStream(bytecode));
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   232
    }
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   233
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   234
    private static void writeToDisk(ZipOutputStream zos, String pathName, InputStream is) throws Exception {
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   235
        if (DEBUG) {
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   236
            System.out.println("ClassFileInstaller: Writing " + pathName);
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   237
        }
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   238
        if (zos != null) {
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   239
            ZipEntry ze = new ZipEntry(pathName);
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   240
            zos.putNextEntry(ze);
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   241
            byte[] buf = new byte[1024];
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   242
            int len;
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   243
            while ((len = is.read(buf))>0){
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   244
                zos.write(buf, 0, len);
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   245
            }
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   246
        } else {
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   247
            // Create the class file's package directory
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   248
            Path p = Paths.get(pathName);
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   249
            if (pathName.contains("/")) {
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   250
                Files.createDirectories(p.getParent());
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   251
            }
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   252
            // Create the class file
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   253
            Files.copy(is, p, StandardCopyOption.REPLACE_EXISTING);
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   254
        }
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   255
        is.close();
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   256
    }
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
diff changeset
   257
}