test/compiler/jsr292/methodHandleExceptions/ByteClassLoader.java

Wed, 27 Apr 2016 01:25:04 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:25:04 +0800
changeset 0
f90c822e73f8
child 6876
710a3c8b516e
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/
changeset: 6782:28b50d07f6f8
tag: jdk8u25-b17

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

mercurial