KotlinGenerateToStringAction: should generate .contentToString instead java.util.Arrays.toString
#KT-27563 Fixed
This commit is contained in:
@@ -43,6 +43,7 @@ import org.jetbrains.kotlin.resolve.BindingContext
|
|||||||
import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns
|
import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns
|
||||||
import org.jetbrains.kotlin.resolve.descriptorUtil.getSuperClassOrAny
|
import org.jetbrains.kotlin.resolve.descriptorUtil.getSuperClassOrAny
|
||||||
import org.jetbrains.kotlin.resolve.source.getPsi
|
import org.jetbrains.kotlin.resolve.source.getPsi
|
||||||
|
import org.jetbrains.kotlin.types.isNullable
|
||||||
import org.jetbrains.kotlin.utils.addToStdlib.lastIsInstanceOrNull
|
import org.jetbrains.kotlin.utils.addToStdlib.lastIsInstanceOrNull
|
||||||
|
|
||||||
private fun ClassDescriptor.findDeclaredToString(checkSupers: Boolean): FunctionDescriptor? {
|
private fun ClassDescriptor.findDeclaredToString(checkSupers: Boolean): FunctionDescriptor? {
|
||||||
@@ -118,7 +119,10 @@ class KotlinGenerateToStringAction : KotlinGenerateMemberActionBase<KotlinGenera
|
|||||||
protected fun renderVariableValue(variableDescriptor: VariableDescriptor, ref: String): String {
|
protected fun renderVariableValue(variableDescriptor: VariableDescriptor, ref: String): String {
|
||||||
val type = variableDescriptor.type
|
val type = variableDescriptor.type
|
||||||
return when {
|
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'"
|
KotlinBuiltIns.isString(type) -> "'$$ref'"
|
||||||
else -> "$$ref"
|
else -> "$$ref"
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-4
@@ -1,5 +1,3 @@
|
|||||||
import java.util.Arrays
|
|
||||||
|
|
||||||
// GENERATOR: MULTIPLE_TEMPLATES
|
// GENERATOR: MULTIPLE_TEMPLATES
|
||||||
class A(val n: IntArray, val s: Array<String>) {
|
class A(val n: IntArray, val s: Array<String>) {
|
||||||
val f: Float = 1.0f
|
val f: Float = 1.0f
|
||||||
@@ -10,8 +8,8 @@ class A(val n: IntArray, val s: Array<String>) {
|
|||||||
|
|
||||||
override fun toString(): String {
|
override fun toString(): String {
|
||||||
return "A(" +
|
return "A(" +
|
||||||
"n=${Arrays.toString(n)}, " +
|
"n=${n.contentToString()}, " +
|
||||||
"s=${Arrays.toString(s)}, " +
|
"s=${s.contentToString()}, " +
|
||||||
"f=$f" +
|
"f=$f" +
|
||||||
")"
|
")"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
import java.util.Arrays
|
|
||||||
|
|
||||||
// GENERATOR: SINGLE_TEMPLATE
|
// GENERATOR: SINGLE_TEMPLATE
|
||||||
class A(val n: IntArray, val s: Array<String>) {
|
class A(val n: IntArray, val s: Array<String>) {
|
||||||
val f: Float = 1.0f
|
val f: Float = 1.0f
|
||||||
@@ -9,6 +7,6 @@ class A(val n: IntArray, val s: Array<String>) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun toString(): 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() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
+12
@@ -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)"
|
||||||
|
}
|
||||||
|
}
|
||||||
Generated
+5
@@ -167,6 +167,11 @@ public class GenerateToStringActionTestGenerated extends AbstractGenerateToStrin
|
|||||||
runTest("idea/testData/codeInsight/generate/toString/singleTemplate/noVars.kt");
|
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")
|
@TestMetadata("singleVar.kt")
|
||||||
public void testSingleVar() throws Exception {
|
public void testSingleVar() throws Exception {
|
||||||
runTest("idea/testData/codeInsight/generate/toString/singleTemplate/singleVar.kt");
|
runTest("idea/testData/codeInsight/generate/toString/singleTemplate/singleVar.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user