Skip to content

Commit 95be419

Browse files
committed
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
1 parent 797c30f commit 95be419

File tree

13 files changed

+37
-37
lines changed

13 files changed

+37
-37
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/SessionAutoConfiguration.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -242,7 +242,7 @@ void checkAvailableImplementations() {
242242

243243
private void addCandidateIfAvailable(List<Class<?>> candidates, String type) {
244244
try {
245-
Class<?> candidate = this.classLoader.loadClass(type);
245+
Class<?> candidate = Class.forName(type, false, this.classLoader);
246246
if (candidate != null) {
247247
candidates.add(candidate);
248248
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/ConditionalOnJavaTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -104,7 +104,7 @@ void java8IsTheFallback() throws Exception {
104104

105105
private String getJavaVersion(Class<?>... hiddenClasses) throws Exception {
106106
FilteredClassLoader classLoader = new FilteredClassLoader(hiddenClasses);
107-
Class<?> javaVersionClass = classLoader.loadClass(JavaVersion.class.getName());
107+
Class<?> javaVersionClass = Class.forName(JavaVersion.class.getName(), false, classLoader);
108108
Method getJavaVersionMethod = ReflectionUtils.findMethod(javaVersionClass, "getJavaVersion");
109109
Object javaVersion = ReflectionUtils.invokeMethod(getJavaVersionMethod, null);
110110
classLoader.close();

spring-boot-project/spring-boot-cli/src/main/java/org/springframework/boot/cli/app/SpringApplicationLauncher.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -59,7 +59,7 @@ public SpringApplicationLauncher(ClassLoader classLoader) {
5959
public Object launch(Class<?>[] sources, String[] args) throws Exception {
6060
Map<String, Object> defaultProperties = new HashMap<>();
6161
defaultProperties.put("spring.groovy.template.check-template-location", "false");
62-
Class<?> applicationClass = this.classLoader.loadClass(getSpringApplicationClassName());
62+
Class<?> applicationClass = Class.forName(getSpringApplicationClassName(), false, this.classLoader);
6363
Constructor<?> constructor = applicationClass.getDeclaredConstructor(Class[].class);
6464
constructor.setAccessible(true);
6565
Object application = constructor.newInstance((Object) sources);

spring-boot-project/spring-boot-cli/src/main/java/org/springframework/boot/cli/app/SpringApplicationWebApplicationInitializer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -72,7 +72,7 @@ protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
7272
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
7373
Class<?>[] sourceClasses = new Class<?>[this.sources.length];
7474
for (int i = 0; i < this.sources.length; i++) {
75-
sourceClasses[i] = classLoader.loadClass(this.sources[i]);
75+
sourceClasses[i] = Class.forName(this.sources[i], false, classLoader);
7676
}
7777
return builder.sources(sourceClasses).properties("spring.groovy.template.check-template-location=false");
7878
}

spring-boot-project/spring-boot-cli/src/main/java/org/springframework/boot/cli/archive/PackagedSpringApplicationLauncher.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -73,7 +73,7 @@ private boolean isCliPackaged(Manifest manifest) {
7373
private Class<?>[] loadClasses(ClassLoader classLoader, String[] names) throws ClassNotFoundException {
7474
Class<?>[] classes = new Class<?>[names.length];
7575
for (int i = 0; i < names.length; i++) {
76-
classes[i] = classLoader.loadClass(names[i]);
76+
classes[i] = Class.forName(names[i], false, classLoader);
7777
}
7878
return classes;
7979
}

spring-boot-project/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/ExtendedGroovyClassLoader.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -238,7 +238,7 @@ public Enumeration<URL> getResources(String name) throws IOException {
238238
@Override
239239
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
240240
if (!name.startsWith("java.")) {
241-
this.groovyOnlyClassLoader.loadClass(name);
241+
Class.forName(name, false, this.groovyOnlyClassLoader);
242242
}
243243
return super.loadClass(name, resolve);
244244
}

spring-boot-project/spring-boot-cli/src/test/java/org/springframework/boot/cli/compiler/ExtendedGroovyClassLoaderTests.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -35,27 +35,27 @@ class ExtendedGroovyClassLoaderTests {
3535

3636
@Test
3737
void loadsGroovyFromSameClassLoader() throws Exception {
38-
Class<?> c1 = this.contextClassLoader.loadClass("groovy.lang.Script");
39-
Class<?> c2 = this.defaultScopeGroovyClassLoader.loadClass("groovy.lang.Script");
38+
Class<?> c1 = Class.forName("groovy.lang.Script", false, this.contextClassLoader);
39+
Class<?> c2 = Class.forName("groovy.lang.Script", false, this.defaultScopeGroovyClassLoader);
4040
assertThat(c1.getClassLoader()).isSameAs(c2.getClassLoader());
4141
}
4242

4343
@Test
4444
void filtersNonGroovy() throws Exception {
45-
this.contextClassLoader.loadClass("org.springframework.util.StringUtils");
46-
assertThatExceptionOfType(ClassNotFoundException.class)
47-
.isThrownBy(() -> this.defaultScopeGroovyClassLoader.loadClass("org.springframework.util.StringUtils"));
45+
Class.forName("org.springframework.util.StringUtils", false, this.contextClassLoader);
46+
assertThatExceptionOfType(ClassNotFoundException.class).isThrownBy(
47+
() -> Class.forName("org.springframework.util.StringUtils", false, this.defaultScopeGroovyClassLoader));
4848
}
4949

5050
@Test
5151
void loadsJavaTypes() throws Exception {
52-
this.defaultScopeGroovyClassLoader.loadClass("java.lang.Boolean");
52+
Class.forName("java.lang.Boolean", false, this.defaultScopeGroovyClassLoader);
5353
}
5454

5555
@Test
5656
void loadsSqlTypes() throws Exception {
57-
this.contextClassLoader.loadClass("java.sql.SQLException");
58-
this.defaultScopeGroovyClassLoader.loadClass("java.sql.SQLException");
57+
Class.forName("java.sql.SQLException", false, this.contextClassLoader);
58+
Class.forName("java.sql.SQLException", false, this.defaultScopeGroovyClassLoader);
5959
}
6060

6161
}

spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/restart/classloader/RestartClassLoaderTests.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -125,13 +125,13 @@ void getResourcesFiltersDuplicates() throws Exception {
125125

126126
@Test
127127
void loadClassFromReloadableUrl() throws Exception {
128-
Class<?> loaded = this.reloadClassLoader.loadClass(PACKAGE + ".Sample");
128+
Class<?> loaded = Class.forName(PACKAGE + ".Sample", false, this.reloadClassLoader);
129129
assertThat(loaded.getClassLoader()).isEqualTo(this.reloadClassLoader);
130130
}
131131

132132
@Test
133133
void loadClassFromParent() throws Exception {
134-
Class<?> loaded = this.reloadClassLoader.loadClass(PACKAGE + ".SampleParent");
134+
Class<?> loaded = Class.forName(PACKAGE + ".SampleParent", false, this.reloadClassLoader);
135135
assertThat(loaded.getClassLoader()).isEqualTo(getClass().getClassLoader());
136136
}
137137

@@ -180,23 +180,23 @@ void getDeletedClass() throws Exception {
180180
String name = PACKAGE_PATH + "/Sample.class";
181181
this.updatedFiles.addFile(name, new ClassLoaderFile(Kind.DELETED, null));
182182
assertThatExceptionOfType(ClassNotFoundException.class)
183-
.isThrownBy(() -> this.reloadClassLoader.loadClass(PACKAGE + ".Sample"));
183+
.isThrownBy(() -> Class.forName(PACKAGE + ".Sample", false, this.reloadClassLoader));
184184
}
185185

186186
@Test
187187
void getUpdatedClass() throws Exception {
188188
String name = PACKAGE_PATH + "/Sample.class";
189189
this.updatedFiles.addFile(name, new ClassLoaderFile(Kind.MODIFIED, new byte[10]));
190190
assertThatExceptionOfType(ClassFormatError.class)
191-
.isThrownBy(() -> this.reloadClassLoader.loadClass(PACKAGE + ".Sample"));
191+
.isThrownBy(() -> Class.forName(PACKAGE + ".Sample", false, this.reloadClassLoader));
192192
}
193193

194194
@Test
195195
void getAddedClass() throws Exception {
196196
String name = PACKAGE_PATH + "/SampleParent.class";
197197
byte[] bytes = FileCopyUtils.copyToByteArray(getClass().getResourceAsStream("SampleParent.class"));
198198
this.updatedFiles.addFile(name, new ClassLoaderFile(Kind.ADDED, bytes));
199-
Class<?> loaded = this.reloadClassLoader.loadClass(PACKAGE + ".SampleParent");
199+
Class<?> loaded = Class.forName(PACKAGE + ".SampleParent", false, this.reloadClassLoader);
200200
assertThat(loaded.getClassLoader()).isEqualTo(this.reloadClassLoader);
201201
}
202202

spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/FilteredClassLoaderTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -43,22 +43,22 @@ void loadClassWhenFilteredOnPackageShouldThrowClassNotFound() throws Exception {
4343
try (FilteredClassLoader classLoader = new FilteredClassLoader(
4444
FilteredClassLoaderTests.class.getPackage().getName())) {
4545
assertThatExceptionOfType(ClassNotFoundException.class)
46-
.isThrownBy(() -> classLoader.loadClass(getClass().getName()));
46+
.isThrownBy(() -> Class.forName(getClass().getName(), false, classLoader));
4747
}
4848
}
4949

5050
@Test
5151
void loadClassWhenFilteredOnClassShouldThrowClassNotFound() throws Exception {
5252
try (FilteredClassLoader classLoader = new FilteredClassLoader(FilteredClassLoaderTests.class)) {
5353
assertThatExceptionOfType(ClassNotFoundException.class)
54-
.isThrownBy(() -> classLoader.loadClass(getClass().getName()));
54+
.isThrownBy(() -> Class.forName(getClass().getName(), false, classLoader));
5555
}
5656
}
5757

5858
@Test
5959
void loadClassWhenNotFilteredShouldLoadClass() throws Exception {
6060
FilteredClassLoader classLoader = new FilteredClassLoader((className) -> false);
61-
Class<?> loaded = classLoader.loadClass(getClass().getName());
61+
Class<?> loaded = Class.forName(getClass().getName(), false, classLoader);
6262
assertThat(loaded.getName()).isEqualTo(getClass().getName());
6363
classLoader.close();
6464
}

spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/fieldvalues/javac/ReflectionWrapper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -54,7 +54,7 @@ protected Method findMethod(String name, Class<?>... parameterTypes) {
5454

5555
protected static Class<?> findClass(ClassLoader classLoader, String name) {
5656
try {
57-
return classLoader.loadClass(name);
57+
return Class.forName(name, false, classLoader);
5858
}
5959
catch (ClassNotFoundException ex) {
6060
throw new IllegalStateException(ex);

0 commit comments

Comments
 (0)