Commit 95be4195 authored by Stephane Nicoll's avatar Stephane Nicoll

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
parent 797c30f9
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -242,7 +242,7 @@ public class SessionAutoConfiguration { ...@@ -242,7 +242,7 @@ public class SessionAutoConfiguration {
private void addCandidateIfAvailable(List<Class<?>> candidates, String type) { private void addCandidateIfAvailable(List<Class<?>> candidates, String type) {
try { try {
Class<?> candidate = this.classLoader.loadClass(type); Class<?> candidate = Class.forName(type, false, this.classLoader);
if (candidate != null) { if (candidate != null) {
candidates.add(candidate); candidates.add(candidate);
} }
......
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -104,7 +104,7 @@ class ConditionalOnJavaTests { ...@@ -104,7 +104,7 @@ class ConditionalOnJavaTests {
private String getJavaVersion(Class<?>... hiddenClasses) throws Exception { private String getJavaVersion(Class<?>... hiddenClasses) throws Exception {
FilteredClassLoader classLoader = new FilteredClassLoader(hiddenClasses); 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"); Method getJavaVersionMethod = ReflectionUtils.findMethod(javaVersionClass, "getJavaVersion");
Object javaVersion = ReflectionUtils.invokeMethod(getJavaVersionMethod, null); Object javaVersion = ReflectionUtils.invokeMethod(getJavaVersionMethod, null);
classLoader.close(); classLoader.close();
......
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -59,7 +59,7 @@ public class SpringApplicationLauncher { ...@@ -59,7 +59,7 @@ public class SpringApplicationLauncher {
public Object launch(Class<?>[] sources, String[] args) throws Exception { public Object launch(Class<?>[] sources, String[] args) throws Exception {
Map<String, Object> defaultProperties = new HashMap<>(); Map<String, Object> defaultProperties = new HashMap<>();
defaultProperties.put("spring.groovy.template.check-template-location", "false"); 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<?> constructor = applicationClass.getDeclaredConstructor(Class[].class);
constructor.setAccessible(true); constructor.setAccessible(true);
Object application = constructor.newInstance((Object) sources); Object application = constructor.newInstance((Object) sources);
......
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -72,7 +72,7 @@ public class SpringApplicationWebApplicationInitializer extends SpringBootServle ...@@ -72,7 +72,7 @@ public class SpringApplicationWebApplicationInitializer extends SpringBootServle
ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
Class<?>[] sourceClasses = new Class<?>[this.sources.length]; Class<?>[] sourceClasses = new Class<?>[this.sources.length];
for (int i = 0; i < this.sources.length; i++) { 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"); return builder.sources(sourceClasses).properties("spring.groovy.template.check-template-location=false");
} }
......
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -73,7 +73,7 @@ public final class PackagedSpringApplicationLauncher { ...@@ -73,7 +73,7 @@ public final class PackagedSpringApplicationLauncher {
private Class<?>[] loadClasses(ClassLoader classLoader, String[] names) throws ClassNotFoundException { private Class<?>[] loadClasses(ClassLoader classLoader, String[] names) throws ClassNotFoundException {
Class<?>[] classes = new Class<?>[names.length]; Class<?>[] classes = new Class<?>[names.length];
for (int i = 0; i < names.length; i++) { for (int i = 0; i < names.length; i++) {
classes[i] = classLoader.loadClass(names[i]); classes[i] = Class.forName(names[i], false, classLoader);
} }
return classes; return classes;
} }
......
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -238,7 +238,7 @@ public class ExtendedGroovyClassLoader extends GroovyClassLoader { ...@@ -238,7 +238,7 @@ public class ExtendedGroovyClassLoader extends GroovyClassLoader {
@Override @Override
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
if (!name.startsWith("java.")) { if (!name.startsWith("java.")) {
this.groovyOnlyClassLoader.loadClass(name); Class.forName(name, false, this.groovyOnlyClassLoader);
} }
return super.loadClass(name, resolve); return super.loadClass(name, resolve);
} }
......
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -35,27 +35,27 @@ class ExtendedGroovyClassLoaderTests { ...@@ -35,27 +35,27 @@ class ExtendedGroovyClassLoaderTests {
@Test @Test
void loadsGroovyFromSameClassLoader() throws Exception { void loadsGroovyFromSameClassLoader() throws Exception {
Class<?> c1 = this.contextClassLoader.loadClass("groovy.lang.Script"); Class<?> c1 = Class.forName("groovy.lang.Script", false, this.contextClassLoader);
Class<?> c2 = this.defaultScopeGroovyClassLoader.loadClass("groovy.lang.Script"); Class<?> c2 = Class.forName("groovy.lang.Script", false, this.defaultScopeGroovyClassLoader);
assertThat(c1.getClassLoader()).isSameAs(c2.getClassLoader()); assertThat(c1.getClassLoader()).isSameAs(c2.getClassLoader());
} }
@Test @Test
void filtersNonGroovy() throws Exception { void filtersNonGroovy() throws Exception {
this.contextClassLoader.loadClass("org.springframework.util.StringUtils"); Class.forName("org.springframework.util.StringUtils", false, this.contextClassLoader);
assertThatExceptionOfType(ClassNotFoundException.class) assertThatExceptionOfType(ClassNotFoundException.class).isThrownBy(
.isThrownBy(() -> this.defaultScopeGroovyClassLoader.loadClass("org.springframework.util.StringUtils")); () -> Class.forName("org.springframework.util.StringUtils", false, this.defaultScopeGroovyClassLoader));
} }
@Test @Test
void loadsJavaTypes() throws Exception { void loadsJavaTypes() throws Exception {
this.defaultScopeGroovyClassLoader.loadClass("java.lang.Boolean"); Class.forName("java.lang.Boolean", false, this.defaultScopeGroovyClassLoader);
} }
@Test @Test
void loadsSqlTypes() throws Exception { void loadsSqlTypes() throws Exception {
this.contextClassLoader.loadClass("java.sql.SQLException"); Class.forName("java.sql.SQLException", false, this.contextClassLoader);
this.defaultScopeGroovyClassLoader.loadClass("java.sql.SQLException"); Class.forName("java.sql.SQLException", false, this.defaultScopeGroovyClassLoader);
} }
} }
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -125,13 +125,13 @@ class RestartClassLoaderTests { ...@@ -125,13 +125,13 @@ class RestartClassLoaderTests {
@Test @Test
void loadClassFromReloadableUrl() throws Exception { 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); assertThat(loaded.getClassLoader()).isEqualTo(this.reloadClassLoader);
} }
@Test @Test
void loadClassFromParent() throws Exception { 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()); assertThat(loaded.getClassLoader()).isEqualTo(getClass().getClassLoader());
} }
...@@ -180,7 +180,7 @@ class RestartClassLoaderTests { ...@@ -180,7 +180,7 @@ class RestartClassLoaderTests {
String name = PACKAGE_PATH + "/Sample.class"; String name = PACKAGE_PATH + "/Sample.class";
this.updatedFiles.addFile(name, new ClassLoaderFile(Kind.DELETED, null)); this.updatedFiles.addFile(name, new ClassLoaderFile(Kind.DELETED, null));
assertThatExceptionOfType(ClassNotFoundException.class) assertThatExceptionOfType(ClassNotFoundException.class)
.isThrownBy(() -> this.reloadClassLoader.loadClass(PACKAGE + ".Sample")); .isThrownBy(() -> Class.forName(PACKAGE + ".Sample", false, this.reloadClassLoader));
} }
@Test @Test
...@@ -188,7 +188,7 @@ class RestartClassLoaderTests { ...@@ -188,7 +188,7 @@ class RestartClassLoaderTests {
String name = PACKAGE_PATH + "/Sample.class"; String name = PACKAGE_PATH + "/Sample.class";
this.updatedFiles.addFile(name, new ClassLoaderFile(Kind.MODIFIED, new byte[10])); this.updatedFiles.addFile(name, new ClassLoaderFile(Kind.MODIFIED, new byte[10]));
assertThatExceptionOfType(ClassFormatError.class) assertThatExceptionOfType(ClassFormatError.class)
.isThrownBy(() -> this.reloadClassLoader.loadClass(PACKAGE + ".Sample")); .isThrownBy(() -> Class.forName(PACKAGE + ".Sample", false, this.reloadClassLoader));
} }
@Test @Test
...@@ -196,7 +196,7 @@ class RestartClassLoaderTests { ...@@ -196,7 +196,7 @@ class RestartClassLoaderTests {
String name = PACKAGE_PATH + "/SampleParent.class"; String name = PACKAGE_PATH + "/SampleParent.class";
byte[] bytes = FileCopyUtils.copyToByteArray(getClass().getResourceAsStream("SampleParent.class")); byte[] bytes = FileCopyUtils.copyToByteArray(getClass().getResourceAsStream("SampleParent.class"));
this.updatedFiles.addFile(name, new ClassLoaderFile(Kind.ADDED, bytes)); 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); assertThat(loaded.getClassLoader()).isEqualTo(this.reloadClassLoader);
} }
......
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -43,7 +43,7 @@ class FilteredClassLoaderTests { ...@@ -43,7 +43,7 @@ class FilteredClassLoaderTests {
try (FilteredClassLoader classLoader = new FilteredClassLoader( try (FilteredClassLoader classLoader = new FilteredClassLoader(
FilteredClassLoaderTests.class.getPackage().getName())) { FilteredClassLoaderTests.class.getPackage().getName())) {
assertThatExceptionOfType(ClassNotFoundException.class) assertThatExceptionOfType(ClassNotFoundException.class)
.isThrownBy(() -> classLoader.loadClass(getClass().getName())); .isThrownBy(() -> Class.forName(getClass().getName(), false, classLoader));
} }
} }
...@@ -51,14 +51,14 @@ class FilteredClassLoaderTests { ...@@ -51,14 +51,14 @@ class FilteredClassLoaderTests {
void loadClassWhenFilteredOnClassShouldThrowClassNotFound() throws Exception { void loadClassWhenFilteredOnClassShouldThrowClassNotFound() throws Exception {
try (FilteredClassLoader classLoader = new FilteredClassLoader(FilteredClassLoaderTests.class)) { try (FilteredClassLoader classLoader = new FilteredClassLoader(FilteredClassLoaderTests.class)) {
assertThatExceptionOfType(ClassNotFoundException.class) assertThatExceptionOfType(ClassNotFoundException.class)
.isThrownBy(() -> classLoader.loadClass(getClass().getName())); .isThrownBy(() -> Class.forName(getClass().getName(), false, classLoader));
} }
} }
@Test @Test
void loadClassWhenNotFilteredShouldLoadClass() throws Exception { void loadClassWhenNotFilteredShouldLoadClass() throws Exception {
FilteredClassLoader classLoader = new FilteredClassLoader((className) -> false); 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()); assertThat(loaded.getName()).isEqualTo(getClass().getName());
classLoader.close(); classLoader.close();
} }
......
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -54,7 +54,7 @@ class ReflectionWrapper { ...@@ -54,7 +54,7 @@ class ReflectionWrapper {
protected static Class<?> findClass(ClassLoader classLoader, String name) { protected static Class<?> findClass(ClassLoader classLoader, String name) {
try { try {
return classLoader.loadClass(name); return Class.forName(name, false, classLoader);
} }
catch (ClassNotFoundException ex) { catch (ClassNotFoundException ex) {
throw new IllegalStateException(ex); throw new IllegalStateException(ex);
......
...@@ -32,7 +32,7 @@ public class SampleApplication { ...@@ -32,7 +32,7 @@ public class SampleApplication {
try { try {
ClassLoader classLoader = SampleApplication.class.getClassLoader(); ClassLoader classLoader = SampleApplication.class.getClassLoader();
classLoader.loadClass(className); Class.forName(className, false, classLoader);
return true; return true;
} }
catch (ClassNotFoundException e) { catch (ClassNotFoundException e) {
......
...@@ -559,7 +559,7 @@ public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo { ...@@ -559,7 +559,7 @@ public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo {
Thread thread = Thread.currentThread(); Thread thread = Thread.currentThread();
ClassLoader classLoader = thread.getContextClassLoader(); ClassLoader classLoader = thread.getContextClassLoader();
try { try {
Class<?> startClass = classLoader.loadClass(this.startClassName); Class<?> startClass = Class.forName(this.startClassName, false, classLoader);
Method mainMethod = startClass.getMethod("main", String[].class); Method mainMethod = startClass.getMethod("main", String[].class);
if (!mainMethod.isAccessible()) { if (!mainMethod.isAccessible()) {
mainMethod.setAccessible(true); mainMethod.setAccessible(true);
......
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -96,7 +96,7 @@ class ModifiedClassPathExtension implements InvocationInterceptor { ...@@ -96,7 +96,7 @@ class ModifiedClassPathExtension implements InvocationInterceptor {
} }
private void runTest(ClassLoader classLoader, String testClassName, String testMethodName) throws Throwable { 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); Method testMethod = findMethod(testClass, testMethodName);
LauncherDiscoveryRequest request = new LauncherDiscoveryRequestBuilder(classLoader) LauncherDiscoveryRequest request = new LauncherDiscoveryRequestBuilder(classLoader)
.selectors(DiscoverySelectors.selectMethod(testClass, testMethod)).build(); .selectors(DiscoverySelectors.selectMethod(testClass, testMethod)).build();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment