Use Class.forName rather than ClassLoader.loadClass

This commit changes uses of ClassLoader.loadClass to Class.forName for
consistency with what was initiated in #19342 and better compatibility
with GraalVM.

Closes gh-19824
This commit is contained in:
Stephane Nicoll
2020-01-23 10:47:53 +01:00
parent 797c30f952
commit 95be419527
13 changed files with 37 additions and 37 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -43,7 +43,7 @@ class FilteredClassLoaderTests {
try (FilteredClassLoader classLoader = new FilteredClassLoader(
FilteredClassLoaderTests.class.getPackage().getName())) {
assertThatExceptionOfType(ClassNotFoundException.class)
.isThrownBy(() -> classLoader.loadClass(getClass().getName()));
.isThrownBy(() -> Class.forName(getClass().getName(), false, classLoader));
}
}
@@ -51,14 +51,14 @@ class FilteredClassLoaderTests {
void loadClassWhenFilteredOnClassShouldThrowClassNotFound() throws Exception {
try (FilteredClassLoader classLoader = new FilteredClassLoader(FilteredClassLoaderTests.class)) {
assertThatExceptionOfType(ClassNotFoundException.class)
.isThrownBy(() -> classLoader.loadClass(getClass().getName()));
.isThrownBy(() -> Class.forName(getClass().getName(), false, classLoader));
}
}
@Test
void loadClassWhenNotFilteredShouldLoadClass() throws Exception {
FilteredClassLoader classLoader = new FilteredClassLoader((className) -> false);
Class<?> loaded = classLoader.loadClass(getClass().getName());
Class<?> loaded = Class.forName(getClass().getName(), false, classLoader);
assertThat(loaded.getName()).isEqualTo(getClass().getName());
classLoader.close();
}