hotspot/test/compiler/jsr292/methodHandleExceptions/ByteClassLoader.java
author tpivovarova
Tue, 12 Jul 2016 18:24:48 +0300
changeset 40059 c2304140ed64
parent 21770 e8932d2fda2c
child 41705 332239c052cc
permissions -rw-r--r--
8132919: Put compiler tests in packages Reviewed-by: vlivanov, dpochepk Contributed-by: igor.ignatyev@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
20298
861da81238ee 8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff changeset
     1
/*
861da81238ee 8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
861da81238ee 8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
861da81238ee 8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff changeset
     4
 *
861da81238ee 8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
861da81238ee 8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
861da81238ee 8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff changeset
     7
 * published by the Free Software Foundation.
861da81238ee 8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff changeset
     8
 *
861da81238ee 8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
861da81238ee 8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
861da81238ee 8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
861da81238ee 8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
861da81238ee 8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff changeset
    13
 * accompanied this code).
861da81238ee 8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff changeset
    14
 *
861da81238ee 8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
861da81238ee 8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
861da81238ee 8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
861da81238ee 8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff changeset
    18
 *
861da81238ee 8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
861da81238ee 8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
861da81238ee 8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff changeset
    21
 * questions.
861da81238ee 8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff changeset
    22
 *
861da81238ee 8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff changeset
    23
 */
861da81238ee 8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff changeset
    24
40059
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 21770
diff changeset
    25
package compiler.jsr292.methodHandleExceptions;
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 21770
diff changeset
    26
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 21770
diff changeset
    27
import java.io.BufferedOutputStream;
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 21770
diff changeset
    28
import java.io.FileNotFoundException;
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 21770
diff changeset
    29
import java.io.FileOutputStream;
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 21770
diff changeset
    30
import java.io.IOException;
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 21770
diff changeset
    31
import java.net.URL;
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 21770
diff changeset
    32
import java.net.URLClassLoader;
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 21770
diff changeset
    33
import java.util.jar.JarEntry;
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 21770
diff changeset
    34
import java.util.jar.JarOutputStream;
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 21770
diff changeset
    35
20298
861da81238ee 8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff changeset
    36
/**
21770
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
    37
 * A ByteClassLoader is used to define classes from collections of bytes, as
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
    38
 * well as loading classes in the usual way. It includes options to write the
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
    39
 * classes to files in a jar, or to read the classes from jars in a later or
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
    40
 * debugging run.
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
    41
 *
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
    42
 * If Boolean property byteclassloader.verbose is true, be chatty about jar
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
    43
 * file operations.
20298
861da81238ee 8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff changeset
    44
 *
861da81238ee 8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff changeset
    45
 */
21770
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
    46
public class ByteClassLoader extends URLClassLoader {
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
    47
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
    48
    final static boolean verbose
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
    49
            = Boolean.getBoolean("byteclassloader.verbose");
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
    50
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
    51
    final boolean read;
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
    52
    final JarOutputStream jos;
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
    53
    final String jar_name;
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
    54
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
    55
    /**
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
    56
     * Make a new ByteClassLoader.
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
    57
     *
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
    58
     * @param jar_name  Basename of jar file to be read/written by this classloader.
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
    59
     * @param read      If true, read classes from jar file instead of from parameter.
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
    60
     * @param write     If true, write classes to jar files for offline study/use.
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
    61
     *
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
    62
     * @throws FileNotFoundException
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
    63
     * @throws IOException
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
    64
     */
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
    65
    public ByteClassLoader(String jar_name, boolean read, boolean write)
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
    66
            throws FileNotFoundException, IOException {
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
    67
        super(read
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
    68
                ? new URL[]{new URL("file:" + jar_name + ".jar")}
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
    69
                : new URL[0]);
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
    70
        this.read = read;
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
    71
        this.jar_name = jar_name;
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
    72
        this.jos = write
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
    73
                ? new JarOutputStream(
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
    74
                new BufferedOutputStream(
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
    75
                new FileOutputStream(jar_name + ".jar"))) : null;
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
    76
        if (read && write) {
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
    77
            throw new Error("At most one of read and write may be true.");
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
    78
        }
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
    79
    }
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
    80
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
    81
    private static void writeJarredFile(JarOutputStream jos, String file, String suffix, byte[] bytes) {
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
    82
        String fileName = file.replace(".", "/") + "." + suffix;
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
    83
        JarEntry ze = new JarEntry(fileName);
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
    84
        try {
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
    85
            ze.setSize(bytes.length);
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
    86
            jos.putNextEntry(ze);
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
    87
            jos.write(bytes);
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
    88
            jos.closeEntry();
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
    89
        } catch (IOException e) {
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
    90
            throw new RuntimeException(e);
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
    91
        }
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
    92
    }
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
    93
20298
861da81238ee 8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff changeset
    94
    /**
861da81238ee 8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff changeset
    95
     * (pre)load class name using classData for the definition.
861da81238ee 8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff changeset
    96
     *
861da81238ee 8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff changeset
    97
     * @param name
861da81238ee 8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff changeset
    98
     * @param classData
861da81238ee 8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff changeset
    99
     * @return
861da81238ee 8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff changeset
   100
     */
21770
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
   101
    public Class<?> loadBytes(String name, byte[] classData) throws ClassNotFoundException {
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
   102
        if (jos != null) {
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
   103
            if (verbose) {
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
   104
                System.out.println("ByteClassLoader: writing " + name);
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
   105
            }
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
   106
            writeJarredFile(jos, name, "class", classData);
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
   107
        }
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
   108
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
   109
        Class<?> clazz = null;
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
   110
        if (read) {
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
   111
            if (verbose) {
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
   112
                System.out.println("ByteClassLoader: reading " + name + " from " + jar_name);
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
   113
            }
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
   114
            clazz = loadClass(name);
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
   115
        } else {
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
   116
            clazz = defineClass(name, classData, 0, classData.length);
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
   117
            resolveClass(clazz);
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
   118
        }
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
   119
        return clazz;
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
   120
    }
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
   121
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
   122
    public void close() {
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
   123
        if (jos != null) {
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
   124
            try {
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
   125
                if (verbose) {
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
   126
                    System.out.println("ByteClassLoader: closing " + jar_name);
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
   127
                }
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
   128
                jos.close();
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
   129
            } catch (IOException ex) {
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
   130
            }
e8932d2fda2c 8016839: JSR292: AME instead of IAE when calling a method
drchase
parents: 20298
diff changeset
   131
        }
20298
861da81238ee 8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff changeset
   132
    }
861da81238ee 8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff changeset
   133
}