author | goetz |
Wed, 27 Jun 2018 09:52:23 +0200 | |
changeset 50816 | a73848f8d0ad |
permissions | -rw-r--r-- |
50816
a73848f8d0ad
8199940: Print more information about class loaders in IllegalAccessErrors.
goetz
parents:
diff
changeset
|
1 |
/* |
a73848f8d0ad
8199940: Print more information about class loaders in IllegalAccessErrors.
goetz
parents:
diff
changeset
|
2 |
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. |
a73848f8d0ad
8199940: Print more information about class loaders in IllegalAccessErrors.
goetz
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
a73848f8d0ad
8199940: Print more information about class loaders in IllegalAccessErrors.
goetz
parents:
diff
changeset
|
4 |
* |
a73848f8d0ad
8199940: Print more information about class loaders in IllegalAccessErrors.
goetz
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
a73848f8d0ad
8199940: Print more information about class loaders in IllegalAccessErrors.
goetz
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
a73848f8d0ad
8199940: Print more information about class loaders in IllegalAccessErrors.
goetz
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
a73848f8d0ad
8199940: Print more information about class loaders in IllegalAccessErrors.
goetz
parents:
diff
changeset
|
8 |
* |
a73848f8d0ad
8199940: Print more information about class loaders in IllegalAccessErrors.
goetz
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
a73848f8d0ad
8199940: Print more information about class loaders in IllegalAccessErrors.
goetz
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
a73848f8d0ad
8199940: Print more information about class loaders in IllegalAccessErrors.
goetz
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
a73848f8d0ad
8199940: Print more information about class loaders in IllegalAccessErrors.
goetz
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
a73848f8d0ad
8199940: Print more information about class loaders in IllegalAccessErrors.
goetz
parents:
diff
changeset
|
13 |
* accompanied this code). |
a73848f8d0ad
8199940: Print more information about class loaders in IllegalAccessErrors.
goetz
parents:
diff
changeset
|
14 |
* |
a73848f8d0ad
8199940: Print more information about class loaders in IllegalAccessErrors.
goetz
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
a73848f8d0ad
8199940: Print more information about class loaders in IllegalAccessErrors.
goetz
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
a73848f8d0ad
8199940: Print more information about class loaders in IllegalAccessErrors.
goetz
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
a73848f8d0ad
8199940: Print more information about class loaders in IllegalAccessErrors.
goetz
parents:
diff
changeset
|
18 |
* |
a73848f8d0ad
8199940: Print more information about class loaders in IllegalAccessErrors.
goetz
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
a73848f8d0ad
8199940: Print more information about class loaders in IllegalAccessErrors.
goetz
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
a73848f8d0ad
8199940: Print more information about class loaders in IllegalAccessErrors.
goetz
parents:
diff
changeset
|
21 |
* questions. |
a73848f8d0ad
8199940: Print more information about class loaders in IllegalAccessErrors.
goetz
parents:
diff
changeset
|
22 |
*/ |
a73848f8d0ad
8199940: Print more information about class loaders in IllegalAccessErrors.
goetz
parents:
diff
changeset
|
23 |
|
a73848f8d0ad
8199940: Print more information about class loaders in IllegalAccessErrors.
goetz
parents:
diff
changeset
|
24 |
package test; |
a73848f8d0ad
8199940: Print more information about class loaders in IllegalAccessErrors.
goetz
parents:
diff
changeset
|
25 |
|
a73848f8d0ad
8199940: Print more information about class loaders in IllegalAccessErrors.
goetz
parents:
diff
changeset
|
26 |
import java.util.*; |
a73848f8d0ad
8199940: Print more information about class loaders in IllegalAccessErrors.
goetz
parents:
diff
changeset
|
27 |
import java.io.*; |
a73848f8d0ad
8199940: Print more information about class loaders in IllegalAccessErrors.
goetz
parents:
diff
changeset
|
28 |
|
a73848f8d0ad
8199940: Print more information about class loaders in IllegalAccessErrors.
goetz
parents:
diff
changeset
|
29 |
public class IAE_Loader1 extends ClassLoader { |
a73848f8d0ad
8199940: Print more information about class loaders in IllegalAccessErrors.
goetz
parents:
diff
changeset
|
30 |
|
a73848f8d0ad
8199940: Print more information about class loaders in IllegalAccessErrors.
goetz
parents:
diff
changeset
|
31 |
private final Set<String> names = new HashSet<>(); |
a73848f8d0ad
8199940: Print more information about class loaders in IllegalAccessErrors.
goetz
parents:
diff
changeset
|
32 |
|
a73848f8d0ad
8199940: Print more information about class loaders in IllegalAccessErrors.
goetz
parents:
diff
changeset
|
33 |
public IAE_Loader1(String name, String[] names) { |
a73848f8d0ad
8199940: Print more information about class loaders in IllegalAccessErrors.
goetz
parents:
diff
changeset
|
34 |
super(name, ClassLoader.getSystemClassLoader()); |
a73848f8d0ad
8199940: Print more information about class loaders in IllegalAccessErrors.
goetz
parents:
diff
changeset
|
35 |
for (String n : names) this.names.add(n); |
a73848f8d0ad
8199940: Print more information about class loaders in IllegalAccessErrors.
goetz
parents:
diff
changeset
|
36 |
} |
a73848f8d0ad
8199940: Print more information about class loaders in IllegalAccessErrors.
goetz
parents:
diff
changeset
|
37 |
|
a73848f8d0ad
8199940: Print more information about class loaders in IllegalAccessErrors.
goetz
parents:
diff
changeset
|
38 |
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { |
a73848f8d0ad
8199940: Print more information about class loaders in IllegalAccessErrors.
goetz
parents:
diff
changeset
|
39 |
if (!names.contains(name)) { |
a73848f8d0ad
8199940: Print more information about class loaders in IllegalAccessErrors.
goetz
parents:
diff
changeset
|
40 |
return super.loadClass(name, resolve); |
a73848f8d0ad
8199940: Print more information about class loaders in IllegalAccessErrors.
goetz
parents:
diff
changeset
|
41 |
} |
a73848f8d0ad
8199940: Print more information about class loaders in IllegalAccessErrors.
goetz
parents:
diff
changeset
|
42 |
String filename = name.replace('.', '/') + ".class"; |
a73848f8d0ad
8199940: Print more information about class loaders in IllegalAccessErrors.
goetz
parents:
diff
changeset
|
43 |
Class<?> result = null; |
a73848f8d0ad
8199940: Print more information about class loaders in IllegalAccessErrors.
goetz
parents:
diff
changeset
|
44 |
try (InputStream data = getResourceAsStream(filename)) { |
a73848f8d0ad
8199940: Print more information about class loaders in IllegalAccessErrors.
goetz
parents:
diff
changeset
|
45 |
if (data == null) { |
a73848f8d0ad
8199940: Print more information about class loaders in IllegalAccessErrors.
goetz
parents:
diff
changeset
|
46 |
throw new ClassNotFoundException(); |
a73848f8d0ad
8199940: Print more information about class loaders in IllegalAccessErrors.
goetz
parents:
diff
changeset
|
47 |
} |
a73848f8d0ad
8199940: Print more information about class loaders in IllegalAccessErrors.
goetz
parents:
diff
changeset
|
48 |
try (ByteArrayOutputStream buffer = new ByteArrayOutputStream()) { |
a73848f8d0ad
8199940: Print more information about class loaders in IllegalAccessErrors.
goetz
parents:
diff
changeset
|
49 |
byte[] bytes = data.readAllBytes(); |
a73848f8d0ad
8199940: Print more information about class loaders in IllegalAccessErrors.
goetz
parents:
diff
changeset
|
50 |
result = defineClass(name, bytes, 0, bytes.length); |
a73848f8d0ad
8199940: Print more information about class loaders in IllegalAccessErrors.
goetz
parents:
diff
changeset
|
51 |
} |
a73848f8d0ad
8199940: Print more information about class loaders in IllegalAccessErrors.
goetz
parents:
diff
changeset
|
52 |
} catch (IOException e) { |
a73848f8d0ad
8199940: Print more information about class loaders in IllegalAccessErrors.
goetz
parents:
diff
changeset
|
53 |
throw new ClassNotFoundException("Error reading class file", e); |
a73848f8d0ad
8199940: Print more information about class loaders in IllegalAccessErrors.
goetz
parents:
diff
changeset
|
54 |
} |
a73848f8d0ad
8199940: Print more information about class loaders in IllegalAccessErrors.
goetz
parents:
diff
changeset
|
55 |
if (resolve) resolveClass(result); |
a73848f8d0ad
8199940: Print more information about class loaders in IllegalAccessErrors.
goetz
parents:
diff
changeset
|
56 |
return result; |
a73848f8d0ad
8199940: Print more information about class loaders in IllegalAccessErrors.
goetz
parents:
diff
changeset
|
57 |
} |
a73848f8d0ad
8199940: Print more information about class loaders in IllegalAccessErrors.
goetz
parents:
diff
changeset
|
58 |
|
a73848f8d0ad
8199940: Print more information about class loaders in IllegalAccessErrors.
goetz
parents:
diff
changeset
|
59 |
} |