Skip to content

Commit

Permalink
Add ServiceLoader interface NativeImageClassLoaderPostProcessing
Browse files Browse the repository at this point in the history
and use it for native-image classloader options processing.
  • Loading branch information
olpaw committed Nov 9, 2021
1 parent 6a8f597 commit 5fb53fa
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 7 deletions.
2 changes: 1 addition & 1 deletion substratevm/mx.substratevm/suite.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# pylint: disable=line-too-long
suite = {
"mxversion": "5.310.0",
"name": "substratevm",
Expand Down Expand Up @@ -1117,6 +1116,7 @@
"com.oracle.truffle.api.TruffleLanguage.Provider",
"com.oracle.truffle.api.instrumentation.TruffleInstrument.Provider",
"com.oracle.svm.hosted.agent.NativeImageBytecodeInstrumentationAgentExtension",
"com.oracle.svm.hosted.NativeImageClassLoaderPostProcessing",
],
"requiresConcealed": {
"jdk.internal.vm.ci": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,9 @@ protected Optional<Module> findModule(String moduleName) {
}

@Override
protected void processClassLoaderOptions(OptionValues optionValues) {
protected void processClassLoaderOptions() {
OptionValues optionValues = getParsedHostedOptions();

processOption(optionValues, NativeImageClassLoaderOptions.AddExports).forEach(val -> {
if (val.targetModules.isEmpty()) {
Modules.addExportsToAllUnnamed(val.module, val.packageName);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.oracle.svm.hosted.NativeImageClassLoaderOptions$ApplyNativeImageClassLoaderOptions
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ public void setupHostedOptionParser(List<String> arguments) {
hostedOptionParser = new HostedOptionParser(getClassLoader());
remainingArguments = Collections.unmodifiableList((hostedOptionParser.parse(arguments)));
parsedHostedOptions = new OptionValues(hostedOptionParser.getHostedValues());
processClassLoaderOptions(parsedHostedOptions);
}

public HostedOptionParser getHostedOptionParser() {
Expand All @@ -133,7 +132,7 @@ public OptionValues getParsedHostedOptions() {
return parsedHostedOptions;
}

protected abstract void processClassLoaderOptions(OptionValues optionValues);
protected abstract void processClassLoaderOptions();

public abstract void propagateQualifiedExports(String fromTargetModule, String toTargetModule);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,11 @@ public class NativeImageClassLoaderOptions {
@Option(help = "Value " + AddReadsFormat + " updates <module> to read <target-module>, regardless of module declaration." +
" <target-module> can be ALL-UNNAMED to read all unnamed modules.")//
public static final HostedOptionKey<LocatableMultiOptionValue.Strings> AddReads = new HostedOptionKey<>(new LocatableMultiOptionValue.Strings());

public static class ApplyNativeImageClassLoaderOptions implements NativeImageClassLoaderPostProcessing {
@Override
public void apply(AbstractNativeImageClassLoaderSupport support) {
support.processClassLoaderOptions();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2021, 2021, 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. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* 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 com.oracle.svm.hosted;

/**
* ServiceLoader interface to allow post-processing tasks that should be performed right after
* {@link AbstractNativeImageClassLoaderSupport} is created. For example, this is used to apply the
* native-image classloader options after hosted options are accessible but before
* {@com.oracle.svm.hosted.ImageClassLoader#initAllClasses()} gets called.
*/
public interface NativeImageClassLoaderPostProcessing {

void apply(AbstractNativeImageClassLoaderSupport support);

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
import java.util.Optional;
import java.util.concurrent.ForkJoinPool;

import org.graalvm.compiler.options.OptionValues;

public class NativeImageClassLoaderSupport extends AbstractNativeImageClassLoaderSupport {

NativeImageClassLoaderSupport(ClassLoader defaultSystemClassLoader, String[] classpath, @SuppressWarnings("unused") String[] modulePath) {
Expand All @@ -54,7 +52,7 @@ protected Optional<Object> findModule(String moduleName) {
}

@Override
protected void processClassLoaderOptions(OptionValues optionValues) {
protected void processClassLoaderOptions() {
/* Nothing to do for Java 8 */
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.ServiceLoader;
import java.util.TimerTask;
import java.util.concurrent.ForkJoinPool;
import java.util.function.Consumer;
Expand Down Expand Up @@ -177,6 +178,10 @@ public static ImageClassLoader installNativeImageClassLoader(String[] classpath,
NativeImageSystemClassLoader nativeImageSystemClassLoader = NativeImageSystemClassLoader.singleton();
AbstractNativeImageClassLoaderSupport nativeImageClassLoaderSupport = createNativeImageClassLoaderSupport(nativeImageSystemClassLoader.defaultSystemClassLoader, classpath, modulepath);
nativeImageClassLoaderSupport.setupHostedOptionParser(arguments);
/* Perform additional post-processing with the created nativeImageClassLoaderSupport */
for (NativeImageClassLoaderPostProcessing postProcessing : ServiceLoader.load(NativeImageClassLoaderPostProcessing.class)) {
postProcessing.apply(nativeImageClassLoaderSupport);
}
ClassLoader nativeImageClassLoader = nativeImageClassLoaderSupport.getClassLoader();
Thread.currentThread().setContextClassLoader(nativeImageClassLoader);
/*
Expand Down

0 comments on commit 5fb53fa

Please sign in to comment.