36511
|
1 |
/*
|
|
2 |
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
|
22 |
*/
|
|
23 |
|
|
24 |
package p3;
|
|
25 |
|
|
26 |
import java.lang.reflect.Layer;
|
|
27 |
import java.lang.reflect.Module;
|
|
28 |
import java.security.AccessControlException;
|
|
29 |
import java.security.Permission;
|
|
30 |
|
|
31 |
/**
|
|
32 |
* Verify RuntimePermission("getClassLoader") is needed to load
|
|
33 |
* a class in another module
|
|
34 |
*/
|
|
35 |
public class NoGetClassLoaderAccess {
|
|
36 |
private static final Module m3 = NoGetClassLoaderAccess.class.getModule();
|
|
37 |
private static final Permission GET_CLASSLOADER_PERMISSION = new RuntimePermission("getClassLoader");
|
|
38 |
|
|
39 |
public static void main(String[] args) throws Exception {
|
|
40 |
Layer boot = Layer.boot();
|
|
41 |
|
|
42 |
System.setSecurityManager(new SecurityManager());
|
|
43 |
Module m1 = boot.findModule("m1").get();
|
|
44 |
Module m2 = boot.findModule("m2").get();
|
|
45 |
findClass(m1, "p1.A");
|
|
46 |
findClass(m1, "p1.internal.B");
|
|
47 |
findClass(m2, "p2.C");
|
|
48 |
findClass(m3, "p3.internal.Foo");
|
|
49 |
}
|
|
50 |
|
|
51 |
static Class<?> findClass(Module module, String cn) {
|
|
52 |
try {
|
|
53 |
Class<?> c = Class.forName(module, cn);
|
|
54 |
if (c == null) {
|
|
55 |
throw new RuntimeException(cn + " not found in " + module);
|
|
56 |
}
|
|
57 |
if (c.getModule() != module) {
|
|
58 |
throw new RuntimeException(c.getModule() + " != " + module);
|
|
59 |
}
|
|
60 |
return c;
|
|
61 |
} catch (AccessControlException e) {
|
|
62 |
if (module != m3) {
|
|
63 |
if (e.getPermission().equals(GET_CLASSLOADER_PERMISSION))
|
|
64 |
return null;
|
|
65 |
}
|
|
66 |
throw e;
|
|
67 |
}
|
|
68 |
}
|
|
69 |
}
|