Skip to content

Commit 010e771

Browse files
committed
Add initial implementation of --require-complete-classdef
1 parent b8485c3 commit 010e771

File tree

5 files changed

+126
-4
lines changed

5 files changed

+126
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
ImageName = native-image
2-
Args = -H:-ParseRuntimeOptions --initialize-at-build-time=com.oracle.svm.driver
2+
Args = -H:-ParseRuntimeOptions \
3+
--initialize-at-build-time=com.oracle.svm.driver \
4+
--require-complete-classdef

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/AbstractNativeImageClassLoaderSupport.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public ClassLoader getClassLoader() {
108108

109109
protected abstract List<Path> applicationModulePath();
110110

111-
protected abstract Optional<? extends Object> findModule(String moduleName);
111+
protected abstract Optional<Module> findModule(String moduleName);
112112

113113
private HostedOptionParser hostedOptionParser;
114114
private OptionValues parsedHostedOptions;

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/ImageClassLoader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ public Optional<String> getMainClassFromModule(Object module) {
427427
return classLoaderSupport.getMainClassFromModule(module);
428428
}
429429

430-
public Optional<? extends Object> findModule(String moduleName) {
430+
public Optional<Module> findModule(String moduleName) {
431431
return classLoaderSupport.findModule(moduleName);
432432
}
433433
}

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/NativeImageClassLoaderSupport.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ protected List<Path> applicationModulePath() {
4747
}
4848

4949
@Override
50-
protected Optional<Object> findModule(String moduleName) {
50+
protected Optional<Module> findModule(String moduleName) {
5151
return Optional.empty();
5252
}
5353

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
* Copyright (c) 2022, 2022, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
26+
package com.oracle.svm.hosted;
27+
28+
import java.lang.module.ModuleFinder;
29+
import java.net.URI;
30+
import java.net.URISyntaxException;
31+
import java.nio.file.Path;
32+
import java.util.Arrays;
33+
import java.util.HashSet;
34+
import java.util.Map;
35+
import java.util.Objects;
36+
import java.util.Set;
37+
import java.util.stream.Collectors;
38+
39+
import org.graalvm.collections.Pair;
40+
import org.graalvm.compiler.options.Option;
41+
import org.graalvm.nativeimage.hosted.Feature;
42+
43+
import com.oracle.svm.core.SubstrateUtil;
44+
import com.oracle.svm.core.annotate.AutomaticFeature;
45+
import com.oracle.svm.core.option.APIOption;
46+
import com.oracle.svm.core.option.HostedOptionKey;
47+
import com.oracle.svm.core.option.LocatableMultiOptionValue;
48+
import com.oracle.svm.core.option.SubstrateOptionsParser;
49+
import com.oracle.svm.core.util.UserError;
50+
51+
@AutomaticFeature
52+
public final class RequireCompleteClassDefFeature implements Feature {
53+
54+
static final class Options {
55+
@APIOption(name = "require-complete-classdef", defaultValue = "")//
56+
@Option(help = "Require types to be fully defined at image build-time. If used without args, all classes in the scope the option are required to be fully defined.")//
57+
public static final HostedOptionKey<LocatableMultiOptionValue.Strings> RequireCompleteClassDef = new HostedOptionKey<>(new LocatableMultiOptionValue.Strings());
58+
}
59+
60+
private final Set<String> requireCompletePackageOrClass = new HashSet<>();
61+
private final Set<Module> requireCompleteModules = new HashSet<>();
62+
private boolean requireCompleteAll = false;
63+
64+
Map<URI, Module> moduleFromURI;
65+
66+
@Override
67+
public void beforeAnalysis(BeforeAnalysisAccess access) {
68+
var loader = ((FeatureImpl.BeforeAnalysisAccessImpl) access).getImageClassLoader();
69+
70+
moduleFromURI = ModuleFinder.of(loader.applicationModulePath().toArray(Path[]::new)).findAll().stream()
71+
.filter(mRef -> mRef.location().isPresent())
72+
.collect(Collectors.toUnmodifiableMap(mRef -> mRef.location().get(), mRef -> loader.findModule(mRef.descriptor().name()).get()));
73+
74+
Options.RequireCompleteClassDef.getValue().getValuesWithOrigins().forEach(this::extractRequireCompleteClasses);
75+
76+
System.out.println("DONE");
77+
}
78+
79+
private void extractRequireCompleteClasses(Pair<String, String> valueOrigin) {
80+
var value = valueOrigin.getLeft();
81+
if (value.isEmpty()) {
82+
String origin = valueOrigin.getRight();
83+
if (origin == null) {
84+
requireCompleteAll = true;
85+
return;
86+
}
87+
var originURI = originURI(origin);
88+
if (originURI == null) {
89+
throw notUsedInModulePath(origin);
90+
}
91+
if (originURI.getScheme().equals("jar")) {
92+
var specific = originURI.getSchemeSpecificPart();
93+
var specificJarFile = specific.substring(0, specific.lastIndexOf('!'));
94+
var specificInner = specific.substring(specific.lastIndexOf('!') + 1);
95+
originURI = originURI(specificJarFile);
96+
}
97+
var originModule = moduleFromURI.get(originURI);
98+
if (originModule == null) {
99+
throw notUsedInModulePath(origin);
100+
}
101+
requireCompleteModules.add(originModule);
102+
} else {
103+
requireCompletePackageOrClass.addAll(Arrays.asList(SubstrateUtil.split(value, ",")));
104+
}
105+
}
106+
107+
private URI originURI(String origin) {
108+
Objects.requireNonNull(origin);
109+
try {
110+
return new URI(origin);
111+
} catch (URISyntaxException x) {
112+
return null;
113+
}
114+
}
115+
116+
private UserError.UserException notUsedInModulePath(String origin) {
117+
return UserError.abort("Using %s without args is only allowed on module-path. Actual location %s",
118+
SubstrateOptionsParser.commandArgument(Options.RequireCompleteClassDef, ""), origin);
119+
}
120+
}

0 commit comments

Comments
 (0)