1 /*
   2  * Copyright (c) 2015, 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 import java.net.URI;
  25 import java.lang.module.ModuleDescriptor;
  26 import java.lang.reflect.Module;
  27 import java.util.Collections;
  28 import java.util.HashSet;
  29 import java.util.Set;
  30 
  31 import sun.hotspot.WhiteBox;
  32 
  33 public class ModuleHelper {
  34 
  35     public static void DefineModule(Object module, String version, String location,
  36                                     String[] pkgs) throws Throwable {
  37         WhiteBox wb = WhiteBox.getWhiteBox();
  38         wb.DefineModule(module, version, location, pkgs);
  39     }
  40 
  41     public static void AddModuleExports(Object from, String pkg, Object to) throws Throwable {
  42         WhiteBox wb = WhiteBox.getWhiteBox();
  43         wb.AddModuleExports(from, pkg, to);
  44         java.lang.reflect.ModuleHelper.addExportsNoSync((Module)from, pkg, (Module)to);
  45     }
  46 
  47     public static void AddReadsModule(Object from, Object to) throws Throwable {
  48         WhiteBox wb = WhiteBox.getWhiteBox();
  49         wb.AddReadsModule(from, to);
  50         java.lang.reflect.ModuleHelper.addReadsNoSync((Module)from, (Module)to);
  51     }
  52 
  53     public static void AddModulePackage(Object m, String pkg) throws Throwable {
  54         WhiteBox wb = WhiteBox.getWhiteBox();
  55         wb.AddModulePackage(m, pkg);
  56         java.lang.reflect.ModuleHelper.addPackageNoSync((Module)m, pkg);
  57     }
  58 
  59     public static Module GetModuleByPackageName(Object ldr, String pkg) throws Throwable {
  60         WhiteBox wb = WhiteBox.getWhiteBox();
  61         return (Module)wb.GetModuleByPackageName(ldr, pkg);
  62     }
  63 
  64     public static void AddModuleExportsToAllUnnamed(Object m, String pkg) throws Throwable {
  65         WhiteBox wb = WhiteBox.getWhiteBox();
  66         wb.AddModuleExportsToAllUnnamed(m, pkg);
  67         //java.lang.reflect.ModuleHelper.addExportsToAllUnnamedNoSync((Module)m, pkg);
  68     }
  69 
  70     public static void AddModuleExportsToAll(Object m, String pkg) throws Throwable {
  71         WhiteBox wb = WhiteBox.getWhiteBox();
  72         wb.AddModuleExportsToAll(m, pkg);
  73         java.lang.reflect.ModuleHelper.addExportsNoSync((Module)m, pkg, (Module)null);
  74     }
  75 
  76     public static Module ModuleObject(String name, ClassLoader loader, String[] pkgs) throws Throwable {
  77         Set<String> pkg_set = new HashSet<>();
  78         if (pkgs != null) {
  79             for (String pkg: pkgs) {
  80                 pkg_set.add(pkg.replace('/', '.'));
  81             }
  82         } else {
  83             pkg_set = Collections.emptySet();
  84         }
  85 
  86         ModuleDescriptor descriptor =
  87             ModuleDescriptor.newModule(name).packages(pkg_set).build();
  88         URI uri = URI.create("module:/" + name);
  89 
  90         return java.lang.reflect.ModuleHelper.newModule(loader, descriptor);
  91     }
  92 
  93 }