|
18 | 18 |
|
19 | 19 | import java.io.File;
|
20 | 20 | import java.lang.annotation.Annotation;
|
21 |
| -import java.lang.reflect.Field; |
| 21 | +import java.lang.management.ManagementFactory; |
22 | 22 | import java.lang.reflect.Method;
|
23 | 23 | import java.net.URL;
|
24 | 24 | import java.net.URLClassLoader;
|
|
28 | 28 | import java.util.List;
|
29 | 29 | import java.util.jar.Attributes;
|
30 | 30 | import java.util.jar.JarFile;
|
| 31 | +import java.util.stream.Stream; |
31 | 32 |
|
32 | 33 | import org.apache.maven.repository.internal.MavenRepositorySystemUtils;
|
33 | 34 | import org.eclipse.aether.DefaultRepositorySystemSession;
|
@@ -102,39 +103,47 @@ private URLClassLoader createTestClassLoader(Class<?> testClass) throws Exceptio
|
102 | 103 |
|
103 | 104 | private URL[] extractUrls(ClassLoader classLoader) throws Exception {
|
104 | 105 | List<URL> extractedUrls = new ArrayList<URL>();
|
105 |
| - for (URL url : doExtractUrls(classLoader)) { |
| 106 | + doExtractUrls(classLoader).forEach((URL url) -> { |
106 | 107 | if (isSurefireBooterJar(url)) {
|
107 | 108 | extractedUrls.addAll(extractUrlsFromManifestClassPath(url));
|
108 | 109 | }
|
109 | 110 | else {
|
110 | 111 | extractedUrls.add(url);
|
111 | 112 | }
|
112 |
| - } |
| 113 | + }); |
113 | 114 | return extractedUrls.toArray(new URL[extractedUrls.size()]);
|
114 | 115 | }
|
115 | 116 |
|
116 |
| - private URL[] doExtractUrls(ClassLoader classLoader) throws Exception { |
| 117 | + private Stream<URL> doExtractUrls(ClassLoader classLoader) throws Exception { |
117 | 118 | 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); |
119 | 131 | }
|
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()]); |
128 | 132 | }
|
129 | 133 |
|
130 | 134 | private boolean isSurefireBooterJar(URL url) {
|
131 | 135 | return url.getPath().contains("surefirebooter");
|
132 | 136 | }
|
133 | 137 |
|
134 |
| - private List<URL> extractUrlsFromManifestClassPath(URL booterJar) throws Exception { |
| 138 | + private List<URL> extractUrlsFromManifestClassPath(URL booterJar) { |
135 | 139 | 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); |
138 | 147 | }
|
139 | 148 | return urls;
|
140 | 149 | }
|
|
0 commit comments