src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.serviceprovider/src/org/graalvm/compiler/serviceprovider/GraalServices.java
changeset 58299 6df94ce3ab2f
parent 57537 ecc6e394475f
child 58679 9c3209ff7550
equal deleted inserted replaced
58298:0152ad7b38b8 58299:6df94ce3ab2f
    87 
    87 
    88         if (IS_BUILDING_NATIVE_IMAGE) {
    88         if (IS_BUILDING_NATIVE_IMAGE) {
    89             synchronized (servicesCache) {
    89             synchronized (servicesCache) {
    90                 ArrayList<S> providersList = new ArrayList<>();
    90                 ArrayList<S> providersList = new ArrayList<>();
    91                 for (S provider : providers) {
    91                 for (S provider : providers) {
    92                     /*
       
    93                      * When building libgraal, we want providers that comes from the Graal community
       
    94                      * and enterprise modules but not those available on the native-image class
       
    95                      * path.
       
    96                      */
       
    97                     Module module = provider.getClass().getModule();
    92                     Module module = provider.getClass().getModule();
    98                     if (module.isNamed()) {
    93                     if (isHotSpotGraalModule(module.getName())) {
    99                         providersList.add(provider);
    94                         providersList.add(provider);
   100                     }
    95                     }
   101                 }
    96                 }
   102                 providers = providersList;
    97                 providers = providersList;
   103                 servicesCache.put(service, providersList);
    98                 servicesCache.put(service, providersList);
   106         }
   101         }
   107 
   102 
   108         return providers;
   103         return providers;
   109     }
   104     }
   110 
   105 
       
   106     /**
       
   107      * Determines if the module named by {@code name} is part of Graal when it is configured as a
       
   108      * HotSpot JIT compiler.
       
   109      */
       
   110     private static boolean isHotSpotGraalModule(String name) {
       
   111         if (name != null) {
       
   112             return name.equals("jdk.internal.vm.compiler") ||
       
   113                             name.equals("jdk.internal.vm.compiler.management") ||
       
   114                             name.equals("com.oracle.graal.graal_enterprise");
       
   115         }
       
   116         return false;
       
   117     }
       
   118 
   111     protected static <S> Iterable<S> load0(Class<S> service) {
   119     protected static <S> Iterable<S> load0(Class<S> service) {
   112         Iterable<S> iterable = ServiceLoader.load(service);
   120         Module module = GraalServices.class.getModule();
       
   121         // Graal cannot know all the services used by another module
       
   122         // (e.g. enterprise) so dynamically register the service use now.
       
   123         if (!module.canUse(service)) {
       
   124             module.addUses(service);
       
   125         }
       
   126 
       
   127         ModuleLayer layer = module.getLayer();
       
   128         Iterable<S> iterable = ServiceLoader.load(layer, service);
   113         return new Iterable<>() {
   129         return new Iterable<>() {
   114             @Override
   130             @Override
   115             public Iterator<S> iterator() {
   131             public Iterator<S> iterator() {
   116                 Iterator<S> iterator = iterable.iterator();
   132                 Iterator<S> iterator = iterable.iterator();
   117                 return new Iterator<>() {
   133                 return new Iterator<>() {
   218     /**
   234     /**
   219      * Determines if invoking {@link Object#toString()} on an instance of {@code c} will only run
   235      * Determines if invoking {@link Object#toString()} on an instance of {@code c} will only run
   220      * trusted code.
   236      * trusted code.
   221      */
   237      */
   222     public static boolean isToStringTrusted(Class<?> c) {
   238     public static boolean isToStringTrusted(Class<?> c) {
       
   239         if (IS_IN_NATIVE_IMAGE) {
       
   240             return true;
       
   241         }
       
   242 
   223         Module module = c.getModule();
   243         Module module = c.getModule();
   224         Module jvmciModule = JVMCI_MODULE;
   244         Module jvmciModule = JVMCI_MODULE;
   225         assert jvmciModule.getPackages().contains("jdk.vm.ci.runtime");
   245         assert jvmciModule.getPackages().contains("jdk.vm.ci.runtime");
   226         if (module == jvmciModule || jvmciModule.isOpen(JVMCI_RUNTIME_PACKAGE, module)) {
   246         if (module == jvmciModule || jvmciModule.isOpen(JVMCI_RUNTIME_PACKAGE, module)) {
   227             // Can access non-statically-exported package in JVMCI
   247             // Can access non-statically-exported package in JVMCI