Skip to content

Commit 12acaa7

Browse files
authored
Update Classes.getClass() to never use the bootstrap class loader. (junit-team#1404)
Previously, if there was no context class loader then the bootstrap class loader is used. Now, the class loader for Classes is used (or for the passed in class, if the new overloaded version of getClass() is used).
1 parent fc06ad1 commit 12acaa7

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/main/java/org/junit/experimental/categories/CategoryFilterFactory.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ private List<Class<?>> parseCategories(String categories) throws ClassNotFoundEx
3737
List<Class<?>> categoryClasses = new ArrayList<Class<?>>();
3838

3939
for (String category : categories.split(",")) {
40-
Class<?> categoryClass = Classes.getClass(category);
40+
/*
41+
* Load the category class using the context class loader.
42+
* If there is no context class loader, use the class loader for this class.
43+
*/
44+
Class<?> categoryClass = Classes.getClass(category, getClass());
4145

4246
categoryClasses.add(categoryClass);
4347
}

src/main/java/org/junit/internal/Classes.java

+18-1
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,28 @@ public Classes() {
1717

1818
/**
1919
* Returns Class.forName for {@code className} using the current thread's class loader.
20+
* If the current thread does not have a class loader, falls back to the class loader for
21+
* {@link Classes}.
2022
*
2123
* @param className Name of the class.
2224
* @throws ClassNotFoundException
2325
*/
2426
public static Class<?> getClass(String className) throws ClassNotFoundException {
25-
return Class.forName(className, true, currentThread().getContextClassLoader());
27+
return getClass(className, Classes.class);
28+
}
29+
30+
/**
31+
* Returns Class.forName for {@code className} using the current thread's class loader.
32+
* If the current thread does not have a class loader, falls back to the class loader for the
33+
* passed-in class.
34+
*
35+
* @param className Name of the class.
36+
* @param callingClass Class that is requesting a the class
37+
* @throws ClassNotFoundException
38+
* @since 4.13
39+
*/
40+
public static Class<?> getClass(String className, Class<?> callingClass) throws ClassNotFoundException {
41+
ClassLoader classLoader = currentThread().getContextClassLoader();
42+
return Class.forName(className, true, classLoader == null ? callingClass.getClassLoader() : classLoader);
2643
}
2744
}

0 commit comments

Comments
 (0)