Skip to content

Commit 2f903c8

Browse files
wilkinsonasnicoll
authored andcommitted
Make ModifiedClassPathRunner compatible with JDK 9
1 parent 781cbc4 commit 2f903c8

File tree

2 files changed

+25
-28
lines changed

2 files changed

+25
-28
lines changed

spring-boot-tools/spring-boot-test-support/pom.xml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -106,17 +106,5 @@
106106
<scope>test</scope>
107107
</dependency>
108108
</dependencies>
109-
<build>
110-
<plugins>
111-
<plugin>
112-
<groupId>org.apache.maven.plugins</groupId>
113-
<artifactId>maven-surefire-plugin</artifactId>
114-
<configuration>
115-
<argLine>--add-opens=java.base/jdk.internal.loader=ALL-UNNAMED</argLine>
116-
</configuration>
117-
118-
</plugin>
119-
</plugins>
120-
</build>
121109

122110
</project>

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

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

1919
import java.io.File;
2020
import java.lang.annotation.Annotation;
21-
import java.lang.reflect.Field;
21+
import java.lang.management.ManagementFactory;
2222
import java.lang.reflect.Method;
2323
import java.net.URL;
2424
import java.net.URLClassLoader;
@@ -28,6 +28,7 @@
2828
import java.util.List;
2929
import java.util.jar.Attributes;
3030
import java.util.jar.JarFile;
31+
import java.util.stream.Stream;
3132

3233
import org.apache.maven.repository.internal.MavenRepositorySystemUtils;
3334
import org.eclipse.aether.DefaultRepositorySystemSession;
@@ -102,39 +103,47 @@ private URLClassLoader createTestClassLoader(Class<?> testClass) throws Exceptio
102103

103104
private URL[] extractUrls(ClassLoader classLoader) throws Exception {
104105
List<URL> extractedUrls = new ArrayList<URL>();
105-
for (URL url : doExtractUrls(classLoader)) {
106+
doExtractUrls(classLoader).forEach((URL url) -> {
106107
if (isSurefireBooterJar(url)) {
107108
extractedUrls.addAll(extractUrlsFromManifestClassPath(url));
108109
}
109110
else {
110111
extractedUrls.add(url);
111112
}
112-
}
113+
});
113114
return extractedUrls.toArray(new URL[extractedUrls.size()]);
114115
}
115116

116-
private URL[] doExtractUrls(ClassLoader classLoader) throws Exception {
117+
private Stream<URL> doExtractUrls(ClassLoader classLoader) throws Exception {
117118
if (classLoader instanceof URLClassLoader) {
118-
return ((URLClassLoader) classLoader).getURLs();
119+
return Stream.of(((URLClassLoader) classLoader).getURLs());
120+
}
121+
return Stream.of(ManagementFactory.getRuntimeMXBean().getClassPath()
122+
.split(File.pathSeparator)).map(this::toURL);
123+
}
124+
125+
private URL toURL(String entry) {
126+
try {
127+
return new File(entry).toURI().toURL();
128+
}
129+
catch (Exception ex) {
130+
throw new IllegalArgumentException(ex);
119131
}
120-
Field ucpField = classLoader.getClass().getDeclaredField("ucp");
121-
ucpField.setAccessible(true);
122-
Object ucp = ucpField.get(classLoader);
123-
Field pathField = ucp.getClass().getDeclaredField("path");
124-
pathField.setAccessible(true);
125-
@SuppressWarnings("unchecked")
126-
List<URL> path = (List<URL>) pathField.get(ucp);
127-
return path.toArray(new URL[path.size()]);
128132
}
129133

130134
private boolean isSurefireBooterJar(URL url) {
131135
return url.getPath().contains("surefirebooter");
132136
}
133137

134-
private List<URL> extractUrlsFromManifestClassPath(URL booterJar) throws Exception {
138+
private List<URL> extractUrlsFromManifestClassPath(URL booterJar) {
135139
List<URL> urls = new ArrayList<URL>();
136-
for (String entry : getClassPath(booterJar)) {
137-
urls.add(new URL(entry));
140+
try {
141+
for (String entry : getClassPath(booterJar)) {
142+
urls.add(new URL(entry));
143+
}
144+
}
145+
catch (Exception ex) {
146+
throw new RuntimeException(ex);
138147
}
139148
return urls;
140149
}

0 commit comments

Comments
 (0)