Evaluate overloaded toString() in watches for Kotlin classes with delegate properties (KT-14068)

(cherry picked from commit 7774d2c)

 #KT-14068 Fixed
This commit is contained in:
Nikolay Krasko
2016-10-14 18:43:36 +03:00
committed by Nikolay Krasko
parent 440bdea736
commit 944b9b5b6c
4 changed files with 90 additions and 2 deletions
@@ -16,13 +16,17 @@
package org.jetbrains.kotlin.idea.debugger.render
import com.intellij.debugger.DebuggerManagerEx
import com.intellij.debugger.engine.DebuggerManagerThreadImpl
import com.intellij.debugger.engine.evaluation.EvaluationContext
import com.intellij.debugger.settings.NodeRendererSettings
import com.intellij.debugger.ui.impl.watch.MessageDescriptor
import com.intellij.debugger.ui.impl.watch.NodeManagerImpl
import com.intellij.debugger.ui.tree.DebuggerTreeNode
import com.intellij.debugger.ui.tree.ValueDescriptor
import com.intellij.debugger.ui.tree.render.ChildrenBuilder
import com.intellij.debugger.ui.tree.render.ClassRenderer
import com.intellij.debugger.ui.tree.render.DescriptorLabelListener
import com.intellij.xdebugger.settings.XDebuggerSettingsManager
import com.sun.jdi.ObjectReference
import com.sun.jdi.ReferenceType
@@ -34,8 +38,7 @@ import java.util.*
import com.sun.jdi.Type as JdiType
import org.jetbrains.org.objectweb.asm.Type as AsmType
class KotlinClassWithDelegatedPropertyRenderer : ClassRenderer() {
class KotlinClassWithDelegatedPropertyRenderer(val rendererSettings: NodeRendererSettings) : ClassRenderer() {
override fun isApplicable(jdiType: Type?): Boolean {
if (!super.isApplicable(jdiType)) return false
@@ -44,6 +47,28 @@ class KotlinClassWithDelegatedPropertyRenderer : ClassRenderer() {
return jdiType.allFields().any { it.name().endsWith(JvmAbi.DELEGATED_PROPERTY_NAME_SUFFIX) }
}
override fun calcLabel(descriptor: ValueDescriptor,
evaluationContext: EvaluationContext,
listener: DescriptorLabelListener): String {
val res = calcToStringLabel(descriptor, evaluationContext, listener)
if (res != null) {
return res
}
return super.calcLabel(descriptor, evaluationContext, listener)
}
private fun calcToStringLabel(descriptor: ValueDescriptor, evaluationContext: EvaluationContext,
listener: DescriptorLabelListener): String? {
val toStringRenderer = rendererSettings.toStringRenderer
if (toStringRenderer.isEnabled && DebuggerManagerEx.getInstanceEx(evaluationContext.project).context.isEvaluationPossible) {
if (toStringRenderer.isApplicable(descriptor.type)) {
return toStringRenderer.calcLabel(descriptor, evaluationContext, listener)
}
}
return null
}
override fun buildChildren(value: Value?, builder: ChildrenBuilder, context: EvaluationContext) {
DebuggerManagerThreadImpl.assertIsManagerThread()
@@ -0,0 +1,36 @@
LineBreakpoint created at delegatedPropertyInClassWithToString.kt:8
!JDK_HOME!\bin\java -agentlib:jdwp=transport=dt_socket,address=!HOST_NAME!:!HOST_PORT!,suspend=y,server=n -Dfile.encoding=!FILE_ENCODING! -classpath !OUTPUT_PATH!;!KOTLIN_RUNTIME!;!CUSTOM_LIBRARY!;!RT_JAR! delegatedPropertyInClassWithToString.DelegatedPropertyInClassWithToStringKt
Connected to the target VM, address: '!HOST_NAME!:PORT_NAME!', transport: 'socket'
delegatedPropertyInClassWithToString.kt:8
package delegatedPropertyInClassWithToString
import kotlin.reflect.KProperty
fun main(args: Array<String>) {
val a = A()
//Breakpoint!
args.size
}
class A {
val prop by MyDelegate()
override fun toString(): String = "KotlinTest"
}
class MyDelegate {
operator fun getValue(t: Any?, p: KProperty<*>): Int = 1
}
// PRINT_FRAME
Compile bytecode for args.size
frame = main:8, DelegatedPropertyInClassWithToStringKt {delegatedPropertyInClassWithToString}
local = args: java.lang.String[] = {java.lang.String[0]@uniqueID} (sp = delegatedPropertyInClassWithToString.kt, 5)
local = a: delegatedPropertyInClassWithToString.A = {delegatedPropertyInClassWithToString.A@uniqueID}KotlinTest (sp = delegatedPropertyInClassWithToString.kt, 6)
field = prop$delegate: delegatedPropertyInClassWithToString.MyDelegate = {delegatedPropertyInClassWithToString.MyDelegate@uniqueID} (sp = delegatedPropertyInClassWithToString.kt, 12)
- Class has no fields
field = prop: int = 1 (sp = delegatedPropertyInClassWithToString.kt, 12)
Disconnected from the target VM, address: '!HOST_NAME!:PORT_NAME!', transport: 'socket'
Process finished with exit code 0
@@ -0,0 +1,21 @@
package delegatedPropertyInClassWithToString
import kotlin.reflect.KProperty
fun main(args: Array<String>) {
val a = A()
//Breakpoint!
args.size
}
class A {
val prop by MyDelegate()
override fun toString(): String = "KotlinTest"
}
class MyDelegate {
operator fun getValue(t: Any?, p: KProperty<*>): Int = 1
}
// PRINT_FRAME
@@ -564,6 +564,12 @@ public class KotlinEvaluateExpressionTestGenerated extends AbstractKotlinEvaluat
doSingleBreakpointTest(fileName);
}
@TestMetadata("delegatedPropertyInClassWithToString.kt")
public void testDelegatedPropertyInClassWithToString() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/debugger/tinyApp/src/evaluate/singleBreakpoint/frame/delegatedPropertyInClassWithToString.kt");
doSingleBreakpointTest(fileName);
}
@TestMetadata("delegatedPropertyInClassWoRenderer.kt")
public void testDelegatedPropertyInClassWoRenderer() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/debugger/tinyApp/src/evaluate/singleBreakpoint/frame/delegatedPropertyInClassWoRenderer.kt");