From b45af20e5ba31967b5aad51da29d25c16a871c79 Mon Sep 17 00:00:00 2001 From: Jon Schneider Date: Tue, 4 Oct 2016 15:00:20 -0700 Subject: [PATCH] Fix bug in treatment of escape characters on changeLiterals --- .../refactor/fix/ChangeMethodInvocation.kt | 23 ++++++++++--------- .../fix/ChangeMethodInvocationTest.kt | 4 ++-- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/main/kotlin/com/netflix/java/refactor/fix/ChangeMethodInvocation.kt b/src/main/kotlin/com/netflix/java/refactor/fix/ChangeMethodInvocation.kt index 1e51d2d..e48fada 100644 --- a/src/main/kotlin/com/netflix/java/refactor/fix/ChangeMethodInvocation.kt +++ b/src/main/kotlin/com/netflix/java/refactor/fix/ChangeMethodInvocation.kt @@ -263,23 +263,24 @@ class ChangeMethodInvocationScanner(val op: ChangeMethodInvocation) : FixingScan private inner class ChangeArgumentScanner : TreePathScanner, RefactorArgument>() { override fun visitLiteral(node: LiteralTree, refactor: RefactorArgument): List { val literal = node as JCTree.JCLiteral - val value = literal.value // prefix and suffix hold the special characters surrounding the values of primitive-ish types, // e.g. the "" around String, the L at the end of a long, etc. - val valueMatcher = "(.*)${Pattern.quote(value.toString())}(.*)".toRegex().find(node.toString().replace("\\", "")) - return when (valueMatcher) { - is MatchResult -> { - val (prefix, suffix) = valueMatcher.groupValues.drop(1) - - val transformed = refactor.refactorLiterals?.invoke(value) ?: value - if (transformed != value.toString()) listOf(literal.replace("$prefix$transformed$suffix")) else emptyList() - } + val (prefix, suffix, value) = when(literal.value) { + is String -> arrayOf("\"", "\"", node.source().trim('"')) else -> { - // this should never happen - emptyList() + val valueMatcher = "(.*)${Pattern.quote(literal.value.toString())}(.*)".toRegex().find(node.toString()) + when(valueMatcher) { + is MatchResult -> (valueMatcher.groupValues.drop(1) + literal.value).toTypedArray() + else -> arrayOf("", "", literal.value) + } } } + + val transformed = refactor.refactorLiterals?.invoke(value) ?: value + return if (transformed != value.toString()) + listOf(literal.replace("$prefix$transformed$suffix")) + else emptyList() } override fun reduce(r1: List?, r2: List?): List = diff --git a/src/test/kotlin/com/netflix/java/refactor/fix/ChangeMethodInvocationTest.kt b/src/test/kotlin/com/netflix/java/refactor/fix/ChangeMethodInvocationTest.kt index 769a8f6..81548f2 100644 --- a/src/test/kotlin/com/netflix/java/refactor/fix/ChangeMethodInvocationTest.kt +++ b/src/test/kotlin/com/netflix/java/refactor/fix/ChangeMethodInvocationTest.kt @@ -342,7 +342,7 @@ class ChangeMethodInvocationTest: AbstractRefactorTest() { |public class B { | A a; | public void test() { - | a.foo("mystring '%s'"); + | a.foo("mystring '%s' \"%s\" \n"); | } |} """) @@ -362,7 +362,7 @@ class ChangeMethodInvocationTest: AbstractRefactorTest() { |public class B { | A a; | public void test() { - | a.foo("mystring '{}'"); + | a.foo("mystring '{}' \"{}\" \n"); | } |} """)