Skip to content

Commit 00e45b7

Browse files
committed
Simplify config providers with Utils.toClasses().
1 parent 8b21143 commit 00e45b7

File tree

6 files changed

+76
-38
lines changed

6 files changed

+76
-38
lines changed

common/junit-platform-native/src/main/java/org/graalvm/junit/platform/NativeImageConfigurationImpl.java

-12
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,6 @@ public void registerForReflection(Field... fields) {
6565
RuntimeReflection.register(fields);
6666
}
6767

68-
@Override
69-
public void initializeAtBuildTime(String... classNames) {
70-
for (String className : classNames) {
71-
Class<?> clazz;
72-
try {
73-
clazz = Class.forName(className);
74-
initializeAtBuildTime(clazz);
75-
} catch (ClassNotFoundException e) {
76-
JUnitPlatformFeature.debug("[Native Image Configuration] Failed to register class for build time initialization: %s Reason: %s", className, e);
77-
}
78-
}
79-
}
8068

8169
@Override
8270
public void initializeAtBuildTime(Class<?>... classes) {

common/junit-platform-native/src/main/java/org/graalvm/junit/platform/config/core/NativeImageConfiguration.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
package org.graalvm.junit.platform.config.core;
4343

4444
import org.graalvm.junit.platform.JUnitPlatformFeature;
45+
import org.graalvm.junit.platform.config.util.Utils;
4546

4647
import java.lang.reflect.Executable;
4748
import java.lang.reflect.Field;
@@ -54,6 +55,10 @@ public interface NativeImageConfiguration {
5455

5556
void registerForReflection(Field... fields);
5657

58+
default void registerAllClassMembersForReflection(String... classNames) {
59+
registerAllClassMembersForReflection(Utils.toClasses(classNames));
60+
};
61+
5762
default void registerAllClassMembersForReflection(Class<?>... classes) {
5863
for (Class<?> clazz : classes) {
5964
JUnitPlatformFeature.debug("[Native Image Configuration] Registering for reflection: %s", clazz.getName());
@@ -64,7 +69,9 @@ default void registerAllClassMembersForReflection(Class<?>... classes) {
6469
}
6570
}
6671

67-
void initializeAtBuildTime(String... classNames);
72+
default void initializeAtBuildTime(String... classNames) {
73+
initializeAtBuildTime(Utils.toClasses(classNames));
74+
};
6875

6976
void initializeAtBuildTime(Class<?>... classes);
7077
}

common/junit-platform-native/src/main/java/org/graalvm/junit/platform/config/jupiter/JupiterConfigProvider.java

+4-15
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public class JupiterConfigProvider implements PluginConfigProvider {
6868

6969
@Override
7070
public void onLoad(NativeImageConfiguration config) {
71-
String[] buildTimeInitializedClasses = new String[]{
71+
config.initializeAtBuildTime(
7272
"org.junit.jupiter.engine.config.EnumConfigurationParameterConverter",
7373
"org.junit.jupiter.engine.descriptor.ClassTestDescriptor",
7474
"org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor",
@@ -84,23 +84,12 @@ public void onLoad(NativeImageConfiguration config) {
8484
// new in Junit 5.10
8585
"org.junit.platform.launcher.core.LauncherConfig",
8686
"org.junit.jupiter.engine.config.InstantiatingConfigurationParameterConverter"
87-
};
88-
for (String className : buildTimeInitializedClasses) {
89-
config.initializeAtBuildTime(className);
90-
}
87+
);
9188

92-
String[] registeredForReflection = {
89+
config.registerAllClassMembersForReflection(
9390
"org.junit.jupiter.engine.extension.TimeoutExtension$ExecutorResource",
9491
"org.junit.jupiter.engine.extension.TimeoutInvocationFactory$SingleThreadExecutorResource"
95-
};
96-
for (String className : registeredForReflection) {
97-
try {
98-
Class <?> executor = Class.forName(className);
99-
config.registerAllClassMembersForReflection(executor);
100-
} catch (ClassNotFoundException e) {
101-
debug("Failed to register class for reflection. Reason: %s", e);
102-
}
103-
}
92+
);
10493
}
10594

10695
@Override

common/junit-platform-native/src/main/java/org/graalvm/junit/platform/config/platform/PlatformConfigProvider.java

+2-5
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public class PlatformConfigProvider implements PluginConfigProvider {
4848

4949
@Override
5050
public void onLoad(NativeImageConfiguration config) {
51-
String[] buildTimeInitializedClasses = new String[] {
51+
config.initializeAtBuildTime(
5252
"org.junit.platform.launcher.TestIdentifier",
5353
"org.junit.platform.launcher.core.InternalTestPlan",
5454
"org.junit.platform.commons.util.StringUtils",
@@ -61,10 +61,7 @@ public void onLoad(NativeImageConfiguration config) {
6161
"org.junit.platform.commons.util.ReflectionUtils",
6262
// https://github.com/graalvm/native-build-tools/issues/300
6363
"org.junit.platform.reporting.open.xml.OpenTestReportGeneratingListener"
64-
};
65-
for (String className : buildTimeInitializedClasses) {
66-
config.initializeAtBuildTime(className);
67-
}
64+
);
6865

6966
try {
7067
/* Verify if the core JUnit Platform test class is available on the classpath */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright (c) 2023, 2023, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
42+
package org.graalvm.junit.platform.config.util;
43+
44+
import org.graalvm.junit.platform.JUnitPlatformFeature;
45+
46+
import java.util.Arrays;
47+
48+
public class Utils {
49+
public static Class<?>[] toClasses(String... classNames) {
50+
return Arrays.stream(classNames).map(className -> {
51+
Class<?> clazz = null;
52+
try {
53+
clazz = Class.forName(className);
54+
} catch (ClassNotFoundException e) {
55+
JUnitPlatformFeature.debug("[Native Image Configuration] Failed to resolve: %s Reason: %s", className, e);
56+
}
57+
return clazz;
58+
}).filter(c -> c != null).toArray(Class[]::new);
59+
}
60+
}

common/junit-platform-native/src/main/java/org/graalvm/junit/platform/config/vintage/VintageConfigProvider.java

+2-5
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public class VintageConfigProvider implements PluginConfigProvider {
4848

4949
@Override
5050
public void onLoad(NativeImageConfiguration config) {
51-
String[] buildTimeInitializedClasses = new String[]{
51+
config.initializeAtBuildTime(
5252
"org.junit.vintage.engine.descriptor.RunnerTestDescriptor",
5353
"org.junit.vintage.engine.support.UniqueIdReader",
5454
"org.junit.vintage.engine.support.UniqueIdStringifier",
@@ -57,10 +57,7 @@ public void onLoad(NativeImageConfiguration config) {
5757
"org.junit.runners.JUnit4",
5858
/* Workaround until we can register serializable classes from a native-image feature */
5959
"org.junit.runner.Result"
60-
};
61-
for (String className : buildTimeInitializedClasses) {
62-
config.initializeAtBuildTime(className);
63-
}
60+
);
6461
}
6562

6663
@Override

0 commit comments

Comments
 (0)