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 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.fxml;
  27 
  28 import java.io.InputStream;
  29 import java.lang.reflect.InvocationTargetException;
  30 import java.lang.reflect.Method;
  31 import java.security.AccessController;
  32 import java.security.PrivilegedAction;
  33 import sun.reflect.misc.MethodUtil;
  34 
  35 public class ModuleHelper {
  36     private static final Method getModuleMethod;
  37     private static final Method addReadsMethod;
  38     private static final Method addExportsMethod;
  39     private static final Method getResourceAsStreamMethod;
  40 
  41     private static final boolean verbose;
  42 
  43     static {
  44         verbose = AccessController.doPrivileged((PrivilegedAction<Boolean>) () ->
  45                 Boolean.getBoolean("javafx.verbose"));
  46 
  47         if (verbose) {
  48             System.err.println("" + ModuleHelper.class.getName() + " : <clinit>");
  49         }
  50         Method mGetModule = null;
  51         Method mAddReads = null;
  52         Method mAddExports = null;
  53         Method mGetResourceAsStream = null;
  54         try {
  55             mGetModule = Class.class.getMethod("getModule");
  56             Class<?> moduleClass = mGetModule.getReturnType();
  57             mAddReads = moduleClass.getMethod("addReads", moduleClass);
  58             mAddExports = moduleClass.getMethod("addExports", String.class, moduleClass);
  59             mGetResourceAsStream = moduleClass.getMethod("getResourceAsStream", String.class);
  60         } catch (NoSuchMethodException e) {
  61             // ignore
  62         }
  63         getModuleMethod = mGetModule;
  64         addReadsMethod = mAddReads;
  65         addExportsMethod = mAddExports;
  66         getResourceAsStreamMethod = mGetResourceAsStream;
  67         if (verbose) {
  68             System.err.println("getModuleMethod = " + getModuleMethod);
  69             System.err.println("addReadsMethod = " + addReadsMethod);
  70             System.err.println("addExportsMethod = " + addExportsMethod);
  71             System.err.println("getResourceAsStreamMethod = " + getResourceAsStreamMethod);
  72         }
  73     }
  74 
  75     public static Object getModule(Class clazz) {
  76         if (getModuleMethod != null) {
  77             try {
  78                 return getModuleMethod.invoke(clazz);
  79             } catch (IllegalAccessException | InvocationTargetException ex) {
  80                 throw new RuntimeException(ex);
  81             }
  82         }
  83         return null;
  84     }
  85 
  86     public static void addReads(Object thisModule, Object targetModule) {
  87         if (addReadsMethod != null) {
  88             try {
  89                 addReadsMethod.invoke(thisModule, targetModule);
  90             } catch (IllegalAccessException | InvocationTargetException ex) {
  91                 throw new RuntimeException(ex);
  92             }
  93         }
  94     }
  95 
  96     public static void addExports(Object thisModule, String packageName, Object targetModule) {
  97         if (addExportsMethod != null) {
  98             try {
  99                 addExportsMethod.invoke(thisModule, packageName, targetModule);
 100             } catch (IllegalAccessException | InvocationTargetException ex) {
 101                 throw new RuntimeException(ex);
 102             }
 103         }
 104     }
 105 
 106     public static InputStream getResourceAsStream(Object thisModule, String name) {
 107         if (getResourceAsStreamMethod != null) {
 108             try {
 109                 return (InputStream)getResourceAsStreamMethod.invoke(thisModule, name);
 110             } catch (IllegalAccessException | InvocationTargetException ex) {
 111                 throw new RuntimeException(ex);
 112             }
 113         }
 114         return null;
 115     }
 116 
 117     public static Object invoke(Method m, Object obj, Object[] params)
 118             throws InvocationTargetException, IllegalAccessException
 119     {
 120         Object thisModule = getModule(ModuleHelper.class);
 121         Object methodModule = getModule(m.getDeclaringClass());
 122         if (verbose) {
 123             System.out.println("thisModule = " + thisModule);
 124             System.out.println("methodModule = " + methodModule);
 125             System.out.println("m = " + m);
 126         }
 127         if (methodModule != thisModule) {
 128             return MethodUtil.invoke(m, obj, params);
 129         } else {
 130             return m.invoke(obj, params);
 131         }
 132     }
 133 }