< prev index next >

modules/fxml/src/main/java/com/sun/javafx/fxml/ModuleHelper.java

Print this page
rev 9717 : 8151320: Remove unnecessary addReads from JavaFX


  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);


  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 getResourceAsStreamMethod;
  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 mGetResourceAsStream = null;
  50         try {
  51             mGetModule = Class.class.getMethod("getModule");
  52             Class<?> moduleClass = mGetModule.getReturnType();


  53             mGetResourceAsStream = moduleClass.getMethod("getResourceAsStream", String.class);
  54         } catch (NoSuchMethodException e) {
  55             // ignore
  56         }
  57         getModuleMethod = mGetModule;


  58         getResourceAsStreamMethod = mGetResourceAsStream;
  59         if (verbose) {
  60             System.err.println("getModuleMethod = " + getModuleMethod);


  61             System.err.println("getResourceAsStreamMethod = " + getResourceAsStreamMethod);
  62         }
  63     }
  64 
  65     public static Object getModule(Class clazz) {
  66         if (getModuleMethod != null) {
  67             try {
  68                 return getModuleMethod.invoke(clazz);
  69             } catch (IllegalAccessException | InvocationTargetException ex) {
  70                 throw new RuntimeException(ex);
  71             }
  72         }
  73         return null;
  74     }
  75 
  76     // FIXME: JIGSAW -- remove this method if not needed



















  77     public static InputStream getResourceAsStream(Object thisModule, String name) {
  78         if (getResourceAsStreamMethod != null) {
  79             try {
  80                 return (InputStream)getResourceAsStreamMethod.invoke(thisModule, name);
  81             } catch (IllegalAccessException | InvocationTargetException ex) {
  82                 throw new RuntimeException(ex);
  83             }
  84         }
  85         return null;
  86     }
  87 
  88     public static Object invoke(Method m, Object obj, Object[] params)
  89             throws InvocationTargetException, IllegalAccessException
  90     {
  91         Object thisModule = getModule(ModuleHelper.class);
  92         Object methodModule = getModule(m.getDeclaringClass());
  93         if (verbose) {
  94             System.out.println("thisModule = " + thisModule);
  95             System.out.println("methodModule = " + methodModule);
  96             System.out.println("m = " + m);
< prev index next >