KotlinGenerateToStringAction: should generate .contentToString instead java.util.Arrays.toString

#KT-27563 Fixed
This commit is contained in:
Dmitry Gridin
2019-07-10 15:50:59 +03:00
parent 43c6049f02
commit e0c5f897fb
6 changed files with 33 additions and 8 deletions
@@ -43,6 +43,7 @@ import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns
import org.jetbrains.kotlin.resolve.descriptorUtil.getSuperClassOrAny
import org.jetbrains.kotlin.resolve.source.getPsi
import org.jetbrains.kotlin.types.isNullable
import org.jetbrains.kotlin.utils.addToStdlib.lastIsInstanceOrNull
private fun ClassDescriptor.findDeclaredToString(checkSupers: Boolean): FunctionDescriptor? {
@@ -118,7 +119,10 @@ class KotlinGenerateToStringAction : KotlinGenerateMemberActionBase<KotlinGenera
protected fun renderVariableValue(variableDescriptor: VariableDescriptor, ref: String): String {
val type = variableDescriptor.type
return when {
KotlinBuiltIns.isArray(type) || KotlinBuiltIns.isPrimitiveArray(type) -> "\${java.util.Arrays.toString($ref)}"
KotlinBuiltIns.isArray(type) || KotlinBuiltIns.isPrimitiveArray(type) -> {
val dot = if (type.isNullable()) "?." else "."
"\${$ref${dot}contentToString()}"
}
KotlinBuiltIns.isString(type) -> "'$$ref'"
else -> "$$ref"
}
@@ -1,5 +1,3 @@
import java.util.Arrays
// GENERATOR: MULTIPLE_TEMPLATES
class A(val n: IntArray, val s: Array<String>) {
val f: Float = 1.0f
@@ -10,8 +8,8 @@ class A(val n: IntArray, val s: Array<String>) {
override fun toString(): String {
return "A(" +
"n=${Arrays.toString(n)}, " +
"s=${Arrays.toString(s)}, " +
"n=${n.contentToString()}, " +
"s=${s.contentToString()}, " +
"f=$f" +
")"
}
@@ -1,5 +1,3 @@
import java.util.Arrays
// GENERATOR: SINGLE_TEMPLATE
class A(val n: IntArray, val s: Array<String>) {
val f: Float = 1.0f
@@ -9,6 +7,6 @@ class A(val n: IntArray, val s: Array<String>) {
}
override fun toString(): String {
return "A(n=${Arrays.toString(n)}, s=${Arrays.toString(s)}, f=$f)"
return "A(n=${n.contentToString()}, s=${s.contentToString()}, f=$f)"
}
}
@@ -0,0 +1,8 @@
// GENERATOR: SINGLE_TEMPLATE
class A(val n: IntArray?, val s: Array<String>) {<caret>
val f: Float = 1.0f
fun foo() {
}
}
@@ -0,0 +1,12 @@
// GENERATOR: SINGLE_TEMPLATE
class A(val n: IntArray?, val s: Array<String>) {
val f: Float = 1.0f
fun foo() {
}
override fun toString(): String {
return "A(n=${n?.contentToString()}, s=${s.contentToString()}, f=$f)"
}
}
@@ -167,6 +167,11 @@ public class GenerateToStringActionTestGenerated extends AbstractGenerateToStrin
runTest("idea/testData/codeInsight/generate/toString/singleTemplate/noVars.kt");
}
@TestMetadata("nullableArrays.kt")
public void testNullableArrays() throws Exception {
runTest("idea/testData/codeInsight/generate/toString/singleTemplate/nullableArrays.kt");
}
@TestMetadata("singleVar.kt")
public void testSingleVar() throws Exception {
runTest("idea/testData/codeInsight/generate/toString/singleTemplate/singleVar.kt");