1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/compiler/jsr292/methodHandleExceptions/ByteClassLoader.java Wed Apr 27 01:25:04 2016 +0800 1.3 @@ -0,0 +1,131 @@ 1.4 +import java.io.BufferedOutputStream; 1.5 +import java.io.FileNotFoundException; 1.6 +import java.io.FileOutputStream; 1.7 +import java.io.IOException; 1.8 +import java.net.URL; 1.9 +import java.net.URLClassLoader; 1.10 +import java.util.jar.JarEntry; 1.11 +import java.util.jar.JarOutputStream; 1.12 + 1.13 +/* 1.14 + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. 1.15 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.16 + * 1.17 + * This code is free software; you can redistribute it and/or modify it 1.18 + * under the terms of the GNU General Public License version 2 only, as 1.19 + * published by the Free Software Foundation. 1.20 + * 1.21 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.22 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.23 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.24 + * version 2 for more details (a copy is included in the LICENSE file that 1.25 + * accompanied this code). 1.26 + * 1.27 + * You should have received a copy of the GNU General Public License version 1.28 + * 2 along with this work; if not, write to the Free Software Foundation, 1.29 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.30 + * 1.31 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.32 + * or visit www.oracle.com if you need additional information or have any 1.33 + * questions. 1.34 + * 1.35 + */ 1.36 + 1.37 +/** 1.38 + * A ByteClassLoader is used to define classes from collections of bytes, as 1.39 + * well as loading classes in the usual way. It includes options to write the 1.40 + * classes to files in a jar, or to read the classes from jars in a later or 1.41 + * debugging run. 1.42 + * 1.43 + * If Boolean property byteclassloader.verbose is true, be chatty about jar 1.44 + * file operations. 1.45 + * 1.46 + */ 1.47 +public class ByteClassLoader extends URLClassLoader { 1.48 + 1.49 + final static boolean verbose 1.50 + = Boolean.getBoolean("byteclassloader.verbose"); 1.51 + 1.52 + final boolean read; 1.53 + final JarOutputStream jos; 1.54 + final String jar_name; 1.55 + 1.56 + /** 1.57 + * Make a new ByteClassLoader. 1.58 + * 1.59 + * @param jar_name Basename of jar file to be read/written by this classloader. 1.60 + * @param read If true, read classes from jar file instead of from parameter. 1.61 + * @param write If true, write classes to jar files for offline study/use. 1.62 + * 1.63 + * @throws FileNotFoundException 1.64 + * @throws IOException 1.65 + */ 1.66 + public ByteClassLoader(String jar_name, boolean read, boolean write) 1.67 + throws FileNotFoundException, IOException { 1.68 + super(read 1.69 + ? new URL[]{new URL("file:" + jar_name + ".jar")} 1.70 + : new URL[0]); 1.71 + this.read = read; 1.72 + this.jar_name = jar_name; 1.73 + this.jos = write 1.74 + ? new JarOutputStream( 1.75 + new BufferedOutputStream( 1.76 + new FileOutputStream(jar_name + ".jar"))) : null; 1.77 + if (read && write) { 1.78 + throw new Error("At most one of read and write may be true."); 1.79 + } 1.80 + } 1.81 + 1.82 + private static void writeJarredFile(JarOutputStream jos, String file, String suffix, byte[] bytes) { 1.83 + String fileName = file.replace(".", "/") + "." + suffix; 1.84 + JarEntry ze = new JarEntry(fileName); 1.85 + try { 1.86 + ze.setSize(bytes.length); 1.87 + jos.putNextEntry(ze); 1.88 + jos.write(bytes); 1.89 + jos.closeEntry(); 1.90 + } catch (IOException e) { 1.91 + throw new RuntimeException(e); 1.92 + } 1.93 + } 1.94 + 1.95 + /** 1.96 + * (pre)load class name using classData for the definition. 1.97 + * 1.98 + * @param name 1.99 + * @param classData 1.100 + * @return 1.101 + */ 1.102 + public Class<?> loadBytes(String name, byte[] classData) throws ClassNotFoundException { 1.103 + if (jos != null) { 1.104 + if (verbose) { 1.105 + System.out.println("ByteClassLoader: writing " + name); 1.106 + } 1.107 + writeJarredFile(jos, name, "class", classData); 1.108 + } 1.109 + 1.110 + Class<?> clazz = null; 1.111 + if (read) { 1.112 + if (verbose) { 1.113 + System.out.println("ByteClassLoader: reading " + name + " from " + jar_name); 1.114 + } 1.115 + clazz = loadClass(name); 1.116 + } else { 1.117 + clazz = defineClass(name, classData, 0, classData.length); 1.118 + resolveClass(clazz); 1.119 + } 1.120 + return clazz; 1.121 + } 1.122 + 1.123 + public void close() { 1.124 + if (jos != null) { 1.125 + try { 1.126 + if (verbose) { 1.127 + System.out.println("ByteClassLoader: closing " + jar_name); 1.128 + } 1.129 + jos.close(); 1.130 + } catch (IOException ex) { 1.131 + } 1.132 + } 1.133 + } 1.134 +}