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.core.common.util;
  24 
  25 import static org.graalvm.compiler.core.common.util.Util.JAVA_SPECIFICATION_VERSION;
  26 
  27 import java.lang.reflect.InvocationTargetException;
  28 import java.lang.reflect.Method;
  29 import java.lang.reflect.Modifier;
  30 
  31 /**
  32  * Reflection based access to the Module API introduced by JDK 9. This allows the API to be used in
  33  * code that must be compiled on a JDK prior to 9. Use of this class must be guarded by a test for
  34  * JDK 9 or later. For example:
  35  *
  36  * <pre>
  37  * if (Util.JAVA_SPECIFICATION_VERSION >= 9) {
  38  *     // Use of ModuleAPI
  39  * }
  40  * </pre>
  41  */
  42 public final class ModuleAPI {
  43 
  44     private ModuleAPI(Method method) {
  45         this.method = method;
  46     }
  47 
  48     private final Method method;
  49 
  50     /**
  51      * {@code Class.getModule()}.
  52      */
  53     public static final ModuleAPI getModule;
  54 
  55     /**
  56      * {@code jdk.internal.module.Modules.addExports(Module, String, Module)}.
  57      */
  58     public static final ModuleAPI addExports;
  59 
  60     /**
  61      * {@code java.lang.reflect.Module.getResourceAsStream(String)}.
  62      */
  63     public static final ModuleAPI getResourceAsStream;
  64 
  65     /**
  66      * {@code java.lang.reflect.Module.canRead(Module)}.
  67      */
  68     public static final ModuleAPI canRead;
  69 
  70     /**
  71      * {@code java.lang.reflect.Module.isExported(String)}.
  72      */
  73     public static final ModuleAPI isExported;
  74 
  75     /**
  76      * {@code java.lang.reflect.Module.isExported(String, Module)}.
  77      */
  78     public static final ModuleAPI isExportedTo;
  79 
  80     /**
  81      * Invokes the static Module API method represented by this object.
  82      */
  83     @SuppressWarnings("unchecked")
  84     public <T> T invokeStatic(Object... args) {
  85         checkAvailability();
  86         assert Modifier.isStatic(method.getModifiers());
  87         try {
  88             return (T) method.invoke(null, args);
  89         } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
  90             throw new InternalError(e);
  91         }
  92     }
  93 
  94     /**
  95      * Invokes the non-static Module API method represented by this object.
  96      */
  97     @SuppressWarnings("unchecked")
  98     public <T> T invoke(Object receiver, Object... args) {
  99         checkAvailability();
 100         assert !Modifier.isStatic(method.getModifiers());
 101         try {
 102             return (T) method.invoke(receiver, args);
 103         } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
 104             throw new InternalError(e);
 105         }
 106     }
 107 
 108     private void checkAvailability() throws InternalError {
 109         if (method == null) {
 110             throw new InternalError("Cannot use Module API on JDK " + JAVA_SPECIFICATION_VERSION);
 111         }
 112     }
 113 
 114     static {
 115         if (JAVA_SPECIFICATION_VERSION >= 9) {
 116             try {
 117                 getModule = new ModuleAPI(Class.class.getMethod("getModule"));
 118                 Class<?> moduleClass = getModule.method.getReturnType();
 119                 Class<?> modulesClass = Class.forName("jdk.internal.module.Modules");
 120                 getResourceAsStream = new ModuleAPI(moduleClass.getMethod("getResourceAsStream", String.class));
 121                 canRead = new ModuleAPI(moduleClass.getMethod("canRead", moduleClass));
 122                 isExported = new ModuleAPI(moduleClass.getMethod("isExported", String.class));
 123                 isExportedTo = new ModuleAPI(moduleClass.getMethod("isExported", String.class, moduleClass));
 124                 addExports = new ModuleAPI(modulesClass.getDeclaredMethod("addExports", moduleClass, String.class, moduleClass));
 125             } catch (NoSuchMethodException | SecurityException | ClassNotFoundException e) {
 126                 throw new InternalError(e);
 127             }
 128         } else {
 129             ModuleAPI unavailable = new ModuleAPI(null);
 130             getModule = unavailable;
 131             getResourceAsStream = unavailable;
 132             canRead = unavailable;
 133             isExported = unavailable;
 134             isExportedTo = unavailable;
 135             addExports = unavailable;
 136         }
 137 
 138     }
 139 }