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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.javafx.util;
  27 
  28 import java.lang.reflect.InvocationTargetException;
  29 import java.lang.reflect.Method;
  30 import java.security.AccessController;
  31 import java.security.PrivilegedAction;
  32 
  33 public class ModuleHelper {
  34     private static final Method getModuleMethod;
  35     private static final Method addReadsMethod;
  36     private static final Method addExportsMethod;
  37 
  38     private static final boolean verbose;
  39 
  40     static {
  41         verbose = AccessController.doPrivileged((PrivilegedAction<Boolean>) () ->
  42                 Boolean.getBoolean("javafx.verbose"));
  43 
  44         if (verbose) {
  45             System.err.println("" + ModuleHelper.class.getName() + " : <clinit>");
  46         }
  47         Method mGetModule = null;
  48         Method mAddReads = null;
  49         Method mAddExports = null;
  50         try {
  51             mGetModule = Class.class.getMethod("getModule");
  52             Class<?> moduleClass = mGetModule.getReturnType();
  53             mAddReads = moduleClass.getMethod("addReads", moduleClass);
  54             mAddExports = moduleClass.getMethod("addExports", String.class, moduleClass);
  55         } catch (NoSuchMethodException e) {
  56             // ignore
  57         }
  58         getModuleMethod = mGetModule;
  59         addReadsMethod = mAddReads;
  60         addExportsMethod = mAddExports;
  61         if (verbose) {
  62             System.err.println("getModuleMethod = " + getModuleMethod);
  63             System.err.println("addReadsMethod = " + addReadsMethod);
  64             System.err.println("addExportsMethod = " + addExportsMethod);
  65         }
  66     }
  67 
  68     public static Object getModule(Class clazz) {
  69         if (getModuleMethod != null) {
  70             try {
  71                 return getModuleMethod.invoke(clazz);
  72             } catch (IllegalAccessException | InvocationTargetException ex) {
  73                 throw new RuntimeException(ex);
  74             }
  75         }
  76         return null;
  77     }
  78 
  79     public static void addReads(Object thisModule, Object targetModule) {
  80         if (addReadsMethod != null) {
  81             try {
  82                 addReadsMethod.invoke(thisModule, targetModule);
  83             } catch (IllegalAccessException | InvocationTargetException ex) {
  84                 throw new RuntimeException(ex);
  85             }
  86         }
  87     }
  88 
  89     public static void addExports(Object thisModule, String packageName, Object targetModule) {
  90         if (addExportsMethod != null) {
  91             try {
  92                 addExportsMethod.invoke(thisModule, packageName, targetModule);
  93             } catch (IllegalAccessException | InvocationTargetException ex) {
  94                 throw new RuntimeException(ex);
  95             }
  96         }
  97     }
  98 
  99 }