8213347: [JVMCI] remove use of reflection in JVMCI
authordnsimon
Wed, 07 Nov 2018 09:39:39 +0100
changeset 52433 a0ea20bc7af0
parent 52432 b43f8976cf81
child 52434 44f34d2c3243
8213347: [JVMCI] remove use of reflection in JVMCI Reviewed-by: kvn
src/hotspot/.mx.jvmci/suite.py
src/jdk.internal.vm.ci/share/classes/jdk.vm.ci.hotspot.aarch64/src/jdk/vm/ci/hotspot/aarch64/AArch64HotSpotVMConfig.java
src/jdk.internal.vm.ci/share/classes/jdk.vm.ci.hotspot.sparc/src/jdk/vm/ci/hotspot/sparc/SPARCHotSpotVMConfig.java
src/jdk.internal.vm.ci/share/classes/jdk.vm.ci.hotspot/src/jdk/vm/ci/hotspot/HotSpotJVMCICompilerConfig.java
src/jdk.internal.vm.ci/share/classes/jdk.vm.ci.hotspot/src/jdk/vm/ci/hotspot/HotSpotResolvedJavaFieldImpl.java
src/jdk.internal.vm.ci/share/classes/jdk.vm.ci.meta/src/jdk/vm/ci/meta/Assumptions.java
src/jdk.internal.vm.ci/share/classes/jdk.vm.ci.services/.checkstyle_checks.xml
src/jdk.internal.vm.ci/share/classes/jdk.vm.ci.services/src/jdk/vm/ci/services/JVMCIServiceLocator.java
src/jdk.internal.vm.ci/share/classes/jdk.vm.ci.services/src/jdk/vm/ci/services/Services.java
src/jdk.internal.vm.ci/share/classes/jdk.vm.ci.services/src/jdk/vm/ci/services/internal/ReflectionAccessJDK.java
--- a/src/hotspot/.mx.jvmci/suite.py	Wed Nov 07 09:13:37 2018 +0100
+++ b/src/hotspot/.mx.jvmci/suite.py	Wed Nov 07 09:39:39 2018 +0100
@@ -43,6 +43,9 @@
     "jdk.vm.ci.services" : {
       "subDir" : "../jdk.internal.vm.ci/share/classes",
       "sourceDirs" : ["src"],
+      "imports" : [
+        "jdk.internal.misc"
+      ],
       "javaCompliance" : "9+",
       "checkstyleVersion" : "8.8",
       "workingSets" : "API,JVMCI",
--- a/src/jdk.internal.vm.ci/share/classes/jdk.vm.ci.hotspot.aarch64/src/jdk/vm/ci/hotspot/aarch64/AArch64HotSpotVMConfig.java	Wed Nov 07 09:13:37 2018 +0100
+++ b/src/jdk.internal.vm.ci/share/classes/jdk.vm.ci.hotspot.aarch64/src/jdk/vm/ci/hotspot/aarch64/AArch64HotSpotVMConfig.java	Wed Nov 07 09:39:39 2018 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -58,6 +58,7 @@
     /*
      * These flags are set if the corresponding support is in the hardware.
      */
+    // Checkstyle: stop
     final long aarch64FP = getConstant("VM_Version::CPU_FP", Long.class);
     final long aarch64ASIMD = getConstant("VM_Version::CPU_ASIMD", Long.class);
     final long aarch64EVTSTRM = getConstant("VM_Version::CPU_EVTSTRM", Long.class);
@@ -70,4 +71,5 @@
     final long aarch64STXR_PREFETCH = getConstant("VM_Version::CPU_STXR_PREFETCH", Long.class);
     final long aarch64A53MAC = getConstant("VM_Version::CPU_A53MAC", Long.class);
     final long aarch64DMB_ATOMICS = getConstant("VM_Version::CPU_DMB_ATOMICS", Long.class);
+    // Checkstyle: resume
 }
--- a/src/jdk.internal.vm.ci/share/classes/jdk.vm.ci.hotspot.sparc/src/jdk/vm/ci/hotspot/sparc/SPARCHotSpotVMConfig.java	Wed Nov 07 09:13:37 2018 +0100
+++ b/src/jdk.internal.vm.ci/share/classes/jdk.vm.ci.hotspot.sparc/src/jdk/vm/ci/hotspot/sparc/SPARCHotSpotVMConfig.java	Wed Nov 07 09:39:39 2018 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -38,12 +38,14 @@
 
     final boolean useCompressedOops = getFlag("UseCompressedOops", Boolean.class);
 
