Skip to content

Commit e044817

Browse files
committed
Migrate remaining use of ClassLoader.loadClass to Class.forName
Closes gh-19824
1 parent 820c671 commit e044817

File tree

6 files changed

+13
-12
lines changed

6 files changed

+13
-12
lines changed

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

Lines changed: 3 additions & 3 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.
@@ -93,7 +93,7 @@ public DependencyCustomizer ifAnyMissingClasses(String... classNames) {
9393
protected boolean canAdd() {
9494
for (String className : classNames) {
9595
try {
96-
DependencyCustomizer.this.loader.loadClass(className);
96+
Class.forName(className, false, DependencyCustomizer.this.loader);
9797
}
9898
catch (Exception ex) {
9999
return true;
@@ -116,7 +116,7 @@ public DependencyCustomizer ifAllMissingClasses(String... classNames) {
116116
protected boolean canAdd() {
117117
for (String className : classNames) {
118118
try {
119-
DependencyCustomizer.this.loader.loadClass(className);
119+
Class.forName(className, false, DependencyCustomizer.this.loader);
120120
return false;
121121
}
122122
catch (Exception ex) {

spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/RestartLauncher.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.
@@ -44,7 +44,7 @@ class RestartLauncher extends Thread {
4444
@Override
4545
public void run() {
4646
try {
47-
Class<?> mainClass = getContextClassLoader().loadClass(this.mainClassName);
47+
Class<?> mainClass = Class.forName(this.mainClassName, false, getContextClassLoader());
4848
Method mainMethod = mainClass.getDeclaredMethod("main", String[].class);
4949
mainMethod.invoke(null, new Object[] { this.args });
5050
}

spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/classloader/RestartClassLoader.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.
@@ -141,7 +141,7 @@ public Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundExce
141141
loadedClass = findClass(name);
142142
}
143143
catch (ClassNotFoundException ex) {
144-
loadedClass = getParent().loadClass(name);
144+
loadedClass = Class.forName(name, false, getParent());
145145
}
146146
}
147147
if (resolve) {

spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/MainMethodRunner.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public MainMethodRunner(String mainClass, String[] args) {
4343
}
4444

4545
public void run() throws Exception {
46-
Class<?> mainClass = Thread.currentThread().getContextClassLoader().loadClass(this.mainClassName);
46+
Class<?> mainClass = Class.forName(this.mainClassName, false, Thread.currentThread().getContextClassLoader());
4747
Method mainMethod = mainClass.getDeclaredMethod("main", String[].class);
4848
mainMethod.setAccessible(true);
4949
mainMethod.invoke(null, new Object[] { this.args });

spring-boot-project/spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/classpath/ModifiedClassPathClassLoader.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.
@@ -76,7 +76,7 @@ final class ModifiedClassPathClassLoader extends URLClassLoader {
7676
@Override
7777
public Class<?> loadClass(String name) throws ClassNotFoundException {
7878
if (name.startsWith("org.junit") || name.startsWith("org.hamcrest")) {
79-
return this.junitLoader.loadClass(name);
79+
return Class.forName(name, false, this.junitLoader);
8080
}
8181
return super.loadClass(name);
8282
}

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/diagnostics/analyzer/NoSuchMethodFailureAnalyzer.java

Lines changed: 3 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.
@@ -106,7 +106,8 @@ private List<URL> findCandidates(String className) {
106106

107107
private URL getActual(String className) {
108108
try {
109-
return getClass().getClassLoader().loadClass(className).getProtectionDomain().getCodeSource().getLocation();
109+
return Class.forName(className, false, getClass().getClassLoader()).getProtectionDomain().getCodeSource()
110+
.getLocation();
110111
}
111112
catch (Throwable ex) {
112113
return null;

0 commit comments

Comments
 (0)