hotspot/test/runtime/modules/ModuleHelper.java
author alanb
Thu, 01 Dec 2016 08:56:41 +0000
changeset 42307 cefc81dc1d52
parent 36508 5f9eee6b383b
child 43665 4bb003cad9b9
permissions -rw-r--r--
8169069: Module system implementation refresh (11/2016) Reviewed-by: lfoltan, acorn, ctornqvi, mchung Contributed-by: lois.foltan@oracle.com, harold.seigel@oracle.com, alan.bateman@oracle.com, mandy.chung@oracle.com, serguei.spitsyn@oracle.com, george.triantafillou@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
     4
 *
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 *
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * accompanied this code).
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 *
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 *
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * questions.
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 */
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    23
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    24
import java.net.URI;
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    25
import java.lang.module.ModuleDescriptor;
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    26
import java.lang.reflect.Module;
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    27
import java.util.Collections;
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    28
import java.util.HashSet;
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    29
import java.util.Set;
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    30
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    31
import sun.hotspot.WhiteBox;
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    32
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    33
public class ModuleHelper {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    34
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    35
    public static void DefineModule(Object module, String version, String location,
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    36
                                    String[] pkgs) throws Throwable {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    37
        WhiteBox wb = WhiteBox.getWhiteBox();
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    38
        wb.DefineModule(module, version, location, pkgs);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    39
    }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    40
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    41
    public static void AddModuleExports(Object from, String pkg, Object to) throws Throwable {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    42
        WhiteBox wb = WhiteBox.getWhiteBox();
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    43
        wb.AddModuleExports(from, pkg, to);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    44
        java.lang.reflect.ModuleHelper.addExportsNoSync((Module)from, pkg, (Module)to);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    45
    }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    46
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    47
    public static void AddReadsModule(Object from, Object to) throws Throwable {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    48
        WhiteBox wb = WhiteBox.getWhiteBox();
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    49
        wb.AddReadsModule(from, to);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    50
        java.lang.reflect.ModuleHelper.addReadsNoSync((Module)from, (Module)to);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    51
    }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    52
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    53
    public static void AddModulePackage(Object m, String pkg) throws Throwable {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    54
        WhiteBox wb = WhiteBox.getWhiteBox();
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    55
        wb.AddModulePackage(m, pkg);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    56
        java.lang.reflect.ModuleHelper.addPackageNoSync((Module)m, pkg);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    57
    }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    58
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    59
    public static Module GetModuleByPackageName(Object ldr, String pkg) throws Throwable {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    60
        WhiteBox wb = WhiteBox.getWhiteBox();
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    61
        return (Module)wb.GetModuleByPackageName(ldr, pkg);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    62
    }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    63
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    64
    public static void AddModuleExportsToAllUnnamed(Object m, String pkg) throws Throwable {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    65
        WhiteBox wb = WhiteBox.getWhiteBox();
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    66
        wb.AddModuleExportsToAllUnnamed(m, pkg);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    67
        //java.lang.reflect.ModuleHelper.addExportsToAllUnnamedNoSync((Module)m, pkg);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    68
    }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    69
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    70
    public static void AddModuleExportsToAll(Object m, String pkg) throws Throwable {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    71
        WhiteBox wb = WhiteBox.getWhiteBox();
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    72
        wb.AddModuleExportsToAll(m, pkg);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    73
        java.lang.reflect.ModuleHelper.addExportsNoSync((Module)m, pkg, (Module)null);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    74
    }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    75
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    76
    public static Module ModuleObject(String name, ClassLoader loader, String[] pkgs) throws Throwable {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    77
        Set<String> pkg_set = new HashSet<>();
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    78
        if (pkgs != null) {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    79
            for (String pkg: pkgs) {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    80
                pkg_set.add(pkg.replace('/', '.'));
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    81
            }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    82
        } else {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    83
            pkg_set = Collections.emptySet();
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    84
        }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    85
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    86
        ModuleDescriptor descriptor =
42307
cefc81dc1d52 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36508
diff changeset
    87
            ModuleDescriptor.module(name).contains(pkg_set).build();
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    88
        URI uri = URI.create("module:/" + name);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    89
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    90
        return java.lang.reflect.ModuleHelper.newModule(loader, descriptor);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    91
    }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    92
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    93
}