+    // @formatter:off
     // CPU capabilities:
     //
     // FIXME: Using a 64-bit value is insufficient to support future capability
     //        sets (including co-processor capabilities such as DAX).
     final long vmVersionFeatures = getFieldValue("Abstract_VM_Version::_features", Long.class, "uint64_t");
 
+    //
     // SPARC specific values:
     //
     // NOTE: Values changed into an enumeration (that do indeed fit within a
@@ -97,4 +99,5 @@
 
     final boolean useBlockZeroing = getFlag("UseBlockZeroing", Boolean.class);
     final int blockZeroingLowLimit = getFlag("BlockZeroingLowLimit", Integer.class);
+    // @formatter:on
 }
--- a/src/jdk.internal.vm.ci/share/classes/jdk.vm.ci.hotspot/src/jdk/vm/ci/hotspot/HotSpotJVMCICompilerConfig.java	Wed Nov 07 09:13:37 2018 +0100
+++ b/src/jdk.internal.vm.ci/share/classes/jdk.vm.ci.hotspot/src/jdk/vm/ci/hotspot/HotSpotJVMCICompilerConfig.java	Wed Nov 07 09:39:39 2018 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -22,6 +22,8 @@
  */
 package jdk.vm.ci.hotspot;
 
+import java.util.Set;
+
 import jdk.vm.ci.code.CompilationRequest;
 import jdk.vm.ci.common.JVMCIError;
 import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime.Option;
@@ -30,7 +32,6 @@
 import jdk.vm.ci.runtime.JVMCIRuntime;
 import jdk.vm.ci.services.JVMCIPermission;
 import jdk.vm.ci.services.JVMCIServiceLocator;
-import jdk.vm.ci.services.internal.ReflectionAccessJDK;
 
 final class HotSpotJVMCICompilerConfig {
 
@@ -90,7 +91,7 @@
                 // Auto select a single available compiler
                 for (JVMCICompilerFactory f : JVMCIServiceLocator.getProviders(JVMCICompilerFactory.class)) {
                     if (factory == null) {
-                        ReflectionAccessJDK.openJVMCITo(f.getClass());
+                        openJVMCITo(f.getClass().getModule());
                         factory = f;
                     } else {
                         // Multiple factories seen - cancel auto selection
@@ -107,4 +108,20 @@
         }
         return compilerFactory;
     }
+
+    /**
+     * Opens all JVMCI packages to {@code otherModule}.
+     */
+    private static void openJVMCITo(Module otherModule) {
+        Module jvmci = HotSpotJVMCICompilerConfig.class.getModule();
+        if (jvmci != otherModule) {
+            Set<String> packages = jvmci.getPackages();
+            for (String pkg : packages) {
+                boolean opened = jvmci.isOpen(pkg, otherModule);
+                if (!opened) {
+                    jvmci.addOpens(pkg, otherModule);
+                }
+            }
+        }
+    }
 }
