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:
@@ -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.
|
||||
@@ -242,7 +242,7 @@ public class SessionAutoConfiguration {
|
||||
|
||||
private void addCandidateIfAvailable(List<Class<?>> candidates, String type) {
|
||||
try {
|
||||
Class<?> candidate = this.classLoader.loadClass(type);
|
||||
Class<?> candidate = Class.forName(type, false, this.classLoader);
|
||||
if (candidate != null) {
|
||||
candidates.add(candidate);
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
@@ -104,7 +104,7 @@ class ConditionalOnJavaTests {
|
||||
|
||||
private String getJavaVersion(Class<?>... hiddenClasses) throws Exception {
|
||||
FilteredClassLoader classLoader = new FilteredClassLoader(hiddenClasses);
|
||||
Class<?> javaVersionClass = classLoader.loadClass(JavaVersion.class.getName());
|
||||
Class<?> javaVersionClass = Class.forName(JavaVersion.class.getName(), false, classLoader);
|
||||
Method getJavaVersionMethod = ReflectionUtils.findMethod(javaVersionClass, "getJavaVersion");
|
||||
Object javaVersion = ReflectionUtils.invokeMethod(getJavaVersionMethod, null);
|
||||
classLoader.close();
|
||||
|
||||
@@ -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.
|
||||
@@ -59,7 +59,7 @@ public class SpringApplicationLauncher {
|
||||
public Object launch(Class<?>[] sources, String[] args) throws Exception {
|
||||
Map<String, Object> defaultProperties = new HashMap<>();
|
||||
defaultProperties.put("spring.groovy.template.check-template-location", "false");
|
||||
Class<?> applicationClass = this.classLoader.loadClass(getSpringApplicationClassName());
|
||||
Class<?> applicationClass = Class.forName(getSpringApplicationClassName(), false, this.classLoader);
|
||||
Constructor<?> constructor = applicationClass.getDeclaredConstructor(Class[].class);
|
||||
constructor.setAccessible(true);
|
||||
Object application = constructor.newInstance((Object) sources);
|
||||
|
||||
@@ -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.
|
||||
@@ -72,7 +72,7 @@ public class SpringApplicationWebApplicationInitializer extends SpringBootServle
|
||||
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
|
||||
Class<?>[] sourceClasses = new Class<?>[this.sources.length];
|
||||
for (int i = 0; i < this.sources.length; i++) {
|
||||
sourceClasses[i] = classLoader.loadClass(this.sources[i]);
|
||||
sourceClasses[i] = Class.forName(this.sources[i], false, classLoader);
|
||||
}
|
||||
return builder.sources(sourceClasses).properties("spring.groovy.template.check-template-location=false");
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
@@ -73,7 +73,7 @@ public final class PackagedSpringApplicationLauncher {
|
||||
private Class<?>[] loadClasses(ClassLoader classLoader, String[] names) throws ClassNotFoundException {
|
||||
Class<?>[] classes = new Class<?>[names.length];
|
||||
for (int i = 0; i < names.length; i++) {
|
||||
classes[i] = classLoader.loadClass(names[i]);
|
||||
classes[i] = Class.forName(names[i], false, classLoader);
|
||||
}
|
||||
return classes;
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
@@ -238,7 +238,7 @@ public class ExtendedGroovyClassLoader extends GroovyClassLoader {
|
||||
@Override
|
||||
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
|
||||
if (!name.startsWith("java.")) {
|
||||
this.groovyOnlyClassLoader.loadClass(name);
|
||||
Class.forName(name, false, this.groovyOnlyClassLoader);
|
||||
}
|
||||
return super.loadClass(name, resolve);
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
@@ -35,27 +35,27 @@ class ExtendedGroovyClassLoaderTests {
|
||||
|
||||
@Test
|
||||
void loadsGroovyFromSameClassLoader() throws Exception {
|
||||
Class<?> c1 = this.contextClassLoader.loadClass("groovy.lang.Script");
|
||||
Class<?> c2 = this.defaultScopeGroovyClassLoader.loadClass("groovy.lang.Script");
|
||||
Class<?> c1 = Class.forName("groovy.lang.Script", false, this.contextClassLoader);
|
||||
Class<?> c2 = Class.forName("groovy.lang.Script", false, this.defaultScopeGroovyClassLoader);
|
||||
assertThat(c1.getClassLoader()).isSameAs(c2.getClassLoader());
|
||||
}
|
||||
|
||||
@Test
|
||||
void filtersNonGroovy() throws Exception {
|
||||
this.contextClassLoader.loadClass("org.springframework.util.StringUtils");
|
||||
assertThatExceptionOfType(ClassNotFoundException.class)
|
||||
.isThrownBy(() -> this.defaultScopeGroovyClassLoader.loadClass("org.springframework.util.StringUtils"));
|
||||
Class.forName("org.springframework.util.StringUtils", false, this.contextClassLoader);
|
||||
assertThatExceptionOfType(ClassNotFoundException.class).isThrownBy(
|
||||
() -> Class.forName("org.springframework.util.StringUtils", false, this.defaultScopeGroovyClassLoader));
|
||||
}
|
||||
|
||||
@Test
|
||||
void loadsJavaTypes() throws Exception {
|
||||
this.defaultScopeGroovyClassLoader.loadClass("java.lang.Boolean");
|
||||
Class.forName("java.lang.Boolean", false, this.defaultScopeGroovyClassLoader);
|
||||
}
|
||||
|
||||
@Test
|
||||
void loadsSqlTypes() throws Exception {
|
||||
this.contextClassLoader.loadClass("java.sql.SQLException");
|
||||
this.defaultScopeGroovyClassLoader.loadClass("java.sql.SQLException");
|
||||
Class.forName("java.sql.SQLException", false, this.contextClassLoader);
|
||||
Class.forName("java.sql.SQLException", false, this.defaultScopeGroovyClassLoader);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
@@ -125,13 +125,13 @@ class RestartClassLoaderTests {
|
||||
|
||||
@Test
|
||||
void loadClassFromReloadableUrl() throws Exception {
|
||||
Class<?> loaded = this.reloadClassLoader.loadClass(PACKAGE + ".Sample");
|
||||
Class<?> loaded = Class.forName(PACKAGE + ".Sample", false, this.reloadClassLoader);
|
||||
assertThat(loaded.getClassLoader()).isEqualTo(this.reloadClassLoader);
|
||||
}
|
||||
|
||||
@Test
|
||||
void loadClassFromParent() throws Exception {
|
||||
Class<?> loaded = this.reloadClassLoader.loadClass(PACKAGE + ".SampleParent");
|
||||
Class<?> loaded = Class.forName(PACKAGE + ".SampleParent", false, this.reloadClassLoader);
|
||||
assertThat(loaded.getClassLoader()).isEqualTo(getClass().getClassLoader());
|
||||
}
|
||||
|
||||
@@ -180,7 +180,7 @@ class RestartClassLoaderTests {
|
||||
String name = PACKAGE_PATH + "/Sample.class";
|
||||
this.updatedFiles.addFile(name, new ClassLoaderFile(Kind.DELETED, null));
|
||||
assertThatExceptionOfType(ClassNotFoundException.class)
|
||||
.isThrownBy(() -> this.reloadClassLoader.loadClass(PACKAGE + ".Sample"));
|
||||
.isThrownBy(() -> Class.forName(PACKAGE + ".Sample", false, this.reloadClassLoader));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -188,7 +188,7 @@ class RestartClassLoaderTests {
|
||||
String name = PACKAGE_PATH + "/Sample.class";
|
||||
this.updatedFiles.addFile(name, new ClassLoaderFile(Kind.MODIFIED, new byte[10]));
|
||||
assertThatExceptionOfType(ClassFormatError.class)
|
||||
.isThrownBy(() -> this.reloadClassLoader.loadClass(PACKAGE + ".Sample"));
|
||||
.isThrownBy(() -> Class.forName(PACKAGE + ".Sample", false, this.reloadClassLoader));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -196,7 +196,7 @@ class RestartClassLoaderTests {
|
||||
String name = PACKAGE_PATH + "/SampleParent.class";
|
||||
byte[] bytes = FileCopyUtils.copyToByteArray(getClass().getResourceAsStream("SampleParent.class"));
|
||||
this.updatedFiles.addFile(name, new ClassLoaderFile(Kind.ADDED, bytes));
|
||||
Class<?> loaded = this.reloadClassLoader.loadClass(PACKAGE + ".SampleParent");
|
||||
Class<?> loaded = Class.forName(PACKAGE + ".SampleParent", false, this.reloadClassLoader);
|
||||
assertThat(loaded.getClassLoader()).isEqualTo(this.reloadClassLoader);
|
||||
}
|
||||
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
@@ -54,7 +54,7 @@ class ReflectionWrapper {
|
||||
|
||||
protected static Class<?> findClass(ClassLoader classLoader, String name) {
|
||||
try {
|
||||
return classLoader.loadClass(name);
|
||||
return Class.forName(name, false, classLoader);
|
||||
}
|
||||
catch (ClassNotFoundException ex) {
|
||||
throw new IllegalStateException(ex);
|
||||
|
||||
@@ -32,7 +32,7 @@ public class SampleApplication {
|
||||
|
||||
try {
|
||||
ClassLoader classLoader = SampleApplication.class.getClassLoader();
|
||||
classLoader.loadClass(className);
|
||||
Class.forName(className, false, classLoader);
|
||||
return true;
|
||||
}
|
||||
catch (ClassNotFoundException e) {
|
||||
|
||||
@@ -559,7 +559,7 @@ public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo {
|
||||
Thread thread = Thread.currentThread();
|
||||
ClassLoader classLoader = thread.getContextClassLoader();
|
||||
try {
|
||||
Class<?> startClass = classLoader.loadClass(this.startClassName);
|
||||
Class<?> startClass = Class.forName(this.startClassName, false, classLoader);
|
||||
Method mainMethod = startClass.getMethod("main", String[].class);
|
||||
if (!mainMethod.isAccessible()) {
|
||||
mainMethod.setAccessible(true);
|
||||
|
||||
@@ -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.
|
||||
@@ -96,7 +96,7 @@ class ModifiedClassPathExtension implements InvocationInterceptor {
|
||||
}
|
||||
|
||||
private void runTest(ClassLoader classLoader, String testClassName, String testMethodName) throws Throwable {
|
||||
Class<?> testClass = classLoader.loadClass(testClassName);
|
||||
Class<?> testClass = Class.forName(testClassName, false, classLoader);
|
||||
Method testMethod = findMethod(testClass, testMethodName);
|
||||
LauncherDiscoveryRequest request = new LauncherDiscoveryRequestBuilder(classLoader)
|
||||
.selectors(DiscoverySelectors.selectMethod(testClass, testMethod)).build();
|
||||
|
||||
Reference in New Issue
Block a user