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; 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 34 public class ModuleHelper { 35 private static final Method getModuleMethod; 36 private static final Method addReadsMethod; 37 private static final Method addExportsMethod; 38 private static final Method getResourceAsStreamMethod; 39 40 private static final boolean verbose; 41 42 static { 43 verbose = AccessController.doPrivileged((PrivilegedAction<Boolean>) () -> 44 Boolean.getBoolean("javafx.verbose")); 45 46 if (verbose) { 47 System.err.println("" + ModuleHelper.class.getName() + " : <clinit>"); 48 } 49 Method mGetModule = null; 50 Method mAddReads = null; 51 Method mAddExports = null; 52 Method mGetResourceAsStream = null; 53 try { 54 mGetModule = Class.class.getMethod("getModule"); 55 Class<?> moduleClass = mGetModule.getReturnType(); 56 mAddReads = moduleClass.getMethod("addReads", moduleClass); 57 mAddExports = moduleClass.getMethod("addExports", String.class, moduleClass); 58 mGetResourceAsStream = moduleClass.getMethod("getResourceAsStream", String.class); 59 } catch (NoSuchMethodException e) { 60 // ignore 61 } 62 getModuleMethod = mGetModule; 63 addReadsMethod = mAddReads; 64 addExportsMethod = mAddExports; 65 getResourceAsStreamMethod = mGetResourceAsStream; 66 if (verbose) { 67 System.err.println("getModuleMethod = " + getModuleMethod); 68 System.err.println("addReadsMethod = " + addReadsMethod); 69 System.err.println("addExportsMethod = " + addExportsMethod); 70 System.err.println("getResourceAsStreamMethod = " + getResourceAsStreamMethod); 71 } 72 } 73 74 public static Object getModule(Class clazz) { 75 if (getModuleMethod != null) { 76 try { 77 return getModuleMethod.invoke(clazz); 78 } catch (IllegalAccessException | InvocationTargetException ex) { 79 throw new RuntimeException(ex); 80 } 81 } 82 return null; 83 } 84 85 public static void addReads(Object thisModule, Object targetModule) { 86 if (addReadsMethod != null) { 87 try { 88 addReadsMethod.invoke(thisModule, targetModule); 89 } catch (IllegalAccessException | InvocationTargetException ex) { 90 throw new RuntimeException(ex); 91 } 92 } 93 } 94 95 public static void addExports(Object thisModule, String packageName, Object targetModule) { 96 if (addExportsMethod != null) { 97 try { 98 addExportsMethod.invoke(thisModule, packageName, targetModule); 99 } catch (IllegalAccessException | InvocationTargetException ex) { 100 throw new RuntimeException(ex); 101 } 102 } 103 } 104 105 public static InputStream getResourceAsStream(Object thisModule, String name) { 106 if (getResourceAsStreamMethod != null) { 107 try { 108 return (InputStream)getResourceAsStreamMethod.invoke(thisModule, name); 109 } catch (IllegalAccessException | InvocationTargetException ex) { 110 throw new RuntimeException(ex); 111 } 112 } 113 return null; 114 } 115 }