--- a/src/jdk.internal.vm.ci/share/classes/jdk.vm.ci.hotspot/src/jdk/vm/ci/hotspot/HotSpotResolvedJavaFieldImpl.java	Wed Nov 07 09:13:37 2018 +0100
+++ b/src/jdk.internal.vm.ci/share/classes/jdk.vm.ci.hotspot/src/jdk/vm/ci/hotspot/HotSpotResolvedJavaFieldImpl.java	Wed Nov 07 09:39:39 2018 +0100
@@ -147,9 +147,9 @@
     }
 
     /**
-     * Checks if this field has the {@link Stable} annotation.
+     * Checks if this field has the {@code Stable} annotation.
      *
-     * @return true if field has {@link Stable} annotation, false otherwise
+     * @return true if field has {@code Stable} annotation, false otherwise
      */
     @Override
     public boolean isStable() {
--- a/src/jdk.internal.vm.ci/share/classes/jdk.vm.ci.meta/src/jdk/vm/ci/meta/Assumptions.java	Wed Nov 07 09:13:37 2018 +0100
+++ b/src/jdk.internal.vm.ci/share/classes/jdk.vm.ci.meta/src/jdk/vm/ci/meta/Assumptions.java	Wed Nov 07 09:39:39 2018 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -98,7 +98,7 @@
     }
 
     /**
-     * An assumption that a given class has no subclasses implementing {@link Object#finalize()}).
+     * An assumption that a given class has no subclasses implementing {@code Object#finalize()}).
      */
     public static final class NoFinalizableSubclass extends Assumption {
 
--- a/src/jdk.internal.vm.ci/share/classes/jdk.vm.ci.services/.checkstyle_checks.xml	Wed Nov 07 09:13:37 2018 +0100
+++ b/src/jdk.internal.vm.ci/share/classes/jdk.vm.ci.services/.checkstyle_checks.xml	Wed Nov 07 09:39:39 2018 +0100
@@ -191,6 +191,16 @@
       <property name="onCommentFormat" value="CheckStyle: stop generated"/>
       <property name="checkFormat" value=".*Name|.*LineLength|.*Header"/>
     </module>
+    <module name="SuppressionCommentFilter">
+      <property name="offCommentFormat" value="Checkstyle: stop"/>
+      <property name="onCommentFormat" value="Checkstyle: resume"/>
+      <property name="checkFormat" value=".*"/>
+    </module>
+    <module name="SuppressionCommentFilter">
+      <property name="offCommentFormat" value="@formatter:off"/>
+      <property name="onCommentFormat" value="@formatter:on"/>
+      <property name="checkFormat" value=".*"/>
+    </module>
   </module>
   <module name="RegexpHeader">
     <property name="header" value="/\*\n \* Copyright \(c\) (20[0-9][0-9], )?20[0-9][0-9], Oracle and/or its affiliates. All rights reserved.\n \* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.\n \*\n \* This code is free software; you can redistribute it and/or modify it\n \* under the terms of the GNU General Public License version 2 only, as\n \* published by the Free Software Foundation.\n \*\n \* This code is distributed in the hope that it will be useful, but WITHOUT\n \* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\n \* FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License\n \* version 2 for more details \(a copy is included in the LICENSE file that\n \* accompanied this code\).\n \*\n \* You should have received a copy of the GNU General Public License version\n \* 2 along with this work; if not, write to the Free Software Foundation,\n \* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.\n \*\n \* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA\n \* or visit www.oracle.com if you need additional information or have any\n \* questions.\n \*/\n"/>
--- a/src/jdk.internal.vm.ci/share/classes/jdk.vm.ci.services/src/jdk/vm/ci/services/JVMCIServiceLocator.java	Wed Nov 07 09:13:37 2018 +0100
+++ b/src/jdk.internal.vm.ci/share/classes/jdk.vm.ci.services/src/jdk/vm/ci/services/JVMCIServiceLocator.java	Wed Nov 07 09:39:39 2018 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -26,8 +26,6 @@
 import java.util.List;
 import java.util.ServiceLoader;
 
-import jdk.vm.ci.services.internal.ReflectionAccessJDK;
-
 /**
  * Service-provider class for the runtime to locate providers of JVMCI services where the latter are
  * not in packages exported by the JVMCI module. As part of instantiating a
@@ -61,7 +59,7 @@
     protected JVMCIServiceLocator() {
         this(checkPermission());
         Services.checkJVMCIEnabled();
-        ReflectionAccessJDK.openJVMCITo(getClass());
+        Services.openJVMCITo(getClass().getModule());
     }
 
     /**
--- a/src/jdk.internal.vm.ci/share/classes/jdk.vm.ci.services/src/jdk/vm/ci/services/Services.java	Wed Nov 07 09:13:37 2018 +0100
+++ b/src/jdk.internal.vm.ci/share/classes/jdk.vm.ci.services/src/jdk/vm/ci/services/Services.java	Wed Nov 07 09:39:39 2018 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -22,8 +22,10 @@
  */
 package jdk.vm.ci.services;
 
-import java.lang.reflect.Method;
 import java.util.Map;
+import java.util.Set;
+
+import jdk.internal.misc.VM;
 
 /**
  * Provides utilities needed by JVMCI clients.
@@ -61,18 +63,7 @@
     private Services() {
     }
 
-    @SuppressWarnings("unchecked")
-    private static Map<String, String> initSavedProperties() throws InternalError {
-        try {
-            Class<?> vmClass = Class.forName("jdk.internal.misc.VM");
-            Method m = vmClass.getMethod("getSavedProperties");
-            return (Map<String, String>) m.invoke(null);
-        } catch (Exception e) {
-            throw new InternalError(e);
-        }
-    }
-
-    static final Map<String, String> SAVED_PROPERTIES = initSavedProperties();
+    static final Map<String, String> SAVED_PROPERTIES = VM.getSavedProperties();
     static final boolean JVMCI_ENABLED = Boolean.parseBoolean(SAVED_PROPERTIES.get("jdk.internal.vm.ci.enabled"));
 
     /**
@@ -107,4 +98,20 @@
             throw new InternalError(e);
         }
     }
+
+    /**
+     * Opens all JVMCI packages to {@code otherModule}.
+     */
+    static void openJVMCITo(Module otherModule) {
+        Module jvmci = Services.class.getModule();
+        if (jvmci != otherModule) {
+            Set<String> packages = jvmci.getPackages();
+            for (String pkg : packages) {
+                boolean opened = jvmci.isOpen(pkg, otherModule);
+                if (!opened) {
+                    jvmci.addOpens(pkg, otherModule);
+                }
+            }
+        }
+    }
 }
