c1ReadEdgeDiffLoader.java revision 12735:afedee84773e
1284990Scy/*
2284990Scy * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
3284990Scy * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4284990Scy *
5284990Scy * This code is free software; you can redistribute it and/or modify it
6284990Scy * under the terms of the GNU General Public License version 2 only, as
7284990Scy * published by the Free Software Foundation.
8284990Scy *
9284990Scy * This code is distributed in the hope that it will be useful, but WITHOUT
10284990Scy * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11284990Scy * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12284990Scy * version 2 for more details (a copy is included in the LICENSE file that
13284990Scy * accompanied this code).
14284990Scy *
15284990Scy * You should have received a copy of the GNU General Public License version
16284990Scy * 2 along with this work; if not, write to the Free Software Foundation,
17284990Scy * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18284990Scy *
19284990Scy * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20284990Scy * or visit www.oracle.com if you need additional information or have any
21284990Scy * questions.
22284990Scy */
23284990Scypackage p1;
24284990Scy
25290000Sglebiusimport myloaders.MyDiffClassLoader;
26290000Sglebiusimport p2.c2;
27290000Sglebius
28284990Scypublic class c1ReadEdgeDiffLoader {
29284990Scy    public c1ReadEdgeDiffLoader() {
30284990Scy        // The goal is to establish a read edge between module m1x
31284990Scy        // which is the module where p1.c1ReadEdgeDiffLoader is defined,
32290000Sglebius        // and the unnamed module that defines p2.c2.  This must be
33290000Sglebius        // done in 2 steps:
34284990Scy        //
35284990Scy        // Step #1: Establish a read edge between m1x, where c1ReadEdgeDiffLoader
36284990Scy        //          is defined, and the System ClassLoader's unnamed module,
37290000Sglebius        //          where MyDiffClassLoader is defined. This read edge
38290000Sglebius        //          is needed before we can obtain MyDiffClassLoader.loader2's unnamed module.
39284990Scy        //
40284990Scy        // Step #2: Establish a read edge between m1x, where c1ReadEdgeDiffLoader
41284990Scy        //          is defined, and the MyDiffClassLoader.loader2's unnamed module,
42284990Scy        //          where p2.c2 will be defined.
43284990Scy
44290000Sglebius        // Step #1: read edge m1x -> System ClassLoader's unnamed module
45284990Scy        Module m1x = c1ReadEdgeDiffLoader.class.getModule();
46284990Scy        ClassLoader system_loader = ClassLoader.getSystemClassLoader();
47284990Scy        Module unnamed_module_one = system_loader.getUnnamedModule();
48284990Scy        m1x.addReads(unnamed_module_one);
49284990Scy
50284990Scy        // Step #2: read edge m1x -> MyDiffClassLoader.loader2's unnamed module
51284990Scy        ClassLoader loader2 = MyDiffClassLoader.loader2;
52293894Sglebius        Module unnamed_module_two = loader2.getUnnamedModule();
53293894Sglebius        m1x.addReads(unnamed_module_two);
54284990Scy
55284990Scy        // Attempt access - access should succeed since m1x can read
56284990Scy        //                  MyDiffClassLoader.loader2's unnamed module
57        p2.c2 c2_obj = new p2.c2();
58        c2_obj.method2();
59    }
60}
61