Mercurial > hg > truffle
annotate test/compiler/jsr292/methodHandleExceptions/ByteClassLoader.java @ 15286:f37a81a76000
handle array length in PEAReadElimination
author | Lukas Stadler <lukas.stadler@oracle.com> |
---|---|
date | Wed, 23 Apr 2014 10:26:19 +0200 |
parents | 9d15b81d5d1b |
children |
rev | line source |
---|---|
13403
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
1 import java.io.BufferedOutputStream; |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
2 import java.io.FileNotFoundException; |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
3 import java.io.FileOutputStream; |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
4 import java.io.IOException; |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
5 import java.net.URL; |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
6 import java.net.URLClassLoader; |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
7 import java.util.jar.JarEntry; |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
8 import java.util.jar.JarOutputStream; |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
9 |
12332
dc261f466b6d
8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff
changeset
|
10 /* |
dc261f466b6d
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. |
dc261f466b6d
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. |
dc261f466b6d
8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff
changeset
|
13 * |
dc261f466b6d
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 |
dc261f466b6d
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 |
dc261f466b6d
8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff
changeset
|
16 * published by the Free Software Foundation. |
dc261f466b6d
8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff
changeset
|
17 * |
dc261f466b6d
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 |
dc261f466b6d
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 |
dc261f466b6d
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 |
dc261f466b6d
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 |
dc261f466b6d
8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff
changeset
|
22 * accompanied this code). |
dc261f466b6d
8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff
changeset
|
23 * |
dc261f466b6d
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 |
dc261f466b6d
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, |
dc261f466b6d
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. |
dc261f466b6d
8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff
changeset
|
27 * |
dc261f466b6d
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 |
dc261f466b6d
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 |
dc261f466b6d
8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff
changeset
|
30 * questions. |
dc261f466b6d
8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff
changeset
|
31 * |
dc261f466b6d
8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff
changeset
|
32 */ |
dc261f466b6d
8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff
changeset
|
33 |
dc261f466b6d
8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff
changeset
|
34 /** |
13403
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
35 * A ByteClassLoader is used to define classes from collections of bytes, as |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
36 * well as loading classes in the usual way. It includes options to write the |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
37 * classes to files in a jar, or to read the classes from jars in a later or |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
38 * debugging run. |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
39 * |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
40 * If Boolean property byteclassloader.verbose is true, be chatty about jar |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
41 * file operations. |
12332
dc261f466b6d
8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff
changeset
|
42 * |
dc261f466b6d
8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff
changeset
|
43 */ |
13403
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
44 public class ByteClassLoader extends URLClassLoader { |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
45 |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
46 final static boolean verbose |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
47 = Boolean.getBoolean("byteclassloader.verbose"); |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
48 |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
49 final boolean read; |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
50 final JarOutputStream jos; |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
51 final String jar_name; |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
52 |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
53 /** |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
54 * Make a new ByteClassLoader. |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
55 * |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
56 * @param jar_name Basename of jar file to be read/written by this classloader. |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
57 * @param read If true, read classes from jar file instead of from parameter. |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
58 * @param write If true, write classes to jar files for offline study/use. |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
59 * |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
60 * @throws FileNotFoundException |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
61 * @throws IOException |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
62 */ |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
63 public ByteClassLoader(String jar_name, boolean read, boolean write) |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
64 throws FileNotFoundException, IOException { |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
65 super(read |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
66 ? new URL[]{new URL("file:" + jar_name + ".jar")} |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
67 : new URL[0]); |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
68 this.read = read; |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
69 this.jar_name = jar_name; |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
70 this.jos = write |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
71 ? new JarOutputStream( |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
72 new BufferedOutputStream( |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
73 new FileOutputStream(jar_name + ".jar"))) : null; |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
74 if (read && write) { |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
75 throw new Error("At most one of read and write may be true."); |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
76 } |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
77 } |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
78 |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
79 private static void writeJarredFile(JarOutputStream jos, String file, String suffix, byte[] bytes) { |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
80 String fileName = file.replace(".", "/") + "." + suffix; |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
81 JarEntry ze = new JarEntry(fileName); |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
82 try { |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
83 ze.setSize(bytes.length); |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
84 jos.putNextEntry(ze); |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
85 jos.write(bytes); |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
86 jos.closeEntry(); |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
87 } catch (IOException e) { |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
88 throw new RuntimeException(e); |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
89 } |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
90 } |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
91 |
12332
dc261f466b6d
8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff
changeset
|
92 /** |
dc261f466b6d
8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff
changeset
|
93 * (pre)load class name using classData for the definition. |
dc261f466b6d
8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff
changeset
|
94 * |
dc261f466b6d
8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff
changeset
|
95 * @param name |
dc261f466b6d
8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff
changeset
|
96 * @param classData |
dc261f466b6d
8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff
changeset
|
97 * @return |
dc261f466b6d
8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff
changeset
|
98 */ |
13403
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
99 public Class<?> loadBytes(String name, byte[] classData) throws ClassNotFoundException { |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
100 if (jos != null) { |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
101 if (verbose) { |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
102 System.out.println("ByteClassLoader: writing " + name); |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
103 } |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
104 writeJarredFile(jos, name, "class", classData); |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
105 } |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
106 |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
107 Class<?> clazz = null; |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
108 if (read) { |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
109 if (verbose) { |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
110 System.out.println("ByteClassLoader: reading " + name + " from " + jar_name); |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
111 } |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
112 clazz = loadClass(name); |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
113 } else { |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
114 clazz = defineClass(name, classData, 0, classData.length); |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
115 resolveClass(clazz); |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
116 } |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
117 return clazz; |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
118 } |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
119 |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
120 public void close() { |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
121 if (jos != null) { |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
122 try { |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
123 if (verbose) { |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
124 System.out.println("ByteClassLoader: closing " + jar_name); |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
125 } |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
126 jos.close(); |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
127 } catch (IOException ex) { |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
128 } |
9d15b81d5d1b
8016839: JSR292: AME instead of IAE when calling a method
drchase
parents:
12332
diff
changeset
|
129 } |
12332
dc261f466b6d
8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff
changeset
|
130 } |
dc261f466b6d
8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
drchase
parents:
diff
changeset
|
131 } |