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