changeset 24110:a8378ff1936d

remove use of cache in jdk.vm.ci.services.Services (GR-3293)
author Doug Simon <doug.simon@oracle.com>
date Mon, 20 Mar 2017 10:46:02 +0100
parents 1e693dba88e4
children 8abcd8e1285d
files jvmci/jdk.vm.ci.services/src/jdk/vm/ci/services/Services.java
diffstat 1 files changed, 17 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/jvmci/jdk.vm.ci.services/src/jdk/vm/ci/services/Services.java	Tue Feb 21 16:57:23 2017 -0800
+++ b/jvmci/jdk.vm.ci.services/src/jdk/vm/ci/services/Services.java	Mon Mar 20 10:46:02 2017 +0100
@@ -22,10 +22,8 @@
  */
 package jdk.vm.ci.services;
 
-import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Formatter;
-import java.util.List;
 import java.util.ServiceLoader;
 
 import sun.reflect.Reflection;
@@ -42,26 +40,24 @@
 
     private static boolean jvmciEnabled = true;
 
-    private static final ClassValue<List<?>> cache = new ClassValue<List<?>>() {
-        @Override
-        protected List<?> computeValue(Class<?> type) {
-            List<Object> impls = new ArrayList<>();
-            if (jvmciEnabled) {
-                try {
-                    for (Object impl : ServiceLoader.load(type, getJVMCIClassLoader())) {
-                        impls.add(impl);
-                    }
-                } catch (InternalError e) {
-                    if (e.getMessage().equals("JVMCI is not enabled")) {
-                        jvmciEnabled = false;
-                    } else {
-                        throw e;
-                    }
+    private static <S> Iterable<S> load0(Class<S> service) {
+        if (jvmciEnabled) {
+            ClassLoader cl = null;
+            try {
+                cl = getJVMCIClassLoader();
+                return ServiceLoader.load(service, cl);
+            } catch (UnsatisfiedLinkError e) {
+                jvmciEnabled = false;
+            } catch (InternalError e) {
+                if (e.getMessage().equals("JVMCI is not enabled")) {
+                    jvmciEnabled = false;
+                } else {
+                    throw e;
                 }
             }
-            return impls;
         }
-    };
+        return Collections.emptyList();
+    }
 
     /**
      * Performs any required security checks and dynamic reconfiguration to allow the module of a
@@ -84,17 +80,12 @@
      * @throws SecurityException if a security manager is present and it denies <tt>
      *             {@link RuntimePermission}("jvmci")</tt>
      */
-    @SuppressWarnings("unchecked")
     public static <S> Iterable<S> load(Class<S> service) {
         SecurityManager sm = System.getSecurityManager();
         if (sm != null) {
             sm.checkPermission(new JVMCIPermission());
         }
-        try {
-            return (Iterable<S>) cache.get(service);
-        } catch (UnsatisfiedLinkError e) {
-            return Collections.emptyList();
-        }
+        return load0(service);
     }
 
     /**
@@ -106,18 +97,12 @@
      * @throws SecurityException if a security manager is present and it denies <tt>
      *             {@link RuntimePermission}("jvmci")</tt>
      */
-    @SuppressWarnings({"unchecked"})
     public static <S> S loadSingle(Class<S> service, boolean required) {
         SecurityManager sm = System.getSecurityManager();
         if (sm != null) {
             sm.checkPermission(new JVMCIPermission());
         }
-        Iterable<S> providers;
-        try {
-            providers = (Iterable<S>) cache.get(service);
-        } catch (UnsatisfiedLinkError e) {
-            providers = Collections.emptyList();
-        }
+        Iterable<S> providers = load0(service);
 
         S singleProvider = null;
         for (S provider : providers) {
@@ -140,7 +125,6 @@
 
     static {
         Reflection.registerMethodsToFilter(Services.class, "getJVMCIClassLoader");
-        Reflection.registerFieldsToFilter(Services.class, "cache");
     }
 
     /**