1 /* 2 * Copyright (c) 2016, 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 org.graalvm.compiler.test; 24 25 import java.lang.reflect.Method; 26 27 /** 28 * Facade for the {@code java.lang.reflect.Module} class introduced in JDK9 that allows tests to be 29 * developed against JDK8 but use module logic if deployed on JDK9. 30 */ 31 public class JLRModule { 32 33 static { 34 if (GraalTest.Java8OrEarlier) { 35 throw new AssertionError("Use of " + JLRModule.class + " only allowed if " + GraalTest.class.getName() + ".JDK8OrEarlier is false"); 36 } 37 } 38 39 private final Object realModule; 40 41 public JLRModule(Object module) { 42 this.realModule = module; 43 } 44 45 private static final Class<?> moduleClass; 46 private static final Method getModuleMethod; 47 private static final Method getUnnamedModuleMethod; 48 private static final Method getPackagesMethod; 49 private static final Method isExportedMethod; 50 private static final Method isExported2Method; 51 private static final Method addExportsMethod; 52 static { 53 try { 54 moduleClass = Class.forName("java.lang.reflect.Module"); 55 getModuleMethod = Class.class.getMethod("getModule"); 56 getUnnamedModuleMethod = ClassLoader.class.getMethod("getUnnamedModule"); 57 getPackagesMethod = moduleClass.getMethod("getPackages"); 58 isExportedMethod = moduleClass.getMethod("isExported", String.class); 59 isExported2Method = moduleClass.getMethod("isExported", String.class, moduleClass); 60 addExportsMethod = moduleClass.getMethod("addExports", String.class, moduleClass); 61 } catch (Exception e) { 62 throw new AssertionError(e); 63 } 64 } 65 66 public static JLRModule fromClass(Class<?> cls) { 67 try { 68 return new JLRModule(getModuleMethod.invoke(cls)); 69 } catch (Exception e) { 70 throw new AssertionError(e); 71 } 72 } 73 74 public static JLRModule getUnnamedModuleFor(ClassLoader cl) { 75 try { 76 return new JLRModule(getUnnamedModuleMethod.invoke(cl)); 77 } catch (Exception e) { 78 throw new AssertionError(e); 79 } 80 } 81 82 /** 83 * Exports all packages in this module to a given module. 84 */ 85 public void exportAllPackagesTo(JLRModule module) { 86 if (this != module) { 87 for (String pkg : getPackages()) { 88 // Export all JVMCI packages dynamically instead 89 // of requiring a long list of -XaddExports 90 // options on the JVM command line. 91 if (!isExported(pkg, module)) { 92 addExports(pkg, module); 93 } 94 } 95 } 96 } 97 98 public String[] getPackages() { 99 try { 100 return (String[]) getPackagesMethod.invoke(realModule); 101 } catch (Exception e) { 102 throw new AssertionError(e); 103 } 104 } 105 106 public boolean isExported(String pn) { 107 try { 108 return (Boolean) isExportedMethod.invoke(realModule, pn); 109 } catch (Exception e) { 110 throw new AssertionError(e); 111 } 112 } 113 114 public boolean isExported(String pn, JLRModule other) { 115 try { 116 return (Boolean) isExported2Method.invoke(realModule, pn, other.realModule); 117 } catch (Exception e) { 118 throw new AssertionError(e); 119 } 120 } 121 122 public void addExports(String pn, JLRModule other) { 123 try { 124 addExportsMethod.invoke(realModule, pn, other.realModule); 125 } catch (Exception e) { 126 throw new AssertionError(e); 127 } 128 } 129 }