Fix deprecated replaceAll and replaceFirst usages.

This commit is contained in:
Ilya Gorbunov
2015-05-13 23:15:15 +03:00
parent 36fb5b3c49
commit 79a5e0607d
13 changed files with 31 additions and 30 deletions
@@ -24,5 +24,5 @@ public abstract class JetElementInstructionImpl(
lexicalScope: LexicalScope lexicalScope: LexicalScope
) : InstructionImpl(lexicalScope), JetElementInstruction { ) : InstructionImpl(lexicalScope), JetElementInstruction {
protected fun render(element: PsiElement): String = protected fun render(element: PsiElement): String =
element.getText()?.replaceAll("\\s+", " ") ?: "" element.getText()?.replace("\\s+".toRegex(), " ") ?: ""
} }
@@ -46,7 +46,7 @@ public abstract class AbstractPseudoValueTest : AbstractPseudocodeTest() {
} }
fun elementText(element: JetElement?): String = fun elementText(element: JetElement?): String =
element?.getText()?.replaceAll("\\s+", " ") ?: "" element?.getText()?.replace("\\s+".toRegex(), " ") ?: ""
fun valueDecl(value: PseudoValue): String { fun valueDecl(value: PseudoValue): String {
val typePredicate = expectedTypePredicateMap.getOrPut(value) { getExpectedTypePredicate(value, bindingContext) } val typePredicate = expectedTypePredicateMap.getOrPut(value) { getExpectedTypePredicate(value, bindingContext) }
@@ -28,6 +28,8 @@ import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.util.text.StringUtil;
import kotlin.Function1; import kotlin.Function1;
import kotlin.KotlinPackage; import kotlin.KotlinPackage;
import kotlin.text.Regex;
import kotlin.text.MatchResult;
import org.intellij.lang.annotations.Language; import org.intellij.lang.annotations.Language;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.cli.common.KotlinVersion; import org.jetbrains.kotlin.cli.common.KotlinVersion;
@@ -39,8 +41,6 @@ import org.junit.Rule;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.regex.MatchResult;
import java.util.regex.Pattern;
import static org.junit.Assert.*; import static org.junit.Assert.*;
@@ -79,12 +79,12 @@ public abstract class KotlinIntegrationTestBase {
String contentWithRelativePaths = content.replace(baseDir.getAbsolutePath(), pathId); String contentWithRelativePaths = content.replace(baseDir.getAbsolutePath(), pathId);
@Language("RegExp") @Language("RegExp")
String RELATIVE_PATH_WITH_MIXED_SEPARATOR = Pattern.quote(pathId) + "[-.\\w/\\\\]*"; String RELATIVE_PATH_WITH_MIXED_SEPARATOR = Regex.Companion.escape(pathId) + "[-.\\w/\\\\]*";
return KotlinPackage.replaceAll(contentWithRelativePaths, RELATIVE_PATH_WITH_MIXED_SEPARATOR, new Function1<MatchResult, String>() { return new Regex(RELATIVE_PATH_WITH_MIXED_SEPARATOR).replace(contentWithRelativePaths, new Function1<MatchResult, String>() {
@Override @Override
public String invoke(MatchResult mr) { public String invoke(MatchResult mr) {
return FileUtil.toSystemIndependentName(mr.group()); return FileUtil.toSystemIndependentName(mr.getValue());
} }
}); });
} }
@@ -57,7 +57,7 @@ public trait AbstractSMAPBaseTest {
if (InTextDirectivesUtils.isDirectiveDefined(fileContent, smapPrefix)) { if (InTextDirectivesUtils.isDirectiveDefined(fileContent, smapPrefix)) {
InTextDirectivesUtils.findLinesWithPrefixesRemoved(fileContent, smapPrefix) InTextDirectivesUtils.findLinesWithPrefixesRemoved(fileContent, smapPrefix)
var smapData = fileContent.substring(fileContent.indexOf(smapPrefix)) var smapData = fileContent.substring(fileContent.indexOf(smapPrefix))
smapData = smapData.replaceAll("//", "").trim() smapData = smapData.replace("//", "").trim()
return SMAPAndFile(if (smapData.startsWith("SMAP ABSENT")) null else smapData, return SMAPAndFile(if (smapData.startsWith("SMAP ABSENT")) null else smapData,
SMAPAndFile.getPath(file.getVirtualFile().getCanonicalPath()!!)) SMAPAndFile.getPath(file.getVirtualFile().getCanonicalPath()!!))
@@ -27,9 +27,11 @@ import java.io.File
import java.util.ArrayDeque import java.util.ArrayDeque
import java.util.ArrayList import java.util.ArrayList
import java.util.regex.Pattern import java.util.regex.Pattern
import kotlin.text.Regex
private val START_PATTERN = Pattern.compile(">>>( *)(.*)$") private val START_PATTERN = Pattern.compile(">>>( *)(.*)$")
private val INCOMPLETE_PATTERN = Pattern.compile("\\.\\.\\.( *)(.*)$") private val INCOMPLETE_PATTERN = Pattern.compile("\\.\\.\\.( *)(.*)$")
private val TRAILING_NEWLINE_REGEX = Regex("\n$")
private val INCOMPLETE_LINE_MESSAGE = "incomplete line" private val INCOMPLETE_LINE_MESSAGE = "incomplete line"
@@ -89,8 +91,8 @@ public abstract class AbstractReplInterpreterTest : UsefulTestCase() {
Assert.assertEquals( Assert.assertEquals(
"After evaluation of: $code", "After evaluation of: $code",
StringUtil.convertLineSeparators(expected).replaceFirst("\n$", ""), StringUtil.convertLineSeparators(expected).replaceFirst(TRAILING_NEWLINE_REGEX, ""),
StringUtil.convertLineSeparators(actual).replaceFirst("\n$", "") StringUtil.convertLineSeparators(actual).replaceFirst(TRAILING_NEWLINE_REGEX, "")
) )
} }
} }
@@ -63,8 +63,8 @@ public fun buildDecompiledText(
!classHeader.isCompatibleAbiVersion -> { !classHeader.isCompatibleAbiVersion -> {
DecompiledText( DecompiledText(
INCOMPATIBLE_ABI_VERSION_COMMENT INCOMPATIBLE_ABI_VERSION_COMMENT
.replaceAll(CURRENT_ABI_VERSION_MARKER, JvmAbi.VERSION.toString()) .replace(CURRENT_ABI_VERSION_MARKER, JvmAbi.VERSION.toString())
.replaceAll(FILE_ABI_VERSION_MARKER, classHeader.version.toString()), .replace(FILE_ABI_VERSION_MARKER, classHeader.version.toString()),
mapOf()) mapOf())
} }
classHeader.isCompatiblePackageFacadeKind() -> classHeader.isCompatiblePackageFacadeKind() ->
@@ -194,7 +194,7 @@ class KotlinEvaluator(val codeFragment: JetCodeFragment,
} }
private fun getClassName(fileName: String): String { private fun getClassName(fileName: String): String {
return fileName.substringBeforeLast(".class").replaceAll("/", ".") return fileName.substringBeforeLast(".class").replace("/", ".")
} }
private fun runEval4j(context: EvaluationContextImpl, compiledData: CompiledDataDescriptor): InterpreterResult { private fun runEval4j(context: EvaluationContextImpl, compiledData: CompiledDataDescriptor): InterpreterResult {
@@ -40,7 +40,7 @@ public class ConvertToConcatenatedStringIntention : JetSelfTargetingOffsetIndepe
entry.toSeparateString(quote, convertExplicitly = (index == 0), isFinalEntry = (index == entries.lastIndex)) entry.toSeparateString(quote, convertExplicitly = (index == 0), isFinalEntry = (index == entries.lastIndex))
} }
.join(separator = "+") .join(separator = "+")
.replaceAll("""$quote\+$quote""", "") .replace("""$quote+$quote""", "")
val replacement = JetPsiFactory(element).createExpression(text) val replacement = JetPsiFactory(element).createExpression(text)
element.replace(replacement) element.replace(replacement)
@@ -109,7 +109,7 @@ public class ConvertToStringTemplateIntention : JetSelfTargetingOffsetIndependen
null -> "" null -> ""
else -> "\${" + expressionText.replaceAll("\n+", " ") + "}" else -> "\${" + expressionText.replace("\n+".toRegex(), " ") + "}"
} }
} }
} }
@@ -58,7 +58,6 @@ import org.jetbrains.kotlin.test.InTextDirectivesUtils
import org.junit.Assert import org.junit.Assert
import java.io.File import java.io.File
import java.util.Collections import java.util.Collections
import java.util.regex.Pattern
public abstract class AbstractKotlinEvaluateExpressionTest : KotlinDebuggerTestBase() { public abstract class AbstractKotlinEvaluateExpressionTest : KotlinDebuggerTestBase() {
private val logger = Logger.getLogger(javaClass<KotlinEvaluateExpressionCache>())!! private val logger = Logger.getLogger(javaClass<KotlinEvaluateExpressionCache>())!!
@@ -255,7 +254,7 @@ public abstract class AbstractKotlinEvaluateExpressionTest : KotlinDebuggerTestB
fun printDescriptor(descriptor: NodeDescriptorImpl, indent: Int): Boolean { fun printDescriptor(descriptor: NodeDescriptorImpl, indent: Int): Boolean {
if (descriptor is DefaultNodeDescriptor) return true if (descriptor is DefaultNodeDescriptor) return true
val label = descriptor.getLabel()!!.replaceAll("Package\\$[\\w]*\\$[0-9a-f]+", "Package\\$@packagePartHASH") val label = descriptor.getLabel()!!.replace("Package\\$[\\w]*\\$[0-9a-f]+".toRegex(), "Package\\$@packagePartHASH")
if (label.endsWith(XDebuggerUIConstants.COLLECTING_DATA_MESSAGE)) return true if (label.endsWith(XDebuggerUIConstants.COLLECTING_DATA_MESSAGE)) return true
val curIndent = " ".repeat(indent) val curIndent = " ".repeat(indent)
@@ -338,18 +337,18 @@ public abstract class AbstractKotlinEvaluateExpressionTest : KotlinDebuggerTestB
Assert.assertTrue("Evaluate expression returns wrong result for $text:\nexpected = $expectedResult\nactual = $actualResult\n", expectedResult == actualResult) Assert.assertTrue("Evaluate expression returns wrong result for $text:\nexpected = $expectedResult\nactual = $actualResult\n", expectedResult == actualResult)
} }
catch (e: EvaluateException) { catch (e: EvaluateException) {
Assert.assertTrue("Evaluate expression throws wrong exception for $text:\nexpected = $expectedResult\nactual = ${e.getMessage()}\n", expectedResult == e.getMessage()?.replaceFirst("id=[0-9]*", "id=ID")) Assert.assertTrue("Evaluate expression throws wrong exception for $text:\nexpected = $expectedResult\nactual = ${e.getMessage()}\n", expectedResult == e.getMessage()?.replaceFirst(ID_PART_REGEX, "id=ID"))
} }
} }
} }
private fun Value.asString(): String { private fun Value.asString(): String {
if (this is ObjectValue && this.value is ObjectReference) { if (this is ObjectValue && this.value is ObjectReference) {
val regexMatcher = PACKAGE_PART_PATTERN.matcher(this.toString()) return this.toString().replace(PACKAGE_PART_REGEX, "$1@packagePartHASH").replaceFirst(ID_PART_REGEX, "id=ID")
return regexMatcher.replaceAll("$1@packagePartHASH").replaceFirst("id=[0-9]*", "id=ID")
} }
return this.toString() return this.toString()
} }
} }
private val PACKAGE_PART_PATTERN = Pattern.compile("(Package\\$[\\w]*\\$)([0-9a-f]+)"); private val PACKAGE_PART_REGEX = "(Package\\$[\\w]*\\$)([0-9a-f]+)".toRegex()
private val ID_PART_REGEX = "id=[0-9]*".toRegex()
@@ -59,11 +59,11 @@ public object JavaToKotlinTranslator {
return code return code
.trim() .trim()
.replaceAll("\r\n", "\n") .replace("\r\n", "\n")
.replaceAll(" \n", "\n") .replace(" \n", "\n")
.replaceAll("\n ", "\n") .replace("\n ", "\n")
.replaceAll("\n+", "\n") .replace("\n+".toRegex(), "\n")
.replaceAll(" +", " ") .replace(" +".toRegex(), " ")
.trim() .trim()
} }
@@ -114,18 +114,18 @@ public abstract class AbstractJavaToKotlinConverterSingleFileTest : AbstractJava
} }
private fun methodToKotlin(text: String, settings: ConverterSettings, project: Project): String { private fun methodToKotlin(text: String, settings: ConverterSettings, project: Project): String {
val result = fileToKotlin("final class C {" + text + "}", settings, project).replaceAll("class C \\{", "").replaceAll("object C \\{", "") val result = fileToKotlin("final class C {" + text + "}", settings, project).replace("class C {", "").replace("object C {", "")
return result.substring(0, (result.lastIndexOf("}"))).trim() return result.substring(0, (result.lastIndexOf("}"))).trim()
} }
private fun statementToKotlin(text: String, settings: ConverterSettings, project: Project): String { private fun statementToKotlin(text: String, settings: ConverterSettings, project: Project): String {
val result = methodToKotlin("void main() {" + text + "}", settings, project) val result = methodToKotlin("void main() {" + text + "}", settings, project)
return result.substring(0, result.lastIndexOf("}")).replaceFirst("fun main\\(\\) \\{", "").trim() return result.substring(0, result.lastIndexOf("}")).replaceFirstLiteral("fun main() {", "").trim()
} }
private fun expressionToKotlin(code: String, settings: ConverterSettings, project: Project): String { private fun expressionToKotlin(code: String, settings: ConverterSettings, project: Project): String {
val result = statementToKotlin("final Object o =" + code + "}", settings, project) val result = statementToKotlin("final Object o =" + code + "}", settings, project)
return result.replaceFirst("val o:Any\\? = ", "").replaceFirst("val o:Any = ", "").replaceFirst("val o = ", "").trim() return result.replaceFirstLiteral("val o:Any? = ", "").replaceFirstLiteral("val o:Any = ", "").replaceFirstLiteral("val o = ", "").trim()
} }
override fun getProjectDescriptor() override fun getProjectDescriptor()
@@ -93,7 +93,7 @@ public abstract class AbstractIncrementalJpsTest : JpsBuildTestCase() {
buildResult buildResult
.getMessages(BuildMessage.Kind.ERROR) .getMessages(BuildMessage.Kind.ERROR)
.map { it.getMessageText() } .map { it.getMessageText() }
.map { it.replaceAll("^.+:\\d+:\\s+", "").trim() } .map { it.replace("^.+:\\d+:\\s+".toRegex(), "").trim() }
.joinToString("\n") .joinToString("\n")
return MakeResult(logger.log + "$COMPILATION_FAILED\n" + errorMessages + "\n", true, null) return MakeResult(logger.log + "$COMPILATION_FAILED\n" + errorMessages + "\n", true, null)
} }