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

Closes gh-14483
This commit is contained in:
durigon
2018-09-16 11:42:49 +09:00
committed by Andy Wilkinson
parent 11016bc7f4
commit 7aaeefbc0e
8 changed files with 44 additions and 10 deletions

View File

@@ -22,6 +22,7 @@ import java.util.Collections;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import javax.annotation.processing.ProcessingEnvironment;
@@ -61,6 +62,8 @@ class TypeUtils {
private static final Map<String, TypeKind> WRAPPER_TO_PRIMITIVE;
private static final Pattern NEW_LINE_PATTERN = Pattern.compile("[\r\n]+");
static {
Map<String, TypeKind> primitives = new HashMap<>();
PRIMITIVE_WRAPPERS.forEach(
@@ -131,7 +134,7 @@ class TypeUtils {
String javadoc = (element != null)
? this.env.getElementUtils().getDocComment(element) : null;
if (javadoc != null) {
javadoc = javadoc.replaceAll("[\r\n]+", "").trim();
javadoc = NEW_LINE_PATTERN.matcher(javadoc).replaceAll("").trim();
}
return "".equals(javadoc) ? null : javadoc;
}

View File

@@ -21,6 +21,7 @@ import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;
import org.gradle.api.Project;
import org.gradle.api.tasks.bundling.AbstractArchiveTask;
@@ -36,6 +37,10 @@ import org.springframework.boot.loader.tools.FileUtils;
@SuppressWarnings("serial")
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 File script;
@@ -134,11 +139,13 @@ public class LaunchScriptConfiguration implements Serializable {
}
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) {
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,

View File

@@ -24,6 +24,7 @@ import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpression;
@@ -50,6 +51,9 @@ import org.springframework.util.FileCopyUtils;
*/
public class GradleBuild implements TestRule {
private static final Pattern GRADLE_VERSION_PATTERN = Pattern
.compile("\\[Gradle .+\\]");
private final TemporaryFolder temp = new TemporaryFolder();
private File projectDir;
@@ -95,7 +99,7 @@ public class GradleBuild implements TestRule {
}
private String removeGradleVersion(String methodName) {
return methodName.replaceAll("\\[Gradle .+\\]", "").trim();
return GRADLE_VERSION_PATTERN.matcher(methodName).replaceAll("").trim();
}
private URL getScriptForTestClass(Class<?> testClass) {

View File

@@ -22,6 +22,7 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.regex.Pattern;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.model.Dependency;
@@ -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)
public class RepackageMojo extends AbstractDependencyFilterMojo {
private static final Pattern WHITE_SPACE_PATTERN = Pattern.compile("\\s+");
/**
* The Maven project.
* @since 1.0
@@ -312,7 +315,8 @@ public class RepackageMojo extends AbstractDependencyFilterMojo {
}
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,