Skip to content

Commit 15dd991

Browse files
committed
Extend mx hellomodule to test --add-exports and --add-opens
1 parent 59ef5d4 commit 15dd991

File tree

5 files changed

+84
-2
lines changed

5 files changed

+84
-2
lines changed

substratevm/mx.substratevm/mx_substratevm.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1156,7 +1156,11 @@ def hellomodule(args):
11561156
# Build module into native image
11571157
mx.log('Building image from java modules: ' + str(module_path))
11581158
module_path_sep = ';' if mx.is_windows() else ':'
1159-
built_image = native_image(['--verbose', '-ea', '-H:Path=' + build_dir, '-p', module_path_sep.join(module_path), '-m', 'moduletests.hello.app'])
1159+
built_image = native_image([
1160+
'--verbose', '-ea', '-H:Path=' + build_dir,
1161+
'--add-exports=moduletests.hello.lib/hello.privateLib=moduletests.hello.app',
1162+
'--add-exports=moduletests.hello.lib/hello.privateLib2=moduletests.hello.app',
1163+
'-p', module_path_sep.join(module_path), '-m', 'moduletests.hello.app'])
11601164
mx.log('Running image ' + built_image + ' built from module:')
11611165
mx.run([built_image])
11621166

substratevm/src/native-image-module-tests/hello.app/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ questions.
5050
<configuration>
5151
<source>11</source>
5252
<target>11</target>
53+
<compilerArgs>
54+
<arg>--add-exports=moduletests.hello.lib/hello.privateLib=moduletests.hello.app</arg>
55+
<arg>--add-exports=moduletests.hello.lib/hello.privateLib2=moduletests.hello.app</arg>
56+
<arg>--add-opens=moduletests.hello.lib/hello.privateLib2=moduletests.hello.app</arg>
57+
</compilerArgs>
5358
</configuration>
5459
</plugin>
5560
<plugin>

substratevm/src/native-image-module-tests/hello.app/src/main/java/hello/Main.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,11 @@
2626

2727
import hello.lib.Greeter;
2828

29+
import java.lang.reflect.InvocationTargetException;
30+
import java.lang.reflect.Method;
31+
2932
public class Main {
30-
public static void main(String[] args) {
33+
public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
3134
Module helloAppModule = Main.class.getModule();
3235
assert helloAppModule.getName().equals("moduletests.hello.app");
3336
assert helloAppModule.isExported("hello");
@@ -41,5 +44,13 @@ public static void main(String[] args) {
4144

4245
System.out.println("Basic Module test involving " + helloAppModule + " and " + helloLibModule);
4346
Greeter.greet();
47+
48+
System.out.println("Now accessing package that is not exported in " + helloLibModule);
49+
hello.privateLib.Greeter.greet();
50+
51+
System.out.println("Now accessing private method from not exported package in " + helloLibModule);
52+
Method greetMethod = hello.privateLib2.PrivateGreeter.class.getDeclaredMethod("greet");
53+
greetMethod.setAccessible(true);
54+
greetMethod.invoke(null);
4455
}
4556
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright (c) 2021, 2021, 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 hello.privateLib;
26+
27+
public class Greeter {
28+
public static void greet() {
29+
System.out.println("Seeing this requires --add-exports");
30+
}
31+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright (c) 2021, 2021, 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 hello.privateLib2;
26+
27+
public class PrivateGreeter {
28+
private static void greet() {
29+
System.out.println("Seeing this requires --add-opens");
30+
}
31+
}

0 commit comments

Comments
 (0)