1 /* 2 * Copyright (c) 2017, 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. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 package jdk.vm.ci.services.internal; 24 25 import java.lang.reflect.Method; 26 import java.util.Set; 27 28 import jdk.vm.ci.services.Services; 29 30 /** 31 * Reflection based access to API introduced in JDK 9. This allows the API to be used in code that 32 * must be compiled (but not executed) on JDK 8. 33 */ 34 public final class ReflectionAccessJDK { 35 36 /** 37 * {@code Class.getModule()}. 38 */ 39 private static final Method getModule; 40 41 /** 42 * {@code java.lang.Module.addOpens(String, Module)}. 43 */ 44 private static final Method addOpens; 45 46 /** 47 * {@code java.lang.Module.getPackages(Module, String, Module)}. 48 */ 49 private static final Method getPackages; 50 51 /** 52 * {@code java.lang.Module.isOpen(String, Module)}. 53 */ 54 private static final Method isOpenTo; 55 56 /** 57 * Opens all JVMCI packages to the module of a given class. 58 * 59 * @param other all JVMCI packages will be opened to the module of this class 60 */ 61 @SuppressWarnings("unchecked") 62 public static void openJVMCITo(Class<?> other) { 63 try { 64 Object jvmci = getModule.invoke(Services.class); 65 Object otherModule = getModule.invoke(other); 66 if (jvmci != otherModule) { 67 Set<String> packages = (Set<String>) getPackages.invoke(jvmci); 68 for (String pkg : packages) { 69 boolean opened = (Boolean) isOpenTo.invoke(jvmci, pkg, otherModule); 70 if (!opened) { 71 addOpens.invoke(jvmci, pkg, otherModule); 72 } 73 } 74 } 75 } catch (Exception e) { 76 throw new InternalError(e); 77 } 78 } 79 80 static { 81 try { 82 getModule = Class.class.getMethod("getModule"); 83 Class<?> moduleClass = getModule.getReturnType(); 84 getPackages = moduleClass.getMethod("getPackages"); 85 isOpenTo = moduleClass.getMethod("isOpen", String.class, moduleClass); 86 addOpens = moduleClass.getDeclaredMethod("addOpens", String.class, moduleClass); 87 } catch (NoSuchMethodException | SecurityException e) { 88 throw new InternalError(e); 89 } 90 } 91 }