Debugger: delegated property should be visible in debugger

#KT-4699 In Progress
 #KT-3582 Fixed
This commit is contained in:
Natalia Ukhorskaya
2014-09-12 14:00:36 +04:00
parent 655682ac97
commit 3dcfae4a81
10 changed files with 297 additions and 3 deletions
@@ -0,0 +1,6 @@
<root>
<item
name='com.intellij.debugger.ui.impl.watch.NodeManagerImpl java.util.Comparator&lt;com.intellij.debugger.ui.tree.DebuggerTreeNode&gt; getNodeComparator()'>
<annotation name='org.jetbrains.annotations.NotNull'/>
</item>
</root>
@@ -0,0 +1,10 @@
<root>
<item
name='com.intellij.debugger.ui.tree.NodeManager com.intellij.debugger.ui.tree.DebuggerTreeNode createMessageNode(java.lang.String)'>
<annotation name='org.jetbrains.annotations.NotNull'/>
</item>
<item
name='com.intellij.debugger.ui.tree.NodeManager com.intellij.debugger.ui.tree.DebuggerTreeNode createNode(com.intellij.debugger.ui.tree.NodeDescriptor, com.intellij.debugger.engine.evaluation.EvaluationContext)'>
<annotation name='org.jetbrains.annotations.NotNull'/>
</item>
</root>
@@ -0,0 +1,10 @@
<root>
<item
name='com.intellij.debugger.ui.tree.render.ChildrenRenderer void buildChildren(com.sun.jdi.Value, com.intellij.debugger.ui.tree.render.ChildrenBuilder, com.intellij.debugger.engine.evaluation.EvaluationContext) 1'>
<annotation name='org.jetbrains.annotations.NotNull'/>
</item>
<item
name='com.intellij.debugger.ui.tree.render.ChildrenRenderer void buildChildren(com.sun.jdi.Value, com.intellij.debugger.ui.tree.render.ChildrenBuilder, com.intellij.debugger.engine.evaluation.EvaluationContext) 2'>
<annotation name='org.jetbrains.annotations.NotNull'/>
</item>
</root>
+12 -3
View File
@@ -14,10 +14,16 @@
<item name='com.sun.jdi.Method java.util.List&lt;com.sun.jdi.Type&gt; argumentTypes()'>
<annotation name='org.jetbrains.annotations.NotNull'/>
</item>
<item name='com.sun.jdi.ReferenceType com.sun.jdi.ClassObjectReference classObject()'>
<item name='com.sun.jdi.ObjectReference com.sun.jdi.ReferenceType referenceType()'>
<annotation name='org.jetbrains.annotations.NotNull'/>
</item>
<item name='com.sun.jdi.ReferenceType com.sun.jdi.ClassObjectReference classObject()'>
<annotation name='org.jetbrains.annotations.NotNull'/>
</item>
<item name='com.sun.jdi.ReferenceType java.util.List&lt;com.sun.jdi.Location&gt; allLineLocations()'>
<item name='com.sun.jdi.ReferenceType java.util.List&lt;com.sun.jdi.Field&gt; allFields()'>
<annotation name='org.jetbrains.annotations.NotNull'/>
</item>
<item name='com.sun.jdi.ReferenceType java.util.List&lt;com.sun.jdi.Location&gt; allLineLocations()'>
<annotation name='org.jetbrains.annotations.NotNull'/>
</item>
<item name='com.sun.jdi.ReferenceType java.util.List&lt;com.sun.jdi.Method&gt; methodsByName(java.lang.String, java.lang.String)'>
@@ -41,7 +47,10 @@
<item name='com.sun.jdi.TypeComponent com.sun.jdi.ReferenceType declaringType()'>
<annotation name='org.jetbrains.annotations.NotNull'/>
</item>
<item name='com.sun.jdi.Value com.sun.jdi.Type type()'>
<item name='com.sun.jdi.TypeComponent java.lang.String name()'>
<annotation name='org.jetbrains.annotations.NotNull'/>
</item>
<item name='com.sun.jdi.Value com.sun.jdi.Type type()'>
<annotation name='org.jetbrains.annotations.NotNull'/>
</item>
<item name='com.sun.jdi.VirtualMachine com.sun.jdi.BooleanValue mirrorOf(boolean)'>
+1
View File
@@ -338,6 +338,7 @@
<debugger.codeFragmentFactory implementation="org.jetbrains.jet.plugin.debugger.evaluate.KotlinCodeFragmentFactory"/>
<debuggerEditorTextProvider language="jet" implementationClass="org.jetbrains.jet.plugin.debugger.KotlinEditorTextProvider"/>
<debuggerClassFilterProvider implementation="org.jetbrains.jet.plugin.debugger.filter.KotlinDebuggerInternalClassesFilterProvider"/>
<debugger.nodeRenderer implementation="org.jetbrains.jet.plugin.debugger.render.KotlinClassWithDelegatedPropertyRenderer"/>
<xdebugger.settings implementation="org.jetbrains.jet.plugin.debugger.KotlinDebuggerSettings"/>
<codeInsight.implementMethod language="jet" implementationClass="org.jetbrains.jet.plugin.codeInsight.ImplementMethodsHandler"/>
@@ -0,0 +1,49 @@
package org.jetbrains.jet.plugin.debugger.render
import com.intellij.openapi.project.Project
import com.sun.jdi.Field
import org.jetbrains.jet.lang.resolve.java.JvmAbi
import com.intellij.debugger.engine.evaluation.EvaluationContextImpl
import com.sun.jdi.Value
import com.intellij.debugger.ui.impl.watch.ValueDescriptorImpl
import com.intellij.debugger.DebuggerContext
import com.intellij.psi.PsiExpression
import com.intellij.debugger.settings.NodeRendererSettings
import com.intellij.debugger.ui.tree.FieldDescriptor
import com.sun.jdi.ObjectReference
class DelegatedPropertyFieldDescriptor(
val project: Project,
val computedValueFromGetter: Value,
val objectRef: ObjectReference,
val delegate: Field
): ValueDescriptorImpl(project, computedValueFromGetter), FieldDescriptor {
override fun getField() = delegate
override fun getObject() = objectRef
override fun calcValue(evaluationContext: EvaluationContextImpl?): Value? {
return computedValueFromGetter
}
override fun calcValueName(): String? {
return with (StringBuilder()) {
val classRenderer = NodeRendererSettings.getInstance()?.getClassRenderer()!!
append(getName())
if (classRenderer.SHOW_DECLARED_TYPE) {
append(": ")
append(classRenderer.renderTypeName(getValue()?.`type`()?.name()))
}
toString()
}
}
override fun getName(): String {
return delegate.name().trimTrailing(JvmAbi.DELEGATED_PROPERTY_NAME_SUFFIX)
}
override fun getDescriptorEvaluation(context: DebuggerContext?): PsiExpression? {
return null
}
}
@@ -0,0 +1,137 @@
/*
* Copyright 2010-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.plugin.debugger.render
import com.intellij.debugger.ui.tree.render.ClassRenderer
import com.sun.jdi.Type as JdiType
import com.sun.jdi.Value
import com.intellij.debugger.ui.tree.render.ChildrenBuilder
import com.intellij.debugger.engine.evaluation.EvaluationContext
import com.intellij.debugger.ui.tree.DebuggerTreeNode
import org.jetbrains.org.objectweb.asm.Type as AsmType
import com.intellij.debugger.engine.DebuggerManagerThreadImpl
import com.intellij.debugger.ui.impl.watch.ValueDescriptorImpl
import com.sun.jdi.ObjectReference
import com.intellij.xdebugger.settings.XDebuggerSettingsManager
import com.intellij.debugger.ui.impl.watch.NodeManagerImpl
import com.intellij.debugger.ui.impl.watch.MessageDescriptor
import java.util.ArrayList
import org.jetbrains.jet.lang.resolve.java.JvmAbi
import com.sun.jdi.Field
import com.intellij.debugger.engine.DebuggerUtils
import com.intellij.debugger.ui.impl.watch.FieldDescriptorImpl
import com.intellij.debugger.engine.evaluation.EvaluateException
import com.sun.jdi.ReferenceType
import com.sun.jdi.Type
import com.sun.jdi.InvocationException
import com.sun.jdi.Method
import org.jetbrains.jet.codegen.PropertyCodegen
import org.jetbrains.jet.lang.resolve.name.Name
public class KotlinClassWithDelegatedPropertyRenderer : ClassRenderer() {
override fun isApplicable(jdiType: Type?): Boolean {
if (!super<ClassRenderer>.isApplicable(jdiType)) return false
if (jdiType !is ReferenceType) return false
return jdiType.allFields().any { it.name().endsWith(JvmAbi.DELEGATED_PROPERTY_NAME_SUFFIX) }
}
override fun buildChildren(value: Value?, builder: ChildrenBuilder, context: EvaluationContext) {
DebuggerManagerThreadImpl.assertIsManagerThread()
if (value !is ObjectReference) return
val nodeManager = builder.getNodeManager()!!
val nodeDescriptorFactory = builder.getDescriptorManager()!!
val fields = value.referenceType().allFields()
if (fields.isEmpty()) {
builder.setChildren(listOf(nodeManager.createMessageNode(MessageDescriptor.CLASS_HAS_NO_FIELDS.getLabel())))
return
}
val children = ArrayList<DebuggerTreeNode>()
for (field in fields) {
if (!shouldDisplay(context, value, field)) {
continue
}
val fieldDescriptor = nodeDescriptorFactory.getFieldDescriptor(builder.getParentDescriptor(), value, field)
children.add(nodeManager.createNode(fieldDescriptor, context))
if (field.name().endsWith(JvmAbi.DELEGATED_PROPERTY_NAME_SUFFIX)) {
val method = findGetterForDelegatedProperty(value, field)
val threadReference = context.getSuspendContext().getThread()?.getThreadReference()
if (method != null && threadReference != null) {
val propValue = try {
value.invokeMethod(threadReference, method, listOf(), context.getSuspendContext().getSuspendPolicy())
}
catch(e: InvocationException) {
e.exception()
}
if (propValue != null) {
val delegatedPropertyDescriptor = DelegatedPropertyFieldDescriptor(
context.getDebugProcess().getProject()!!, propValue, value, field)
children.add(nodeManager.createNode(delegatedPropertyDescriptor, context))
}
}
}
}
if (XDebuggerSettingsManager.getInstance()!!.getDataViewSettings().isSortValues()) {
children.sortBy(NodeManagerImpl.getNodeComparator())
}
builder.setChildren(children)
}
private fun findGetterForDelegatedProperty(objRef: ObjectReference, delegate: Field): Method? {
val fieldName = delegate.name().trimTrailing(JvmAbi.DELEGATED_PROPERTY_NAME_SUFFIX)
val getterName = PropertyCodegen.getterName(Name.identifier(fieldName))
return objRef.referenceType().methodsByName(getterName)?.firstOrNull()
}
private fun shouldDisplay(context: EvaluationContext, objInstance: ObjectReference, field: Field): Boolean {
val isSynthetic = DebuggerUtils.isSynthetic(field)
when {
!SHOW_SYNTHETICS && isSynthetic,
!SHOW_STATIC && field.isStatic(),
!SHOW_STATIC_FINAL && field.isStatic() && field.isFinal() -> return false
SHOW_VAL_FIELDS_AS_LOCAL_VARIABLES && isSynthetic -> {
try {
val frameProxy = context.getFrameProxy()
if (frameProxy != null) {
val location = frameProxy.location()
if (location != null &&
objInstance == context.getThisObject() &&
objInstance.referenceType() == location.declaringType() &&
field.name().startsWith(FieldDescriptorImpl.OUTER_LOCAL_VAR_FIELD_PREFIX)
) {
return false
}
}
}
catch (ignored: EvaluateException) {
}
return true
}
else -> return true
}
}
}
@@ -0,0 +1,42 @@
LineBreakpoint created at delegatedPropertyInClass.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 !APP_PATH!\classes;!KOTLIN_RUNTIME!;!CUSTOM_LIBRARY!;!RT_JAR! delegatedPropertyInClass.DelegatedPropertyInClassPackage
Connected to the target VM, address: '!HOST_NAME!:PORT_NAME!', transport: 'socket'
delegatedPropertyInClass.kt:7
package delegatedPropertyInClass
import kotlin.properties.Delegates
fun main(args: Array<String>) {
val a = A()
//Breakpoint!
args.size
}
class A {
val prop by MyDelegate()
val propEx by MyDelegateThrowsException()
}
class MyDelegate {
fun get(t: Any?, p: PropertyMetadata): Int = 1
}
class MyDelegateThrowsException {
fun get(t: Any?, p: PropertyMetadata): Int = throw IllegalStateException()
}
// PRINT_FRAME
frame = main():8, DelegatedPropertyInClassPackage-@packagePartHASH {delegatedPropertyInClass}
static = static = delegatedPropertyInClass.DelegatedPropertyInClassPackage-@packagePartHASH
local = args: java.lang.String[] = {java.lang.String[0]@uniqueID}
local = a: delegatedPropertyInClass.A = {delegatedPropertyInClass.A@uniqueID}
field = prop$delegate: delegatedPropertyInClass.MyDelegate = {delegatedPropertyInClass.MyDelegate@uniqueID}
field = prop: int = 1
field = propEx$delegate: delegatedPropertyInClass.MyDelegateThrowsException = {delegatedPropertyInClass.MyDelegateThrowsException@uniqueID}
field = propEx: java.lang.IllegalStateException = {java.lang.IllegalStateException@uniqueID}java.lang.IllegalStateException
field = detailMessage: java.lang.String = null
field = cause: java.lang.Throwable = {java.lang.IllegalStateException@uniqueID}java.lang.IllegalStateException
field = stackTrace: java.lang.StackTraceElement[] = null
Disconnected from the target VM, address: '!HOST_NAME!:PORT_NAME!', transport: 'socket'
Process finished with exit code 0
@@ -0,0 +1,24 @@
package delegatedPropertyInClass
import kotlin.properties.Delegates
fun main(args: Array<String>) {
val a = A()
//Breakpoint!
args.size
}
class A {
val prop by MyDelegate()
val propEx by MyDelegateThrowsException()
}
class MyDelegate {
fun get(t: Any?, p: PropertyMetadata): Int = 1
}
class MyDelegateThrowsException {
fun get(t: Any?, p: PropertyMetadata): Int = throw IllegalStateException()
}
// PRINT_FRAME
@@ -188,6 +188,12 @@ public class KotlinEvaluateExpressionTestGenerated extends AbstractKotlinEvaluat
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/debugger/tinyApp/src/evaluate/singleBreakpoint/frame"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("delegatedPropertyInClass.kt")
public void testDelegatedPropertyInClass() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/debugger/tinyApp/src/evaluate/singleBreakpoint/frame/delegatedPropertyInClass.kt");
doSingleBreakpointTest(fileName);
}
@TestMetadata("frameAnonymousObject.kt")
public void testFrameAnonymousObject() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/debugger/tinyApp/src/evaluate/singleBreakpoint/frame/frameAnonymousObject.kt");