Skip to content

Commit 3364e05

Browse files
committed
Extract annottaion element fetching into a separate method
1 parent 82d3fbb commit 3364e05

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

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

+20
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@
4141

4242
package org.graalvm.junit.platform.config.core;
4343

44+
import java.lang.annotation.Annotation;
45+
import java.lang.reflect.Method;
46+
4447
public abstract class PluginConfigProvider {
4548

4649
protected ClassLoader applicationClassLoader;
@@ -55,4 +58,21 @@ public final void initialize(ClassLoader classLoader, NativeImageConfiguration n
5558
applicationClassLoader = classLoader;
5659
nativeImageConfigImpl = nic;
5760
}
61+
62+
@SuppressWarnings("unchecked")
63+
protected final <T> T getAnnotationElementValue(Class<?> annotatedClass, String annotationName, String annotationElementName) {
64+
try {
65+
Class<Annotation> annotation = (Class<Annotation>) applicationClassLoader.loadClass(annotationName);
66+
Method classProvider = annotation.getDeclaredMethod(annotationElementName);
67+
68+
Annotation classAnnotation = annotatedClass.getAnnotation(annotation);
69+
if (classAnnotation != null) {
70+
return (T) classProvider.invoke(classAnnotation);
71+
}
72+
} catch (ReflectiveOperationException e) {
73+
// intentionally ignored
74+
}
75+
76+
return null;
77+
}
5878
}

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

+6-18
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,6 @@
4646
import org.graalvm.nativeimage.hosted.RuntimeReflection;
4747
import org.graalvm.nativeimage.hosted.RuntimeSerialization;
4848

49-
import java.lang.annotation.Annotation;
50-
import java.lang.reflect.Method;
51-
5249
public class VintageConfigProvider extends PluginConfigProvider {
5350

5451
@Override
@@ -63,23 +60,14 @@ public void onLoad(NativeImageConfiguration config) {
6360

6461
@Override
6562
public void onTestClassRegistered(Class<?> testClass, NativeImageConfiguration registry) {
66-
registerAnnotationClassesForReflection("org.junit.runner.RunWith", "value", testClass);
67-
registerAnnotationClassesForReflection("org.junit.runners.Parameterized.UseParametersRunnerFactory", "value", testClass);
63+
registerAnnotationClassesForReflection(testClass, "org.junit.runner.RunWith", "value");
64+
registerAnnotationClassesForReflection(testClass, "org.junit.runners.Parameterized.UseParametersRunnerFactory", "value");
6865
}
6966

70-
@SuppressWarnings("unchecked")
71-
private void registerAnnotationClassesForReflection(String annotationClass, String classProviderMethod, Class<?> testClass) {
72-
try {
73-
Class<Annotation> annotation = (Class<Annotation>) applicationClassLoader.loadClass(annotationClass);
74-
Method classProvider = annotation.getDeclaredMethod(classProviderMethod);
75-
76-
Annotation classAnnotation = testClass.getAnnotation(annotation);
77-
if (classAnnotation != null) {
78-
Class<?> annotationArgument = (Class<?>) classProvider.invoke(classAnnotation);
79-
nativeImageConfigImpl.registerAllClassMembersForReflection(annotationArgument);
80-
}
81-
} catch (ReflectiveOperationException e) {
82-
// intentionally ignored
67+
private void registerAnnotationClassesForReflection(Class<?> testClass, String annotationName, String annotationElementName) {
68+
Class<?> annotationArgument = getAnnotationElementValue(testClass, annotationName, annotationElementName);
69+
if (annotationArgument != null) {
70+
nativeImageConfigImpl.registerAllClassMembersForReflection(annotationArgument);
8371
}
8472
}
8573
}

0 commit comments

Comments
 (0)