Path: blob/master/test/hotspot/jtreg/compiler/jsr292/methodHandleExceptions/ByteClassLoader.java
41154 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223package compiler.jsr292.methodHandleExceptions;2425import java.io.BufferedOutputStream;26import java.io.FileNotFoundException;27import java.io.FileOutputStream;28import java.io.IOException;29import java.net.URL;30import java.net.URLClassLoader;31import java.util.jar.JarEntry;32import java.util.jar.JarOutputStream;3334/**35* A ByteClassLoader is used to define classes from collections of bytes, as36* well as loading classes in the usual way. It includes options to write the37* classes to files in a jar, or to read the classes from jars in a later or38* debugging run.39*40* If Boolean property byteclassloader.verbose is true, be chatty about jar41* file operations.42*43*/44public class ByteClassLoader extends URLClassLoader {4546final static boolean verbose47= Boolean.getBoolean("byteclassloader.verbose");4849final boolean read;50final JarOutputStream jos;51final String jar_name;5253/**54* Make a new ByteClassLoader.55*56* @param jar_name Basename of jar file to be read/written by this classloader.57* @param read If true, read classes from jar file instead of from parameter.58* @param write If true, write classes to jar files for offline study/use.59*60* @throws FileNotFoundException61* @throws IOException62*/63public ByteClassLoader(String jar_name, boolean read, boolean write)64throws FileNotFoundException, IOException {65super(read66? new URL[]{new URL("file:" + jar_name + ".jar")}67: new URL[0]);68this.read = read;69this.jar_name = jar_name;70this.jos = write71? new JarOutputStream(72new BufferedOutputStream(73new FileOutputStream(jar_name + ".jar"))) : null;74if (read && write) {75throw new Error("At most one of read and write may be true.");76}77}7879private static void writeJarredFile(JarOutputStream jos, String file, String suffix, byte[] bytes) {80String fileName = file.replace(".", "/") + "." + suffix;81JarEntry ze = new JarEntry(fileName);82try {83ze.setSize(bytes.length);84jos.putNextEntry(ze);85jos.write(bytes);86jos.closeEntry();87} catch (IOException e) {88throw new RuntimeException(e);89}90}9192/**93* (pre)load class name using classData for the definition.94*95* @param name96* @param classData97* @return98*/99public Class<?> loadBytes(String name, byte[] classData) throws ClassNotFoundException {100if (jos != null) {101if (verbose) {102System.out.println("ByteClassLoader: writing " + name);103}104writeJarredFile(jos, name, "class", classData);105}106107Class<?> clazz = null;108if (read) {109if (verbose) {110System.out.println("ByteClassLoader: reading " + name + " from " + jar_name);111}112clazz = loadClass(name);113} else {114clazz = defineClass(name, classData, 0, classData.length);115resolveClass(clazz);116}117return clazz;118}119120public void close() {121if (jos != null) {122try {123if (verbose) {124System.out.println("ByteClassLoader: closing " + jar_name);125}126jos.close();127} catch (IOException ex) {128}129}130}131}132133134