Skip to content

Commit cd4d060

Browse files
committed
Introduce ClassLoaderUtils.getClassLoader(Class)
1 parent 142476c commit cd4d060

File tree

6 files changed

+67
-16
lines changed

6 files changed

+67
-16
lines changed

junit-jupiter-api/src/main/java/org/junit/jupiter/api/condition/MethodBasedCondition.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,7 @@ Method getConditionMethod(String fullyQualifiedMethodName, ExtensionContext cont
6666
String[] methodParts = ReflectionUtils.parseFullyQualifiedMethodName(fullyQualifiedMethodName);
6767
String className = methodParts[0];
6868
String methodName = methodParts[1];
69-
ClassLoader classLoader = testClass.getClassLoader();
70-
if (classLoader == null) {
71-
classLoader = ClassLoaderUtils.getDefaultClassLoader();
72-
}
69+
ClassLoader classLoader = ClassLoaderUtils.getClassLoader(testClass);
7370
Class<?> clazz = ReflectionUtils.tryToLoadClass(className, classLoader).getOrThrow(
7471
cause -> new JUnitException(format("Could not load class [%s]", className), cause));
7572
return findMethod(clazz, methodName);

junit-jupiter-params/src/main/java/org/junit/jupiter/params/converter/DefaultArgumentConverter.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,8 @@ public final Object convert(Object source, Class<?> targetType, ParameterContext
9292
Optional<StringToObjectConverter> converter = stringToObjectConverters.stream().filter(
9393
candidate -> candidate.canConvert(targetTypeToUse)).findFirst();
9494
if (converter.isPresent()) {
95-
ClassLoader classLoader = context.getDeclaringExecutable().getDeclaringClass().getClassLoader();
96-
if (classLoader == null) {
97-
classLoader = ClassLoaderUtils.getDefaultClassLoader();
98-
}
95+
Class<?> declaringClass = context.getDeclaringExecutable().getDeclaringClass();
96+
ClassLoader classLoader = ClassLoaderUtils.getClassLoader(declaringClass);
9997
try {
10098
return converter.get().convert((String) source, targetTypeToUse, classLoader);
10199
}

junit-jupiter-params/src/main/java/org/junit/jupiter/params/provider/MethodArgumentsProvider.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,7 @@ static Method findFactoryMethodByFullyQualifiedName(Class<?> testClass, Method t
111111
String className = methodParts[0];
112112
String methodName = methodParts[1];
113113
String methodParameters = methodParts[2];
114-
ClassLoader classLoader = testClass.getClassLoader();
115-
if (classLoader == null) {
116-
classLoader = ClassLoaderUtils.getDefaultClassLoader();
117-
}
114+
ClassLoader classLoader = ClassLoaderUtils.getClassLoader(testClass);
118115
Class<?> clazz = loadRequiredClass(className, classLoader);
119116

120117
// Attempt to find an exact match first.

junit-platform-commons/src/main/java/org/junit/platform/commons/util/ClassLoaderUtils.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,19 @@ private ClassLoaderUtils() {
3636
/* no-op */
3737
}
3838

39+
/**
40+
* Get the {@link ClassLoader} for the supplied {@link Class}, falling back
41+
* to the {@link #getDefaultClassLoader() default class loader} if the class
42+
* loader for the supplied class is {@code null}.
43+
* @param clazz the class for which to retrieve the class loader; never {@code null}
44+
* @since 1.10
45+
*/
46+
public static ClassLoader getClassLoader(Class<?> clazz) {
47+
Preconditions.notNull(clazz, "Class must not be null");
48+
ClassLoader classLoader = clazz.getClassLoader();
49+
return (classLoader != null) ? classLoader : getDefaultClassLoader();
50+
}
51+
3952
public static ClassLoader getDefaultClassLoader() {
4053
try {
4154
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();

junit-platform-commons/src/main/java/org/junit/platform/commons/util/ReflectionUtils.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1375,10 +1375,7 @@ private static Class<?>[] resolveParameterTypes(Class<?> clazz, String methodNam
13751375
}
13761376

13771377
private static Class<?> loadRequiredParameterType(Class<?> clazz, String methodName, String typeName) {
1378-
ClassLoader classLoader = clazz.getClassLoader();
1379-
if (classLoader == null) {
1380-
classLoader = ClassLoaderUtils.getDefaultClassLoader();
1381-
}
1378+
ClassLoader classLoader = ClassLoaderUtils.getClassLoader(clazz);
13821379

13831380
// @formatter:off
13841381
return tryToLoadClass(typeName, classLoader)

platform-tests/src/test/java/org/junit/platform/commons/util/ClassLoaderUtilsTests.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
package org.junit.platform.commons.util;
1212

13+
import static org.assertj.core.api.Assertions.assertThat;
14+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
1315
import static org.junit.jupiter.api.Assertions.assertEquals;
1416
import static org.junit.jupiter.api.Assertions.assertSame;
1517
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -18,6 +20,7 @@
1820

1921
import org.junit.jupiter.api.Test;
2022
import org.junit.platform.commons.PreconditionViolationException;
23+
import org.junit.platform.commons.test.TestClassLoader;
2124

2225
/**
2326
* Unit tests for {@link ClassLoaderUtils}.
@@ -26,6 +29,52 @@
2629
*/
2730
class ClassLoaderUtilsTests {
2831

32+
@Test
33+
void getClassLoaderPreconditions() {
34+
assertThatExceptionOfType(PreconditionViolationException.class)//
35+
.isThrownBy(() -> ClassLoaderUtils.getClassLoader(null))//
36+
.withMessage("Class must not be null");
37+
}
38+
39+
@Test
40+
void getClassLoaderForPrimitive() {
41+
assertThat(int.class.getClassLoader()).isNull();
42+
ClassLoader classLoader = ClassLoaderUtils.getClassLoader(int.class);
43+
assertThat(classLoader).isSameAs(getClass().getClassLoader());
44+
}
45+
46+
@Test
47+
void getClassLoaderForWrapperType() {
48+
assertThat(Byte.class.getClassLoader()).isNull();
49+
ClassLoader classLoader = ClassLoaderUtils.getClassLoader(Byte.class);
50+
assertThat(classLoader).isSameAs(getClass().getClassLoader());
51+
}
52+
53+
@Test
54+
void getClassLoaderForVoidType() {
55+
assertThat(void.class.getClassLoader()).isNull();
56+
ClassLoader classLoader = ClassLoaderUtils.getClassLoader(void.class);
57+
assertThat(classLoader).isSameAs(getClass().getClassLoader());
58+
}
59+
60+
@Test
61+
void getClassLoaderForTestClass() {
62+
assertThat(getClass().getClassLoader()).isNotNull();
63+
ClassLoader classLoader = ClassLoaderUtils.getClassLoader(getClass());
64+
assertThat(classLoader).isSameAs(getClass().getClassLoader());
65+
}
66+
67+
@Test
68+
void getClassLoaderForClassInDifferentClassLoader() throws Exception {
69+
try (var testClassLoader = TestClassLoader.forClasses(getClass())) {
70+
var testClass = testClassLoader.loadClass(getClass().getName());
71+
assertThat(testClass.getClassLoader()).isSameAs(testClassLoader);
72+
73+
var classLoader = ClassLoaderUtils.getClassLoader(testClass);
74+
assertThat(classLoader).isSameAs(testClassLoader);
75+
}
76+
}
77+
2978
@Test
3079
void getDefaultClassLoaderWithExplicitContextClassLoader() {
3180
var original = Thread.currentThread().getContextClassLoader();

0 commit comments

Comments
 (0)