Skip to content

Commit

Permalink
Merge pull request #42896 from Shadow-Devil/replaceall-to-replace
Browse files Browse the repository at this point in the history
Use `replace` instead of `replaceAll` if no regex is needed
  • Loading branch information
warunalakshitha authored Jun 11, 2024
2 parents 7402686 + 2fd88ef commit 18deb9f
Show file tree
Hide file tree
Showing 31 changed files with 49 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ public void run(String... args) {
} else {
try {
ballerinaShell.outputInfo(NEW_LINE + bbeHelpProvider.getDescription(topic)
.replaceAll(TAGS, EMPTY_STRING));
ballerinaShell.outputInfo(URL_PREFIX + URL + topic.replaceAll(" ", "-"));
.replace(TAGS, EMPTY_STRING));
ballerinaShell.outputInfo(URL_PREFIX + URL + topic.replace(" ", "-"));

} catch (HelpProviderException e) {
ballerinaShell.outputError(NEW_LINE + "Can not find the topic : " + topic + NEW_LINE + NEW_LINE +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ private void populateTopicList() {
Stream<BbeRecord> sampleList = Arrays.stream(samples);
sampleList.forEach((bbeRecordElement) -> {
if (bbeRecordElement != null) {
topicList.add(bbeRecordElement.getUrl().replaceAll("-", " "));
topicList.add(bbeRecordElement.getUrl().replace("-", " "));
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static String quoted(String identifier) {
*/
public static String shortenedString(Object input) {
String value = String.valueOf(input);
value = value.replaceAll("\n", "");
value = value.replace("\n", "");
if (value.length() > MAX_VAR_STRING_LENGTH) {
int subStrLength = MAX_VAR_STRING_LENGTH / 2;
return value.substring(0, subStrLength)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,6 @@ static String getCallStackString(String stackKey) {
}

private static String decodeStackElement(String stackElement) {
return Utils.decodeIdentifier(stackElement.replaceAll("\\$value\\$", ""));
return Utils.decodeIdentifier(stackElement.replace("$value$", ""));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -954,9 +954,9 @@ public static void initPackage(Path path, String packageName) throws IOException
String defaultManifest = FileUtils.readFileAsString(NEW_CMD_DEFAULTS + "/" + "manifest-app.toml");
// replace manifest distribution with a guessed value
defaultManifest = defaultManifest
.replaceAll(ORG_NAME, ProjectUtils.guessOrgName())
.replaceAll(PKG_NAME, guessPkgName(packageName, "app"))
.replaceAll(DIST_VERSION, RepoUtils.getBallerinaShortVersion());
.replace(ORG_NAME, ProjectUtils.guessOrgName())
.replace(PKG_NAME, guessPkgName(packageName, "app"))
.replace(DIST_VERSION, RepoUtils.getBallerinaShortVersion());
Files.write(ballerinaToml, defaultManifest.getBytes(StandardCharsets.UTF_8));
}

Expand All @@ -966,9 +966,9 @@ private static void initLibPackage(Path path, String packageName) throws IOExcep

String defaultManifest = FileUtils.readFileAsString(NEW_CMD_DEFAULTS + "/" + "manifest-lib.toml");
// replace manifest org and name with a guessed value.
defaultManifest = defaultManifest.replaceAll(ORG_NAME, ProjectUtils.guessOrgName())
.replaceAll(PKG_NAME, guessPkgName(packageName, "lib"))
.replaceAll(DIST_VERSION, RepoUtils.getBallerinaShortVersion());
defaultManifest = defaultManifest.replace(ORG_NAME, ProjectUtils.guessOrgName())
.replace(PKG_NAME, guessPkgName(packageName, "lib"))
.replace(DIST_VERSION, RepoUtils.getBallerinaShortVersion());

write(ballerinaToml, defaultManifest.getBytes(StandardCharsets.UTF_8));

Expand All @@ -990,16 +990,16 @@ private static void initToolPackage(Path path, String packageName) throws IOExce

String defaultManifest = FileUtils.readFileAsString(NEW_CMD_DEFAULTS + "/" + "manifest-app.toml");
defaultManifest = defaultManifest
.replaceAll(ORG_NAME, ProjectUtils.guessOrgName())
.replaceAll(PKG_NAME, guessPkgName(packageName, TOOL_DIR))
.replaceAll(DIST_VERSION, RepoUtils.getBallerinaShortVersion());
.replace(ORG_NAME, ProjectUtils.guessOrgName())
.replace(PKG_NAME, guessPkgName(packageName, TOOL_DIR))
.replace(DIST_VERSION, RepoUtils.getBallerinaShortVersion());
Files.write(ballerinaToml, defaultManifest.getBytes(StandardCharsets.UTF_8));

Path balToolToml = path.resolve(ProjectConstants.BAL_TOOL_TOML);
Files.createFile(balToolToml);

String balToolManifest = FileUtils.readFileAsString(NEW_CMD_DEFAULTS + "/" + "manifest-tool.toml");
balToolManifest = balToolManifest.replaceAll(TOOL_ID, guessPkgName(packageName, TOOL_DIR));
balToolManifest = balToolManifest.replace(TOOL_ID, guessPkgName(packageName, TOOL_DIR));

write(balToolToml, balToolManifest.getBytes(StandardCharsets.UTF_8));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ private void pushBalaToRemote(Path balaPath, CentralAPIClient client) {
errorMessage = errorMessage.substring(0, errorMessage.indexOf("\n\tat"));
}

errorMessage = errorMessage.replaceAll("error: ", "");
errorMessage = errorMessage.replace("error: ", "");

// when unauthorized access token for organization is given
if (errorMessage.contains("subject claims missing in the user info repsonse")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ private static void extractMockFunctionClassMapping(Map<String, TestSuite> testS
functionToMock = key.substring(key.indexOf(MOCK_LEGACY_DELIMITER) + 1);
}
}
functionToMock = functionToMock.replaceAll("\\\\", "");
functionToMock = functionToMock.replace("\\", "");
mockFunctionClassMapping.computeIfAbsent(functionToMockClassName,
k -> new ArrayList<>()).add("$ORIG_" + functionToMock);
}
Expand Down Expand Up @@ -444,7 +444,7 @@ private static void dumpJar(Map<String, byte[]> modifiedClassDefs, Map<String, b
for (Map.Entry<String, byte[]> modifiedClassDef : modifiedClassDefs.entrySet()) {
if (modifiedClassDef.getValue().length > 0) {
String entry = modifiedClassDef.getKey();
String path = entry.replaceAll("\\.", PATH_SEPARATOR) + CLASS_EXTENSION;
String path = entry.replace(".", PATH_SEPARATOR) + CLASS_EXTENSION;
duplicatePaths.add(path);
jarOutputStream.putNextEntry(new ZipEntry(path));
jarOutputStream.write(modifiedClassDefs.get(entry));
Expand Down Expand Up @@ -502,7 +502,7 @@ private static void populateClassNameVsFunctionToMockMap(Map<String, List<String
functionToMock = key.substring(key.indexOf(MOCK_LEGACY_DELIMITER));
}
}
functionToMock = functionToMock.replaceAll("\\\\", "");
functionToMock = functionToMock.replace("\\", "");
classVsMockFunctionsMap.computeIfAbsent(functionToMockClassName,
k -> new ArrayList<>()).add(functionToMock);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public static void generateTesterinaReports(Project project, TestReport testRepo
testReport.finalizeTestResults(project.buildOptions().codeCoverage());

Gson gson = new Gson();
String json = gson.toJson(testReport).replaceAll("\\\\\\(", "(");
String json = gson.toJson(testReport).replace("\\(", "(");

File jsonFile = new File(reportDir.resolve(RESULTS_JSON_FILE).toString());
try (FileOutputStream fileOutputStream = new FileOutputStream(jsonFile)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ public static String guessPkgName(String packageName, String template) {

// if package name has consecutive underscores, replace them with a single underscore
if (packageName.contains("__")) {
packageName = packageName.replaceAll("__", "_");
packageName = packageName.replace("__", "_");
}

// if package name has trailing underscore remove it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,7 @@ private String generateName(String name, BLangNode parent) {
case CLASS_DEFN:
return generateName(((BLangClassDefinition) parent).name.getValue() + UNDERSCORE + name, parent.parent);
case FUNCTION:
name = ((BLangFunction) parent).symbol.name.value.replaceAll("\\.", UNDERSCORE) + UNDERSCORE + name;
name = ((BLangFunction) parent).symbol.name.value.replace(".", UNDERSCORE) + UNDERSCORE + name;
return generateName(name, parent.parent);
case RESOURCE_FUNC:
return generateName(((BLangResourceFunction) parent).name.value + UNDERSCORE + name, parent.parent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public BLangMarkDownDeprecatedParametersDocumentation getDeprecatedParametersDoc
public String getDocumentation() {
return documentationLines.stream()
.map(BLangMarkdownDocumentationLine::getText)
.collect(Collectors.joining("\n")).replaceAll("\r", "");
.collect(Collectors.joining("\n")).replace("\r", "");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void addDeprecationLine(String text) {

@Override
public String getDocumentation() {
return String.join("\n", this.deprecationDocumentationLines).replaceAll("\r", "");
return String.join("\n", this.deprecationDocumentationLines).replace("\r", "");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void addParameterDocumentationLine(String text) {

@Override
public String getParameterDocumentation() {
return parameterDocumentationLines.stream().collect(Collectors.joining("\n")).replaceAll("\r", "");
return parameterDocumentationLines.stream().collect(Collectors.joining("\n")).replace("\r", "");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void addReturnParameterDocumentationLine(String text) {

@Override
public String getReturnParameterDocumentation() {
return returnParameterDocumentationLines.stream().collect(Collectors.joining("\n")).replaceAll("\r", "");
return returnParameterDocumentationLines.stream().collect(Collectors.joining("\n")).replace("\r", "");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ public static boolean isLangLibOrLangTest(ModuleID moduleID) {
* @return The identifier with escape characters escaped
*/
public static String escapeEscapeCharsInIdentifier(String identifier) {
return identifier.replaceAll("\\\\", "\\\\\\\\");
return identifier.replace("\\", "\\\\");
}

/**
Expand All @@ -279,8 +279,8 @@ public static String escapeEscapeCharsInIdentifier(String identifier) {
* @return Processed text
*/
public static String escapeSpecialCharsInInsertText(String text) {
return text.replaceAll("\\\\", "\\\\\\\\")
.replaceAll("\\$", Matcher.quoteReplacement("\\$"));
return text.replace("\\", "\\\\")
.replace("$", "\\$");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ protected static String getComponentName(LSOperation operation) {
if (operation == null) {
return LS_TELEMETRY_COMPONENT_NAME;
}
return LS_TELEMETRY_COMPONENT_NAME + "." + operation.getName().replaceAll("/", "_");
return LS_TELEMETRY_COMPONENT_NAME + "." + operation.getName().replace("/", "_");
}

public String getComponent() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -789,9 +789,9 @@ private String generateMappingFunction(String mappingFromServer, String foundTyp
rightType = rhsSignature;
}

mappingFromServer = mappingFromServer.replaceAll("\"", "");
mappingFromServer = mappingFromServer.replaceAll(",", ", ");
mappingFromServer = mappingFromServer.replaceAll(":", ": ");
mappingFromServer = mappingFromServer.replace("\"", "");
mappingFromServer = mappingFromServer.replace(",", ", ");
mappingFromServer = mappingFromServer.replace(":", ": ");


// change the generated mapping function to be compatible with spread fields.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void visit(VariableDeclarationNode variableDeclarationNode) {
SpecificFieldNode specificFieldNode = (SpecificFieldNode) field;
if (specificFieldNode.fieldName().kind() == SyntaxKind.STRING_LITERAL) {
String fieldName = ((BasicLiteralNode) specificFieldNode.fieldName()).literalToken().text();
fieldName = fieldName.replaceAll("\"", "");
fieldName = fieldName.replace("\"", "");
Optional<Symbol> symbol = this.model.symbol(variableDeclarationNode);
if (symbol.isPresent()) {
TypeSymbol typeSymbol = ((VariableSymbol) symbol.get()).typeDescriptor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ public static String[] getQModuleNameParts(String path) {
String[] moduleParts;
// Makes the path os-independent, as some of the incoming windows source paths can contain both of the
// separator types(possibly due to a potential JDI bug).
path = path.replaceAll("\\\\", "/");
path = path.replace("\\", "/");
if (path.contains("/")) {
moduleParts = path.split("/");
} else {
Expand Down Expand Up @@ -349,9 +349,9 @@ private static String getFullModuleName(Document document) {

private static String replaceSeparators(String path) {
if (path.contains("/")) {
return path.replaceAll("/", ".");
return path.replace("/", ".");
} else if (path.contains("\\")) {
return path.replaceAll("\\\\", ".");
return path.replace("\\", ".");
}
return path;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public String computeValue() {
try {
String stringValue = getStringValue(context, jvmValue);
if (stringValue.startsWith(TYPEDESC_VALUE_PREFIX)) {
stringValue = stringValue.replaceAll(TYPEDESC_VALUE_PREFIX, "");
stringValue = stringValue.replace(TYPEDESC_VALUE_PREFIX, "");
}
return stringValue;
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ public static Object getXMLStepResult(BXml xmlVal, String xmlStepPattern) {

private static BString[] processXMLNamePattern(String xmlNamePattern) {
// removes LT and GT tokens if presents.
xmlNamePattern = xmlNamePattern.replaceAll("<", "").replaceAll(">", "");
xmlNamePattern = xmlNamePattern.replace("<", "").replace(">", "");

if (xmlNamePattern.contains(XML_STEP_SEPARATOR)) {
String[] stepParts = xmlNamePattern.split(XML_ALL_CHILDREN_STEP);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class SyntaxTreeUtils {
* @return node typename
*/
public static String getNodeKindName(SyntaxKind nodeKind) {
return nodeKind.name().toLowerCase(Locale.ROOT).toLowerCase(Locale.ENGLISH).replaceAll("_", " ");
return nodeKind.name().toLowerCase(Locale.ROOT).toLowerCase(Locale.ENGLISH).replace("_", " ");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ private static void gatherMockedFunctions(List<String> mockedFunctionList, Expre
if ("test".equals(modulePrefix) && "call".equals(methodName)
&& "when".equals(identifier)) {
String mockedFunction = methodCallExpressionNode.arguments()
.get(0).toString().replaceAll("\"", "");
.get(0).toString().replace("\"", "");
mockedFunctionList.add(mockedFunction);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public static BString getStringDiff(BString actual, BString expected) {
}
}
}
output = output.replaceAll("\n\n", " \n \n ");
output = output.replace("\n\n", " \n \n ");
return StringUtils.fromString(output);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ public void process(FunctionNode functionNode, List<AnnotationAttachmentNode> an
// Adding `<className> # <functionToMock> --> <MockFnObjectName>` to registry
String className = getQualifiedClassName(bLangTestablePackage,
functionToMockID.toString(), vals[1]);
vals[1] = vals[1].replaceAll("\\\\", "");
vals[1] = vals[1].replace("\\", "");
registry.addMockFunctionsSourceMap(bLangTestablePackage.packageID.getName().toString()
+ MODULE_DELIMITER + className + MOCK_LEGACY_DELIMITER + vals[1], functionName);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ private boolean isGroupAvailable(List<String> inputGroups, List<String> function
* @return String
*/
private String getStringValue(Node valueExpr) {
return valueExpr.toString().replaceAll("\\\"", "").trim();
return valueExpr.toString().replace("\"", "").trim();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public static Object decode(BString str, BString charset) {
try {
String javaStr = str.getValue();
javaStr = javaStr.replaceAll("%(?![0-9a-fA-F]{2})", "%25");
javaStr = javaStr.replaceAll("\\+", "%2B");
javaStr = javaStr.replace("+", "%2B");
return io.ballerina.runtime.api.utils.StringUtils.fromString(
URLDecoder.decode(javaStr, charset.getValue()));
} catch (UnsupportedEncodingException | IllegalArgumentException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ private static Object callMockFunction(String originalFunction, String originalC
if (Thread.currentThread().getStackTrace().length >= 5) {
String classname = Thread.currentThread().getStackTrace()[4].getClassName();
if (classname.contains("/")) {
classname = classname.replaceAll("/", ".");
classname = classname.replace("/", ".");
}
String[] projectInfo = classname.split(Pattern.quote("."));
// Set project info
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ private static byte[] replaceMethodBody(Method method, Method mockMethod, String
try {
InputStream ins;
if (coverage) {
String instrumentedClassPath = instrumentDir + "/" + clazz.getName().replaceAll("\\.", "/") + ".class";
String instrumentedClassPath = instrumentDir + "/" + clazz.getName().replace(".", "/") + ".class";
ins = new FileInputStream(instrumentedClassPath);
} else {
ins = clazz.getResourceAsStream(clazz.getSimpleName() + ".class");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static void instrumentOffline(List<URL> projectModuleJarList, Path destDi
URLClassLoader classLoader = new URLClassLoader(projectModuleJarList.toArray(new URL[0]));
for (String className : mockClassNames) {
Class<?> clazz = classLoader.loadClass(className);
File file = new File(destDir.toString(), className.replaceAll("\\.", "/") + ".class");
File file = new File(destDir.toString(), className.replace(".", "/") + ".class");
file.getParentFile().mkdirs();
try (InputStream input = clazz.getResourceAsStream(clazz.getSimpleName() + ".class");
OutputStream output = new FileOutputStream(file)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ private static int getTestExecutionState(Class<?> initClazz) {
*/
public static String formatError(String errorMsg) {
StringBuilder newErrMsg = new StringBuilder();
errorMsg = errorMsg.replaceAll("\n", "\n\t");
errorMsg = errorMsg.replace("\n", "\n\t");
List<String> msgParts = Arrays.asList(errorMsg.split("\n"));
boolean stackTraceStartFlag = true;

Expand Down

0 comments on commit 18deb9f

Please sign in to comment.