Skip to content

Commit 7a82660

Browse files
wilkinsonasnicoll
authored andcommitted
Make ModifiedClassPathRunner compatible with JDK 9
Closes gh-10020
1 parent a07833f commit 7a82660

File tree

1 file changed

+31
-7
lines changed

1 file changed

+31
-7
lines changed

spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/runner/classpath/ModifiedClassPathRunner.java

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.io.File;
2020
import java.lang.annotation.Annotation;
21+
import java.lang.management.ManagementFactory;
2122
import java.lang.reflect.Method;
2223
import java.net.URL;
2324
import java.net.URLClassLoader;
@@ -27,6 +28,7 @@
2728
import java.util.List;
2829
import java.util.jar.Attributes;
2930
import java.util.jar.JarFile;
31+
import java.util.stream.Stream;
3032

3133
import org.apache.maven.repository.internal.MavenRepositorySystemUtils;
3234
import org.eclipse.aether.DefaultRepositorySystemSession;
@@ -87,33 +89,55 @@ protected Object createTest() throws Exception {
8789
}
8890

8991
private URLClassLoader createTestClassLoader(Class<?> testClass) throws Exception {
90-
URLClassLoader classLoader = (URLClassLoader) this.getClass().getClassLoader();
92+
ClassLoader classLoader = this.getClass().getClassLoader();
9193
return new ModifiedClassPathClassLoader(
9294
processUrls(extractUrls(classLoader), testClass), classLoader.getParent(),
9395
classLoader);
9496
}
9597

96-
private URL[] extractUrls(URLClassLoader classLoader) throws Exception {
98+
private URL[] extractUrls(ClassLoader classLoader) throws Exception {
9799
List<URL> extractedUrls = new ArrayList<>();
98-
for (URL url : classLoader.getURLs()) {
100+
doExtractUrls(classLoader).forEach((URL url) -> {
99101
if (isSurefireBooterJar(url)) {
100102
extractedUrls.addAll(extractUrlsFromManifestClassPath(url));
101103
}
102104
else {
103105
extractedUrls.add(url);
104106
}
105-
}
107+
});
106108
return extractedUrls.toArray(new URL[extractedUrls.size()]);
107109
}
108110

111+
private Stream<URL> doExtractUrls(ClassLoader classLoader) throws Exception {
112+
if (classLoader instanceof URLClassLoader) {
113+
return Stream.of(((URLClassLoader) classLoader).getURLs());
114+
}
115+
return Stream.of(ManagementFactory.getRuntimeMXBean().getClassPath()
116+
.split(File.pathSeparator)).map(this::toURL);
117+
}
118+
119+
private URL toURL(String entry) {
120+
try {
121+
return new File(entry).toURI().toURL();
122+
}
123+
catch (Exception ex) {
124+
throw new IllegalArgumentException(ex);
125+
}
126+
}
127+
109128
private boolean isSurefireBooterJar(URL url) {
110129
return url.getPath().contains("surefirebooter");
111130
}
112131

113-
private List<URL> extractUrlsFromManifestClassPath(URL booterJar) throws Exception {
132+
private List<URL> extractUrlsFromManifestClassPath(URL booterJar) {
114133
List<URL> urls = new ArrayList<>();
115-
for (String entry : getClassPath(booterJar)) {
116-
urls.add(new URL(entry));
134+
try {
135+
for (String entry : getClassPath(booterJar)) {
136+
urls.add(new URL(entry));
137+
}
138+
}
139+
catch (Exception ex) {
140+
throw new RuntimeException(ex);
117141
}
118142
return urls;
119143
}

0 commit comments

Comments
 (0)