Skip to content

Commit 7a25e4e

Browse files
dreis2211philwebb
authored andcommitted
Avoid illegal reflective access in CLI tests
See gh-26930
1 parent 093e7f7 commit 7a25e4e

File tree

3 files changed

+13
-20
lines changed

3 files changed

+13
-20
lines changed

spring-boot-project/spring-boot-cli/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ dependencies {
5656

5757
loader(project(":spring-boot-project:spring-boot-tools:spring-boot-loader"))
5858

59+
testCompileOnly("org.apache.tomcat.embed:tomcat-embed-core")
5960
testImplementation(project(":spring-boot-project:spring-boot"))
6061
testImplementation(project(":spring-boot-project:spring-boot-tools:spring-boot-test-support"))
6162
testImplementation(project(":spring-boot-project:spring-boot-test"))

spring-boot-project/spring-boot-cli/src/test/java/org/springframework/boot/cli/CliTester.java

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 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.
@@ -22,9 +22,7 @@
2222
import java.io.IOException;
2323
import java.io.InputStream;
2424
import java.io.InputStreamReader;
25-
import java.lang.reflect.Field;
2625
import java.net.URI;
27-
import java.net.URL;
2826
import java.nio.file.Files;
2927
import java.util.ArrayList;
3028
import java.util.List;
@@ -127,7 +125,6 @@ public File getTemp() {
127125
}
128126

129127
private <T extends OptionParsingCommand> Future<T> submitCommand(T command, String... args) {
130-
clearUrlHandler();
131128
final String[] sources = getSources(args);
132129
return Executors.newSingleThreadExecutor().submit(() -> {
133130
ClassLoader loader = Thread.currentThread().getContextClassLoader();
@@ -152,21 +149,6 @@ private <T extends OptionParsingCommand> Future<T> submitCommand(T command, Stri
152149
});
153150
}
154151

155-
/**
156-
* The TomcatURLStreamHandlerFactory fails if the factory is already set, use
157-
* reflection to reset it.
158-
*/
159-
private void clearUrlHandler() {
160-
try {
161-
Field field = URL.class.getDeclaredField("factory");
162-
field.setAccessible(true);
163-
field.set(null, null);
164-
}
165-
catch (Exception ex) {
166-
throw new IllegalStateException(ex);
167-
}
168-
}
169-
170152
protected String[] getSources(String... args) {
171153
final String[] sources = new String[args.length];
172154
for (int i = 0; i < args.length; i++) {

spring-boot-project/spring-boot-cli/src/test/java/org/springframework/boot/cli/CliTesterSpringApplication.java

Lines changed: 11 additions & 1 deletion
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-2021 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.
@@ -16,9 +16,12 @@
1616

1717
package org.springframework.boot.cli;
1818

19+
import org.apache.catalina.webresources.TomcatURLStreamHandlerFactory;
20+
1921
import org.springframework.boot.SpringApplication;
2022
import org.springframework.boot.web.context.WebServerPortFileWriter;
2123
import org.springframework.context.ConfigurableApplicationContext;
24+
import org.springframework.util.ClassUtils;
2225

2326
/**
2427
* Custom {@link SpringApplication} used by {@link CliTester}.
@@ -27,6 +30,13 @@
2730
*/
2831
public class CliTesterSpringApplication extends SpringApplication {
2932

33+
static {
34+
if (ClassUtils.isPresent("org.apache.catalina.webresources.TomcatURLStreamHandlerFactory",
35+
CliTesterSpringApplication.class.getClassLoader())) {
36+
TomcatURLStreamHandlerFactory.disable();
37+
}
38+
}
39+
3040
public CliTesterSpringApplication(Class<?>... sources) {
3141
super(sources);
3242
}

0 commit comments

Comments
 (0)