Skip to content

Commit 4585bc8

Browse files
committed
Restore RestartClassLoader's Java 6 compatibility
Closes gh-12826
1 parent 6047a2c commit 4585bc8

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/classloader/RestartClassLoader.java

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.boot.devtools.restart.classloader;
1818

1919
import java.io.IOException;
20+
import java.lang.reflect.Method;
2021
import java.net.MalformedURLException;
2122
import java.net.URL;
2223
import java.net.URLClassLoader;
@@ -30,6 +31,7 @@
3031
import org.springframework.boot.devtools.restart.classloader.ClassLoaderFile.Kind;
3132
import org.springframework.core.SmartClassLoader;
3233
import org.springframework.util.Assert;
34+
import org.springframework.util.ReflectionUtils;
3335

3436
/**
3537
* Disposable {@link ClassLoader} used to support application restarting. Provides parent
@@ -41,6 +43,8 @@
4143
*/
4244
public class RestartClassLoader extends URLClassLoader implements SmartClassLoader {
4345

46+
private final ClassLoadingLockSupplier classLoadingLockSupplier;
47+
4448
private final Log logger;
4549

4650
private final ClassLoaderFileRepository updatedFiles;
@@ -85,6 +89,11 @@ public RestartClassLoader(ClassLoader parent, URL[] urls,
8589
if (logger.isDebugEnabled()) {
8690
logger.debug("Created RestartClassLoader " + toString());
8791
}
92+
Method classLoadingLockMethod = ReflectionUtils.findMethod(ClassLoader.class,
93+
"getClassLoadingLock", String.class);
94+
this.classLoadingLockSupplier = classLoadingLockMethod != null
95+
? new StandardClassLoadingLockSupplier()
96+
: new Java6ClassLoadingLockSupplier();
8897
}
8998

9099
@Override
@@ -142,7 +151,7 @@ public Class<?> loadClass(String name, boolean resolve)
142151
if (file != null && file.getKind() == Kind.DELETED) {
143152
throw new ClassNotFoundException(name);
144153
}
145-
synchronized (getClassLoadingLock(name)) {
154+
synchronized (this.classLoadingLockSupplier.getClassLoadingLock(this, name)) {
146155
Class<?> loadedClass = findLoadedClass(name);
147156
if (loadedClass == null) {
148157
try {
@@ -232,4 +241,32 @@ public E nextElement() {
232241

233242
}
234243

244+
private interface ClassLoadingLockSupplier {
245+
246+
Object getClassLoadingLock(RestartClassLoader classLoader, String className);
247+
248+
}
249+
250+
private static final class Java6ClassLoadingLockSupplier
251+
implements ClassLoadingLockSupplier {
252+
253+
@Override
254+
public Object getClassLoadingLock(RestartClassLoader classLoader,
255+
String className) {
256+
return classLoader;
257+
}
258+
259+
}
260+
261+
private static final class StandardClassLoadingLockSupplier
262+
implements ClassLoadingLockSupplier {
263+
264+
@Override
265+
public Object getClassLoadingLock(RestartClassLoader classLoader,
266+
String className) {
267+
return classLoader.getClassLoadingLock(className);
268+
}
269+
270+
}
271+
235272
}

0 commit comments

Comments
 (0)