test/hotspot/jtreg/compiler/graalunit/com.oracle.mxtool.junit/com/oracle/mxtool/junit/JLModule.java
branchstuefe-new-metaspace-branch
changeset 58645 28c7e6711871
parent 58494 54c1ba464b78
parent 58644 64597a6fd186
child 58646 bcdba1c9f1fe
equal deleted inserted replaced
58494:54c1ba464b78 58645:28c7e6711871
     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 com.oracle.mxtool.junit;
       
    24 
       
    25 import java.lang.reflect.Method;
       
    26 import java.util.Arrays;
       
    27 import java.util.Set;
       
    28 
       
    29 /**
       
    30  * Facade for the {@code java.lang.Module} class introduced in JDK9 that allows tests to be
       
    31  * developed against JDK8 but use module logic if deployed on JDK9.
       
    32  */
       
    33 class JLModule {
       
    34 
       
    35     private final Object realModule;
       
    36 
       
    37     JLModule(Object module) {
       
    38         this.realModule = module;
       
    39     }
       
    40 
       
    41     private static final Class<?> moduleClass;
       
    42     private static final Class<?> layerClass;
       
    43 
       
    44     private static final Method bootMethod;
       
    45     private static final Method modulesMethod;
       
    46     private static final Method getModuleMethod;
       
    47     private static final Method getUnnamedModuleMethod;
       
    48     private static final Method getNameMethod;
       
    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     private static final Method addOpensMethod;
       
    54     static {
       
    55         try {
       
    56             moduleClass = findModuleClass();
       
    57             Class<?> modulesClass = Class.forName("jdk.internal.module.Modules");
       
    58             layerClass = findModuleLayerClass();
       
    59             bootMethod = layerClass.getMethod("boot");
       
    60             modulesMethod = layerClass.getMethod("modules");
       
    61             getModuleMethod = Class.class.getMethod("getModule");
       
    62             getUnnamedModuleMethod = ClassLoader.class.getMethod("getUnnamedModule");
       
    63             getNameMethod = moduleClass.getMethod("getName");
       
    64             getPackagesMethod = moduleClass.getMethod("getPackages");
       
    65             isExportedMethod = moduleClass.getMethod("isExported", String.class);
       
    66             isExported2Method = moduleClass.getMethod("isExported", String.class, moduleClass);
       
    67             addExportsMethod = modulesClass.getDeclaredMethod("addExports", moduleClass, String.class, moduleClass);
       
    68             addOpensMethod = getDeclaredMethodOptional(modulesClass, "addOpens", moduleClass, String.class, moduleClass);
       
    69         } catch (Exception e) {
       
    70             throw new AssertionError(e);
       
    71         }
       
    72     }
       
    73 
       
    74     // API change http://hg.openjdk.java.net/jdk9/dev/hotspot/rev/afedee84773e.
       
    75     protected static Class<?> findModuleClass() throws ClassNotFoundException {
       
    76         try {
       
    77             return Class.forName("java.lang.Module");
       
    78         } catch (ClassNotFoundException e) {
       
    79             return Class.forName("java.lang.reflect.Module");
       
    80         }
       
    81     }
       
    82 
       
    83     // API change http://hg.openjdk.java.net/jdk9/dev/hotspot/rev/afedee84773e.
       
    84     protected static Class<?> findModuleLayerClass() throws ClassNotFoundException {
       
    85         try {
       
    86             return Class.forName("java.lang.ModuleLayer");
       
    87         } catch (ClassNotFoundException e) {
       
    88             return Class.forName("java.lang.reflect.Layer");
       
    89         }
       
    90     }
       
    91 
       
    92     private static Method getDeclaredMethodOptional(Class<?> declaringClass, String name, Class<?>... parameterTypes) {
       
    93         try {
       
    94             return declaringClass.getDeclaredMethod(name, parameterTypes);
       
    95         } catch (NoSuchMethodException e) {
       
    96             return null;
       
    97         }
       
    98     }
       
    99 
       
   100     public static JLModule fromClass(Class<?> cls) {
       
   101         try {
       
   102             return new JLModule(getModuleMethod.invoke(cls));
       
   103         } catch (Exception e) {
       
   104             throw new AssertionError(e);
       
   105         }
       
   106     }
       
   107 
       
   108     @SuppressWarnings("unchecked")
       
   109     public static JLModule find(String name) {
       
   110         try {
       
   111             Object bootLayer = bootMethod.invoke(null);
       
   112             Set<Object> modules = (Set<Object>) modulesMethod.invoke(bootLayer);
       
   113             for (Object m : modules) {
       
   114                 JLModule module = new JLModule(m);
       
   115                 String mname = module.getName();
       
   116                 if (mname.equals(name)) {
       
   117                     return module;
       
   118                 }
       
   119             }
       
   120         } catch (Exception e) {
       
   121             throw new InternalError(e);
       
   122         }
       
   123         return null;
       
   124     }
       
   125 
       
   126     public static JLModule getUnnamedModuleFor(ClassLoader cl) {
       
   127         try {
       
   128             return new JLModule(getUnnamedModuleMethod.invoke(cl));
       
   129         } catch (Exception e) {
       
   130             throw new AssertionError(e);
       
   131         }
       
   132     }
       
   133 
       
   134     public String getName() {
       
   135         try {
       
   136             return (String) getNameMethod.invoke(realModule);
       
   137         } catch (Exception e) {
       
   138             throw new AssertionError(e);
       
   139         }
       
   140     }
       
   141 
       
   142     /**
       
   143      * Exports all packages in this module to a given module.
       
   144      */
       
   145     public void exportAllPackagesTo(JLModule module) {
       
   146         if (this != module) {
       
   147             for (String pkg : getPackages()) {
       
   148                 // Export all JVMCI packages dynamically instead
       
   149                 // of requiring a long list of -XaddExports
       
   150                 // options on the JVM command line.
       
   151                 if (!isExported(pkg, module)) {
       
   152                     addExports(pkg, module);
       
   153                     addOpens(pkg, module);
       
   154                 }
       
   155             }
       
   156         }
       
   157     }
       
   158 
       
   159     @SuppressWarnings("unchecked")
       
   160     public Iterable<String> getPackages() {
       
   161         try {
       
   162             // API change http://hg.openjdk.java.net/jdk9/dev/hotspot/rev/afedee84773e#l1.15
       
   163             Object res = getPackagesMethod.invoke(realModule);
       
   164             if (res instanceof String[]) {
       
   165                 return Arrays.asList((String[]) res);
       
   166             }
       
   167             return (Set<String>) res;
       
   168         } catch (Exception e) {
       
   169             throw new AssertionError(e);
       
   170         }
       
   171     }
       
   172 
       
   173     public boolean isExported(String pn) {
       
   174         try {
       
   175             return (Boolean) isExportedMethod.invoke(realModule, pn);
       
   176         } catch (Exception e) {
       
   177             throw new AssertionError(e);
       
   178         }
       
   179     }
       
   180 
       
   181     public boolean isExported(String pn, JLModule other) {
       
   182         try {
       
   183             return (Boolean) isExported2Method.invoke(realModule, pn, other.realModule);
       
   184         } catch (Exception e) {
       
   185             throw new AssertionError(e);
       
   186         }
       
   187     }
       
   188 
       
   189     public void addExports(String pn, JLModule other) {
       
   190         try {
       
   191             addExportsMethod.invoke(null, realModule, pn, other.realModule);
       
   192         } catch (Exception e) {
       
   193             throw new AssertionError(e);
       
   194         }
       
   195     }
       
   196 
       
   197     public void addOpens(String pn, JLModule other) {
       
   198         if (addOpensMethod != null) {
       
   199             try {
       
   200                 addOpensMethod.invoke(null, realModule, pn, other.realModule);
       
   201             } catch (Exception e) {
       
   202                 throw new AssertionError(e);
       
   203             }
       
   204         }
       
   205     }
       
   206 
       
   207     @Override
       
   208     public String toString() {
       
   209         return realModule.toString();
       
   210     }
       
   211 }