Skip to content

Commit 637f697

Browse files
committed
Merge pull request #14483 from durigon
* gh-14483: Use Matcher from pre-compiled Pattern rather than String for replaceAll
2 parents 11016bc + 7aaeefb commit 637f697

File tree

8 files changed

+44
-10
lines changed

8 files changed

+44
-10
lines changed

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/web/WebOperationRequestPredicate.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.util.Collection;
2020
import java.util.Collections;
21+
import java.util.regex.Pattern;
2122

2223
import org.springframework.util.CollectionUtils;
2324
import org.springframework.util.StringUtils;
@@ -30,6 +31,8 @@
3031
*/
3132
public final class WebOperationRequestPredicate {
3233

34+
private static final Pattern PATH_VAR_PATTERN = Pattern.compile("\\{.*?}");
35+
3336
private final String path;
3437

3538
private final String canonicalPath;
@@ -50,7 +53,7 @@ public final class WebOperationRequestPredicate {
5053
public WebOperationRequestPredicate(String path, WebEndpointHttpMethod httpMethod,
5154
Collection<String> consumes, Collection<String> produces) {
5255
this.path = path;
53-
this.canonicalPath = path.replaceAll("\\{.*?}", "{*}");
56+
this.canonicalPath = PATH_VAR_PATTERN.matcher(path).replaceAll("{*}");
5457
this.httpMethod = httpMethod;
5558
this.consumes = consumes;
5659
this.produces = produces;

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/client/RestTemplateExchangeTags.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.io.IOException;
2020
import java.net.URI;
21+
import java.util.regex.Pattern;
2122

2223
import io.micrometer.core.instrument.Tag;
2324

@@ -36,6 +37,8 @@
3637
*/
3738
public final class RestTemplateExchangeTags {
3839

40+
private static final Pattern STRIP_URI_PATTERN = Pattern.compile("^https?://[^/]+/");
41+
3942
private RestTemplateExchangeTags() {
4043
}
4144

@@ -69,7 +72,7 @@ public static Tag uri(String uriTemplate) {
6972
}
7073

7174
private static String stripUri(String uri) {
72-
return uri.replaceAll("^https?://[^/]+/", "");
75+
return STRIP_URI_PATTERN.matcher(uri).replaceAll("");
7376
}
7477

7578
private static String ensureLeadingSlash(String url) {

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/servlet/WebMvcTags.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.boot.actuate.metrics.web.servlet;
1818

19+
import java.util.regex.Pattern;
20+
1921
import javax.servlet.http.HttpServletRequest;
2022
import javax.servlet.http.HttpServletResponse;
2123

@@ -50,6 +52,10 @@ public final class WebMvcTags {
5052

5153
private static final Tag METHOD_UNKNOWN = Tag.of("method", "UNKNOWN");
5254

55+
private static final Pattern TRAILING_SLASH_PATTERN = Pattern.compile("/$");
56+
57+
private static final Pattern MULTIPLE_SLASH_PATTERN = Pattern.compile("//+");
58+
5359
private WebMvcTags() {
5460
}
5561

@@ -124,7 +130,8 @@ private static String getMatchingPattern(HttpServletRequest request) {
124130
private static String getPathInfo(HttpServletRequest request) {
125131
String pathInfo = request.getPathInfo();
126132
String uri = StringUtils.hasText(pathInfo) ? pathInfo : "/";
127-
return uri.replaceAll("//+", "/").replaceAll("/$", "");
133+
uri = MULTIPLE_SLASH_PATTERN.matcher(uri).replaceAll("/");
134+
return TRAILING_SLASH_PATTERN.matcher(uri).replaceAll("");
128135
}
129136

130137
/**

spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/TypeUtils.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.EnumMap;
2323
import java.util.HashMap;
2424
import java.util.Map;
25+
import java.util.regex.Pattern;
2526
import java.util.stream.Collectors;
2627

2728
import javax.annotation.processing.ProcessingEnvironment;
@@ -61,6 +62,8 @@ class TypeUtils {
6162

6263
private static final Map<String, TypeKind> WRAPPER_TO_PRIMITIVE;
6364

65+
private static final Pattern NEW_LINE_PATTERN = Pattern.compile("[\r\n]+");
66+
6467
static {
6568
Map<String, TypeKind> primitives = new HashMap<>();
6669
PRIMITIVE_WRAPPERS.forEach(
@@ -131,7 +134,7 @@ public String getJavaDoc(Element element) {
131134
String javadoc = (element != null)
132135
? this.env.getElementUtils().getDocComment(element) : null;
133136
if (javadoc != null) {
134-
javadoc = javadoc.replaceAll("[\r\n]+", "").trim();
137+
javadoc = NEW_LINE_PATTERN.matcher(javadoc).replaceAll("").trim();
135138
}
136139
return "".equals(javadoc) ? null : javadoc;
137140
}

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/LaunchScriptConfiguration.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.io.Serializable;
2222
import java.util.HashMap;
2323
import java.util.Map;
24+
import java.util.regex.Pattern;
2425

2526
import org.gradle.api.Project;
2627
import org.gradle.api.tasks.bundling.AbstractArchiveTask;
@@ -36,6 +37,10 @@
3637
@SuppressWarnings("serial")
3738
public class LaunchScriptConfiguration implements Serializable {
3839

40+
private static final Pattern WHITE_SPACE_PATTERN = Pattern.compile("\\s+");
41+
42+
private static final Pattern LINE_FEED_PATTERN = Pattern.compile("\n");
43+
3944
private final Map<String, String> properties = new HashMap<>();
4045

4146
private File script;
@@ -134,11 +139,13 @@ public int hashCode() {
134139
}
135140

136141
private String removeLineBreaks(String string) {
137-
return (string != null) ? string.replaceAll("\\s+", " ") : null;
142+
return (string != null) ? WHITE_SPACE_PATTERN.matcher(string).replaceAll(" ")
143+
: null;
138144
}
139145

140146
private String augmentLineBreaks(String string) {
141-
return (string != null) ? string.replaceAll("\n", "\n# ") : null;
147+
return (string != null) ? LINE_FEED_PATTERN.matcher(string).replaceAll("\n# ")
148+
: null;
142149
}
143150

144151
private void putIfMissing(Map<String, String> properties, String key,

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/testkit/GradleBuild.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.ArrayList;
2525
import java.util.Arrays;
2626
import java.util.List;
27+
import java.util.regex.Pattern;
2728

2829
import javax.xml.xpath.XPath;
2930
import javax.xml.xpath.XPathExpression;
@@ -50,6 +51,9 @@
5051
*/
5152
public class GradleBuild implements TestRule {
5253

54+
private static final Pattern GRADLE_VERSION_PATTERN = Pattern
55+
.compile("\\[Gradle .+\\]");
56+
5357
private final TemporaryFolder temp = new TemporaryFolder();
5458

5559
private File projectDir;
@@ -95,7 +99,7 @@ private URL getScriptForTestMethod(Description description) {
9599
}
96100

97101
private String removeGradleVersion(String methodName) {
98-
return methodName.replaceAll("\\[Gradle .+\\]", "").trim();
102+
return GRADLE_VERSION_PATTERN.matcher(methodName).replaceAll("").trim();
99103
}
100104

101105
private URL getScriptForTestClass(Class<?> testClass) {

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/RepackageMojo.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.List;
2323
import java.util.Properties;
2424
import java.util.Set;
25+
import java.util.regex.Pattern;
2526

2627
import org.apache.maven.artifact.Artifact;
2728
import org.apache.maven.model.Dependency;
@@ -62,6 +63,8 @@
6263
@Mojo(name = "repackage", defaultPhase = LifecyclePhase.PACKAGE, requiresProject = true, threadSafe = true, requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME, requiresDependencyCollection = ResolutionScope.COMPILE_PLUS_RUNTIME)
6364
public class RepackageMojo extends AbstractDependencyFilterMojo {
6465

66+
private static final Pattern WHITE_SPACE_PATTERN = Pattern.compile("\\s+");
67+
6568
/**
6669
* The Maven project.
6770
* @since 1.0
@@ -312,7 +315,8 @@ private Properties buildLaunchScriptProperties() {
312315
}
313316

314317
private String removeLineBreaks(String description) {
315-
return (description != null) ? description.replaceAll("\\s+", " ") : null;
318+
return (description != null)
319+
? WHITE_SPACE_PATTERN.matcher(description).replaceAll(" ") : null;
316320
}
317321

318322
private void putIfMissing(Properties properties, String key,

spring-boot-tests/spring-boot-integration-tests/spring-boot-devtools-tests/src/test/java/org/springframework/boot/devtools/tests/JvmLauncher.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.ArrayList;
2222
import java.util.Arrays;
2323
import java.util.List;
24+
import java.util.regex.Pattern;
2425

2526
import org.junit.rules.TestRule;
2627
import org.junit.runner.Description;
@@ -36,12 +37,14 @@
3637
*/
3738
class JvmLauncher implements TestRule {
3839

40+
private static final Pattern NON_ALPHABET_PATTERN = Pattern.compile("[^A-Za-z]+");
41+
3942
private File outputDirectory;
4043

4144
@Override
4245
public Statement apply(Statement base, Description description) {
43-
this.outputDirectory = new File("target/output/"
44-
+ description.getMethodName().replaceAll("[^A-Za-z]+", ""));
46+
this.outputDirectory = new File("target/output/" + NON_ALPHABET_PATTERN
47+
.matcher(description.getMethodName()).replaceAll(""));
4548
this.outputDirectory.mkdirs();
4649
return base;
4750
}

0 commit comments

Comments
 (0)