author | dcubed |
Tue, 05 Jul 2016 14:00:21 -0700 | |
changeset 39696 | 80c9894e18f9 |
parent 21770 | e8932d2fda2c |
child 40059 | c2304140ed64 |
permissions | -rw-r--r-- |
21770
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
1 |
import java.io.BufferedOutputStream; |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
2 |
import java.io.FileNotFoundException; |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
3 |
import java.io.FileOutputStream; |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
4 |
import java.io.IOException; |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
5 |
import java.net.URL; |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
6 |
import java.net.URLClassLoader; |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
7 |
import java.util.jar.JarEntry; |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
8 |
import java.util.jar.JarOutputStream; |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
9 |
|
20298
861da81238ee
8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff
changeset
|
10 |
/* |
861da81238ee
8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff
changeset
|
11 |
* 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
|
12 |
* 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
|
13 |
* |
861da81238ee
8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff
changeset
|
14 |
* 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
|
15 |
* 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
|
16 |
* published by the Free Software Foundation. |
861da81238ee
8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff
changeset
|
17 |
* |
861da81238ee
8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff
changeset
|
18 |
* 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
|
19 |
* 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
|
20 |
* 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
|
21 |
* 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
|
22 |
* accompanied this code). |
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 |
* 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
|
25 |
* 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
|
26 |
* 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
|
27 |
* |
861da81238ee
8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff
changeset
|
28 |
* 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
|
29 |
* 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
|
30 |
* questions. |
861da81238ee
8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff
changeset
|
31 |
* |
861da81238ee
8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff
changeset
|
32 |
*/ |
861da81238ee
8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff
changeset
|
33 |
|
861da81238ee
8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff
changeset
|
34 |
/** |
21770
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
35 |
* 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
|
36 |
* 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
|
37 |
* 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
|
38 |
* debugging run. |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
39 |
* |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
40 |
* 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
|
41 |
* file operations. |
20298
861da81238ee
8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff
changeset
|
42 |
* |
861da81238ee
8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff
changeset
|
43 |
*/ |
21770
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
44 |
public class ByteClassLoader extends URLClassLoader { |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
45 |
|
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
46 |
final static boolean verbose |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
47 |
= Boolean.getBoolean("byteclassloader.verbose"); |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
48 |
|
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
49 |
final boolean read; |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
50 |
final JarOutputStream jos; |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
51 |
final String jar_name; |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
52 |
|
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
53 |
/** |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
54 |
* Make a new ByteClassLoader. |
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 |
* @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
|
57 |
* @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
|
58 |
* @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
|
59 |
* |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
60 |
* @throws FileNotFoundException |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
61 |
* @throws IOException |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
62 |
*/ |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
63 |
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
|
64 |
throws FileNotFoundException, IOException { |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
65 |
super(read |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
66 |
? new URL[]{new URL("file:" + jar_name + ".jar")} |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
67 |
: new URL[0]); |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
68 |
this.read = read; |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
69 |
this.jar_name = jar_name; |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
70 |
this.jos = write |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
71 |
? new JarOutputStream( |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
72 |
new BufferedOutputStream( |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
73 |
new FileOutputStream(jar_name + ".jar"))) : null; |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
74 |
if (read && write) { |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
75 |
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
|
76 |
} |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
77 |
} |
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 |
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
|
80 |
String fileName = file.replace(".", "/") + "." + suffix; |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
81 |
JarEntry ze = new JarEntry(fileName); |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
82 |
try { |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
83 |
ze.setSize(bytes.length); |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
84 |
jos.putNextEntry(ze); |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
85 |
jos.write(bytes); |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
86 |
jos.closeEntry(); |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
87 |
} catch (IOException e) { |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
88 |
throw new RuntimeException(e); |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
89 |
} |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
90 |
} |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
91 |
|
20298
861da81238ee
8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff
changeset
|
92 |
/** |
861da81238ee
8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff
changeset
|
93 |
* (pre)load class name using classData for the definition. |
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 |
* @param name |
861da81238ee
8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff
changeset
|
96 |
* @param classData |
861da81238ee
8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff
changeset
|
97 |
* @return |
861da81238ee
8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff
changeset
|
98 |
*/ |
21770
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
99 |
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
|
100 |
if (jos != null) { |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
101 |
if (verbose) { |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
102 |
System.out.println("ByteClassLoader: writing " + name); |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
103 |
} |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
104 |
writeJarredFile(jos, name, "class", classData); |
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 |
|
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
107 |
Class<?> clazz = null; |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
108 |
if (read) { |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
109 |
if (verbose) { |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
110 |
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
|
111 |
} |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
112 |
clazz = loadClass(name); |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
113 |
} else { |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
114 |
clazz = defineClass(name, classData, 0, classData.length); |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
115 |
resolveClass(clazz); |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
116 |
} |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
117 |
return 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 |
|
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
120 |
public void close() { |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
121 |
if (jos != null) { |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
122 |
try { |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
123 |
if (verbose) { |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
124 |
System.out.println("ByteClassLoader: closing " + jar_name); |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
125 |
} |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
126 |
jos.close(); |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
127 |
} catch (IOException ex) { |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
128 |
} |
e8932d2fda2c
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
20298
diff
changeset
|
129 |
} |
20298
861da81238ee
8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff
changeset
|
130 |
} |
861da81238ee
8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff
changeset
|
131 |
} |