--- a/src/jdk.internal.vm.ci/share/classes/jdk.vm.ci.services/src/jdk/vm/ci/services/internal/ReflectionAccessJDK.java	Wed Nov 07 09:13:37 2018 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,91 +0,0 @@
-/*
- * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
- * This code is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 only, as
- * published by the Free Software Foundation.
- *
- * This code is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
- * version 2 for more details (a copy is included in the LICENSE file that
- * accompanied this code).
- *
- * You should have received a copy of the GNU General Public License version
- * 2 along with this work; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
- * or visit www.oracle.com if you need additional information or have any
- * questions.
- */
-package jdk.vm.ci.services.internal;
-
-import java.lang.reflect.Method;
-import java.util.Set;
-
-import jdk.vm.ci.services.Services;
-
-/**
- * Reflection based access to API introduced in JDK 9. This allows the API to be used in code that
- * must be compiled (but not executed) on JDK 8.
- */
-public final class ReflectionAccessJDK {
-
-    /**
-     * {@code Class.getModule()}.
-     */
-    private static final Method getModule;
-
-    /**
-     * {@code java.lang.Module.addOpens(String, Module)}.
-     */
-    private static final Method addOpens;
-
-    /**
-     * {@code java.lang.Module.getPackages(Module, String, Module)}.
-     */
-    private static final Method getPackages;
-
-    /**
-     * {@code java.lang.Module.isOpen(String, Module)}.
-     */
-    private static final Method isOpenTo;
-
-    /**
-     * Opens all JVMCI packages to the module of a given class.
-     *
-     * @param other all JVMCI packages will be opened to the module of this class
-     */
-    @SuppressWarnings("unchecked")
-    public static void openJVMCITo(Class<?> other) {
-        try {
-            Object jvmci = getModule.invoke(Services.class);
-            Object otherModule = getModule.invoke(other);
-            if (jvmci != otherModule) {
-                Set<String> packages = (Set<String>) getPackages.invoke(jvmci);
-                for (String pkg : packages) {
-                    boolean opened = (Boolean) isOpenTo.invoke(jvmci, pkg, otherModule);
-                    if (!opened) {
-                        addOpens.invoke(jvmci, pkg, otherModule);
-                    }
-                }
-            }
-        } catch (Exception e) {
-            throw new InternalError(e);
-        }
-    }
-
-    static {
-        try {
-            getModule = Class.class.getMethod("getModule");
-            Class<?> moduleClass = getModule.getReturnType();
-            getPackages = moduleClass.getMethod("getPackages");
-            isOpenTo = moduleClass.getMethod("isOpen", String.class, moduleClass);
-            addOpens = moduleClass.getDeclaredMethod("addOpens", String.class, moduleClass);
-        } catch (NoSuchMethodException | SecurityException e) {
-            throw new InternalError(e);
-        }
-    }
-}