Skip to content

Commit f8dbcc1

Browse files
committed
svm: introduce HostModuleUtil#addReads
1 parent e6eb015 commit f8dbcc1

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (c) 2025, 2025, 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+
package com.oracle.svm.util;
26+
27+
/**
28+
* Utilities to access {@linkplain ResolvedJavaModule runtime modules} from hosted code.
29+
*/
30+
public abstract class HostModuleUtil {
31+
32+
/**
33+
* Updates the module of {@code hostedClass} to read {@code runtimeModule} during the image
34+
* build. This method is not meant to change any runtime property of {@code runtimeModule}.
35+
*/
36+
public static void addReads(Class<?> hostedClass, ResolvedJavaModule runtimeModule) {
37+
addReads(hostedClass.getModule(), runtimeModule);
38+
}
39+
40+
/**
41+
* Updates a {@code hostedModule} to read {@code runtimeModule} during the image build. This
42+
* method is not meant to change any runtime property of {@code runtimeModule}.
43+
*/
44+
private static void addReads(Module hostedModule, ResolvedJavaModule runtimeModule) {
45+
ResolvedJavaModuleImpl.addReads(hostedModule, runtimeModule);
46+
}
47+
48+
private HostModuleUtil() {
49+
}
50+
}

substratevm/src/com.oracle.svm.util/src/com/oracle/svm/util/ModuleSupport.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,10 @@ public static void accessModuleByClass(Access access, Class<?> accessingClass, M
161161
}
162162

163163
@Platforms(Platform.HOSTED_ONLY.class)
164-
public static void accessModule(Access access, Module accessingModule, Module declaringModule, String packageName) {
165-
access.giveAccess(accessingModule, declaringModule, packageName);
164+
public static void accessModule(Access access, Module accessingModule, Module declaringModule, String... packageNames) {
165+
Set<String> packages = packageNames.length > 0 ? Set.of(packageNames) : declaringModule.getPackages();
166+
for (String packageName : packages) {
167+
access.giveAccess(accessingModule, declaringModule, packageName);
168+
}
166169
}
167170
}

substratevm/src/com.oracle.svm.util/src/com/oracle/svm/util/ResolvedJavaModuleImpl.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,8 @@ private static ResolvedJavaModuleImpl toImpl(ResolvedJavaModule module) {
9999
}
100100
throw new IllegalArgumentException("Unsupported ResolvedJavaModule implementation: " + module.getClass().getName());
101101
}
102+
103+
static void addReads(Module accessingModule, ResolvedJavaModule declaringModule) {
104+
ModuleSupport.accessModule(ModuleSupport.Access.OPEN, accessingModule, toImpl(declaringModule).module);
105+
}
102106
}

0 commit comments

Comments
 (0)