Commit 7aaeefbc authored by durigon's avatar durigon Committed by Andy Wilkinson

Use Matcher from pre-compiled Pattern rather than String for replaceAll

Closes gh-14483
parent 11016bc7
...@@ -18,6 +18,7 @@ package org.springframework.boot.actuate.endpoint.web; ...@@ -18,6 +18,7 @@ package org.springframework.boot.actuate.endpoint.web;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.regex.Pattern;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
...@@ -30,6 +31,8 @@ import org.springframework.util.StringUtils; ...@@ -30,6 +31,8 @@ import org.springframework.util.StringUtils;
*/ */
public final class WebOperationRequestPredicate { public final class WebOperationRequestPredicate {
private static final Pattern PATH_VAR_PATTERN = Pattern.compile("\\{.*?}");
private final String path; private final String path;
private final String canonicalPath; private final String canonicalPath;
...@@ -50,7 +53,7 @@ public final class WebOperationRequestPredicate { ...@@ -50,7 +53,7 @@ public final class WebOperationRequestPredicate {
public WebOperationRequestPredicate(String path, WebEndpointHttpMethod httpMethod, public WebOperationRequestPredicate(String path, WebEndpointHttpMethod httpMethod,
Collection<String> consumes, Collection<String> produces) { Collection<String> consumes, Collection<String> produces) {
this.path = path; this.path = path;
this.canonicalPath = path.replaceAll("\\{.*?}", "{*}"); this.canonicalPath = PATH_VAR_PATTERN.matcher(path).replaceAll("{*}");
this.httpMethod = httpMethod; this.httpMethod = httpMethod;
this.consumes = consumes; this.consumes = consumes;
this.produces = produces; this.produces = produces;
......
...@@ -18,6 +18,7 @@ package org.springframework.boot.actuate.metrics.web.client; ...@@ -18,6 +18,7 @@ package org.springframework.boot.actuate.metrics.web.client;
import java.io.IOException; import java.io.IOException;
import java.net.URI; import java.net.URI;
import java.util.regex.Pattern;
import io.micrometer.core.instrument.Tag; import io.micrometer.core.instrument.Tag;
...@@ -36,6 +37,8 @@ import org.springframework.web.client.RestTemplate; ...@@ -36,6 +37,8 @@ import org.springframework.web.client.RestTemplate;
*/ */
public final class RestTemplateExchangeTags { public final class RestTemplateExchangeTags {
private static final Pattern STRIP_URI_PATTERN = Pattern.compile("^https?://[^/]+/");
private RestTemplateExchangeTags() { private RestTemplateExchangeTags() {
} }
...@@ -69,7 +72,7 @@ public final class RestTemplateExchangeTags { ...@@ -69,7 +72,7 @@ public final class RestTemplateExchangeTags {
} }
private static String stripUri(String uri) { private static String stripUri(String uri) {
return uri.replaceAll("^https?://[^/]+/", ""); return STRIP_URI_PATTERN.matcher(uri).replaceAll("");
} }
private static String ensureLeadingSlash(String url) { private static String ensureLeadingSlash(String url) {
......
...@@ -16,6 +16,8 @@ ...@@ -16,6 +16,8 @@
package org.springframework.boot.actuate.metrics.web.servlet; package org.springframework.boot.actuate.metrics.web.servlet;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
...@@ -50,6 +52,10 @@ public final class WebMvcTags { ...@@ -50,6 +52,10 @@ public final class WebMvcTags {
private static final Tag METHOD_UNKNOWN = Tag.of("method", "UNKNOWN"); private static final Tag METHOD_UNKNOWN = Tag.of("method", "UNKNOWN");
private static final Pattern TRAILING_SLASH_PATTERN = Pattern.compile("/$");
private static final Pattern MULTIPLE_SLASH_PATTERN = Pattern.compile("//+");
private WebMvcTags() { private WebMvcTags() {
} }
...@@ -124,7 +130,8 @@ public final class WebMvcTags { ...@@ -124,7 +130,8 @@ public final class WebMvcTags {
private static String getPathInfo(HttpServletRequest request) { private static String getPathInfo(HttpServletRequest request) {
String pathInfo = request.getPathInfo(); String pathInfo = request.getPathInfo();
String uri = StringUtils.hasText(pathInfo) ? pathInfo : "/"; String uri = StringUtils.hasText(pathInfo) ? pathInfo : "/";
return uri.replaceAll("//+", "/").replaceAll("/$", ""); uri = MULTIPLE_SLASH_PATTERN.matcher(uri).replaceAll("/");
return TRAILING_SLASH_PATTERN.matcher(uri).replaceAll("");
} }
/** /**
......
...@@ -22,6 +22,7 @@ import java.util.Collections; ...@@ -22,6 +22,7 @@ import java.util.Collections;
import java.util.EnumMap; import java.util.EnumMap;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.regex.Pattern;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import javax.annotation.processing.ProcessingEnvironment; import javax.annotation.processing.ProcessingEnvironment;
...@@ -61,6 +62,8 @@ class TypeUtils { ...@@ -61,6 +62,8 @@ class TypeUtils {
private static final Map<String, TypeKind> WRAPPER_TO_PRIMITIVE; private static final Map<String, TypeKind> WRAPPER_TO_PRIMITIVE;
private static final Pattern NEW_LINE_PATTERN = Pattern.compile("[\r\n]+");
static { static {
Map<String, TypeKind> primitives = new HashMap<>(); Map<String, TypeKind> primitives = new HashMap<>();
PRIMITIVE_WRAPPERS.forEach( PRIMITIVE_WRAPPERS.forEach(
...@@ -131,7 +134,7 @@ class TypeUtils { ...@@ -131,7 +134,7 @@ class TypeUtils {
String javadoc = (element != null) String javadoc = (element != null)
? this.env.getElementUtils().getDocComment(element) : null; ? this.env.getElementUtils().getDocComment(element) : null;
if (javadoc != null) { if (javadoc != null) {
javadoc = javadoc.replaceAll("[\r\n]+", "").trim(); javadoc = NEW_LINE_PATTERN.matcher(javadoc).replaceAll("").trim();
} }
return "".equals(javadoc) ? null : javadoc; return "".equals(javadoc) ? null : javadoc;
} }
......
...@@ -21,6 +21,7 @@ import java.io.IOException; ...@@ -21,6 +21,7 @@ import java.io.IOException;
import java.io.Serializable; import java.io.Serializable;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.regex.Pattern;
import org.gradle.api.Project; import org.gradle.api.Project;
import org.gradle.api.tasks.bundling.AbstractArchiveTask; import org.gradle.api.tasks.bundling.AbstractArchiveTask;
...@@ -36,6 +37,10 @@ import org.springframework.boot.loader.tools.FileUtils; ...@@ -36,6 +37,10 @@ import org.springframework.boot.loader.tools.FileUtils;
@SuppressWarnings("serial") @SuppressWarnings("serial")
public class LaunchScriptConfiguration implements Serializable { public class LaunchScriptConfiguration implements Serializable {
private static final Pattern WHITE_SPACE_PATTERN = Pattern.compile("\\s+");
private static final Pattern LINE_FEED_PATTERN = Pattern.compile("\n");
private final Map<String, String> properties = new HashMap<>(); private final Map<String, String> properties = new HashMap<>();
private File script; private File script;
...@@ -134,11 +139,13 @@ public class LaunchScriptConfiguration implements Serializable { ...@@ -134,11 +139,13 @@ public class LaunchScriptConfiguration implements Serializable {
} }
private String removeLineBreaks(String string) { private String removeLineBreaks(String string) {
return (string != null) ? string.replaceAll("\\s+", " ") : null; return (string != null) ? WHITE_SPACE_PATTERN.matcher(string).replaceAll(" ")
: null;
} }
private String augmentLineBreaks(String string) { private String augmentLineBreaks(String string) {
return (string != null) ? string.replaceAll("\n", "\n# ") : null; return (string != null) ? LINE_FEED_PATTERN.matcher(string).replaceAll("\n# ")
: null;
} }
private void putIfMissing(Map<String, String> properties, String key, private void putIfMissing(Map<String, String> properties, String key,
......
...@@ -24,6 +24,7 @@ import java.net.URL; ...@@ -24,6 +24,7 @@ import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.regex.Pattern;
import javax.xml.xpath.XPath; import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpression; import javax.xml.xpath.XPathExpression;
...@@ -50,6 +51,9 @@ import org.springframework.util.FileCopyUtils; ...@@ -50,6 +51,9 @@ import org.springframework.util.FileCopyUtils;
*/ */
public class GradleBuild implements TestRule { public class GradleBuild implements TestRule {
private static final Pattern GRADLE_VERSION_PATTERN = Pattern
.compile("\\[Gradle .+\\]");
private final TemporaryFolder temp = new TemporaryFolder(); private final TemporaryFolder temp = new TemporaryFolder();
private File projectDir; private File projectDir;
...@@ -95,7 +99,7 @@ public class GradleBuild implements TestRule { ...@@ -95,7 +99,7 @@ public class GradleBuild implements TestRule {
} }
private String removeGradleVersion(String methodName) { private String removeGradleVersion(String methodName) {
return methodName.replaceAll("\\[Gradle .+\\]", "").trim(); return GRADLE_VERSION_PATTERN.matcher(methodName).replaceAll("").trim();
} }
private URL getScriptForTestClass(Class<?> testClass) { private URL getScriptForTestClass(Class<?> testClass) {
......
...@@ -22,6 +22,7 @@ import java.util.ArrayList; ...@@ -22,6 +22,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Properties; import java.util.Properties;
import java.util.Set; import java.util.Set;
import java.util.regex.Pattern;
import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.Artifact;
import org.apache.maven.model.Dependency; import org.apache.maven.model.Dependency;
...@@ -62,6 +63,8 @@ import org.springframework.boot.loader.tools.Repackager.MainClassTimeoutWarningL ...@@ -62,6 +63,8 @@ import org.springframework.boot.loader.tools.Repackager.MainClassTimeoutWarningL
@Mojo(name = "repackage", defaultPhase = LifecyclePhase.PACKAGE, requiresProject = true, threadSafe = true, requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME, requiresDependencyCollection = ResolutionScope.COMPILE_PLUS_RUNTIME) @Mojo(name = "repackage", defaultPhase = LifecyclePhase.PACKAGE, requiresProject = true, threadSafe = true, requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME, requiresDependencyCollection = ResolutionScope.COMPILE_PLUS_RUNTIME)
public class RepackageMojo extends AbstractDependencyFilterMojo { public class RepackageMojo extends AbstractDependencyFilterMojo {
private static final Pattern WHITE_SPACE_PATTERN = Pattern.compile("\\s+");
/** /**
* The Maven project. * The Maven project.
* @since 1.0 * @since 1.0
...@@ -312,7 +315,8 @@ public class RepackageMojo extends AbstractDependencyFilterMojo { ...@@ -312,7 +315,8 @@ public class RepackageMojo extends AbstractDependencyFilterMojo {
} }
private String removeLineBreaks(String description) { private String removeLineBreaks(String description) {
return (description != null) ? description.replaceAll("\\s+", " ") : null; return (description != null)
? WHITE_SPACE_PATTERN.matcher(description).replaceAll(" ") : null;
} }
private void putIfMissing(Properties properties, String key, private void putIfMissing(Properties properties, String key,
......
...@@ -21,6 +21,7 @@ import java.io.IOException; ...@@ -21,6 +21,7 @@ import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.regex.Pattern;
import org.junit.rules.TestRule; import org.junit.rules.TestRule;
import org.junit.runner.Description; import org.junit.runner.Description;
...@@ -36,12 +37,14 @@ import org.springframework.util.StringUtils; ...@@ -36,12 +37,14 @@ import org.springframework.util.StringUtils;
*/ */
class JvmLauncher implements TestRule { class JvmLauncher implements TestRule {
private static final Pattern NON_ALPHABET_PATTERN = Pattern.compile("[^A-Za-z]+");
private File outputDirectory; private File outputDirectory;
@Override @Override
public Statement apply(Statement base, Description description) { public Statement apply(Statement base, Description description) {
this.outputDirectory = new File("target/output/" this.outputDirectory = new File("target/output/" + NON_ALPHABET_PATTERN
+ description.getMethodName().replaceAll("[^A-Za-z]+", "")); .matcher(description.getMethodName()).replaceAll(""));
this.outputDirectory.mkdirs(); this.outputDirectory.mkdirs();
return base; return base;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment