Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[#8890] ServiceLoader does not work properly in OpenJ9 #8903

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ public String getService() {
return services;
}

public String getServicePackage() {
int lastDotIndex = services.lastIndexOf('.');
if (lastDotIndex == -1) {
return services;
}
return services.substring(0, lastDotIndex);
}

public List<String> getProviders() {
return providers;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,21 @@
import jdk.internal.loader.BootLoader;
import jdk.internal.module.Modules;

import java.lang.module.Configuration;
import java.lang.module.ModuleDescriptor;
import java.lang.module.ModuleFinder;
import java.net.URI;
import java.util.List;
import java.util.Objects;
import java.util.Set;

final class InternalModules {

private InternalModules() {
}

/**
* Returns the unnamed module for the boot loader.
* Returns the unnamed module for the bootloader.
*/
static Module getUnnamedModule() {
return BootLoader.getUnnamedModule();
Expand All @@ -47,14 +52,24 @@ static Module defineModule(ClassLoader loader,
ModuleDescriptor descriptor,
URI uri)
{
return Modules.defineModule(loader, descriptor, uri);
final String moduleName = descriptor.name();
final ModuleLayer parent = ModuleLayer.boot();

final ModuleFinder before = new SingleModuleFinder(descriptor, uri);
final Configuration cf = parent.configuration().resolve(before, ModuleFinder.of(), Set.of(moduleName));
final Module module = ModuleLayer.defineModules(cf, List.of(parent), name -> loader)
.layer()
.findModule(moduleName)
.orElse(null);

return Objects.requireNonNull(module, moduleName);
}


/**
* Called by the VM to load a system module, typically "java.instrument" or
* "jdk.management.agent". If the module is not loaded then it is resolved
* and loaded (along with any dependences that weren't previously loaded)
* and loaded (along with any dependencies that weren't previously loaded)
* into a child layer.
*/
static Module loadModule(String name) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2022 NAVER Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.navercorp.pinpoint.bootstrap.java9.module;

import java.lang.module.ModuleDescriptor;
import java.lang.module.ModuleFinder;
import java.lang.module.ModuleReader;
import java.lang.module.ModuleReference;
import java.net.URI;
import java.util.Optional;
import java.util.Set;

/**
* @author youngjin.kim2
*/
public class SingleModuleFinder implements ModuleFinder {
private final ModuleReference target;

public SingleModuleFinder(ModuleDescriptor descriptor, URI uri) {
this.target = new ModuleReference(descriptor, uri) {
@Override
public ModuleReader open() {
throw new RuntimeException("open must not be called");
}
};
}

@Override
public Optional<ModuleReference> find(String name) {
if (target.descriptor().name().equals(name)) {
return Optional.of(target);
}
return Optional.empty();
}

@Override
public Set<ModuleReference> findAll() {
return Set.of(target);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public PackageInfo analyze() {

final Providers provides = this.serviceLoaderEntryFilter.filter(jarEntry);
if (provides != null) {
packageSet.add(provides.getServicePackage());
providesList.add(provides);
}
}
Expand Down Expand Up @@ -124,7 +125,7 @@ public String filter(JarEntry jarEntry) {

final String fileName = jarEntry.getName();
if (!checkFIleExtension(fileName, CLASS_EXTENSION)) {
// skip non class file
// skip non-class file
return null;
}

Expand Down