jdk
1/*
2* Copyright (c) 2015, 2017, 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*/
23package p1;24
25import myloaders.MyDiffClassLoader;26import p2.c2;27
28public class c1ReadEdgeDiffLoader {29public c1ReadEdgeDiffLoader() {30// The goal is to establish a read edge between module m1x31// which is the module where p1.c1ReadEdgeDiffLoader is defined,32// and the unnamed module that defines p2.c2. This must be33// done in 2 steps:34//35// Step #1: Establish a read edge between m1x, where c1ReadEdgeDiffLoader36// is defined, and the System ClassLoader's unnamed module,37// where MyDiffClassLoader is defined. This read edge38// is needed before we can obtain MyDiffClassLoader.loader2's unnamed module.39//40// Step #2: Establish a read edge between m1x, where c1ReadEdgeDiffLoader41// is defined, and the MyDiffClassLoader.loader2's unnamed module,42// where p2.c2 will be defined.43
44// Step #1: read edge m1x -> System ClassLoader's unnamed module45Module m1x = c1ReadEdgeDiffLoader.class.getModule();46ClassLoader system_loader = ClassLoader.getSystemClassLoader();47Module unnamed_module_one = system_loader.getUnnamedModule();48m1x.addReads(unnamed_module_one);49
50// Step #2: read edge m1x -> MyDiffClassLoader.loader2's unnamed module51ClassLoader loader2 = MyDiffClassLoader.loader2;52Module unnamed_module_two = loader2.getUnnamedModule();53m1x.addReads(unnamed_module_two);54
55// Attempt access - access should succeed since m1x can read56// MyDiffClassLoader.loader2's unnamed module57p2.c2 c2_obj = new p2.c2();58c2_obj.method2();59}60}
61