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