Skip to content

Commit 27add0f

Browse files
Debugger: correct scope for runtime type calculation
1 parent c6b6e9f commit 27add0f

File tree

6 files changed

+17
-15
lines changed

6 files changed

+17
-15
lines changed

ChangeLog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111

1212
### IDE
1313

14+
#### Debugger
15+
16+
- Do not cast to runtime type unavailable in current scope
17+
1418
## 1.0.2
1519

1620
### Compiler

idea/src/org/jetbrains/kotlin/idea/debugger/evaluate/FrameVisitor.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import org.jetbrains.kotlin.resolve.jvm.AsmTypes
3535
import org.jetbrains.org.objectweb.asm.Type
3636

3737
class FrameVisitor(context: EvaluationContextImpl) {
38-
private val project = context.debugProcess.project
38+
private val scope = context.debugProcess.searchScope
3939
private val frame = context.frameProxy
4040

4141
companion object {
@@ -177,16 +177,15 @@ class FrameVisitor(context: EvaluationContextImpl) {
177177

178178
private fun isValueOfCorrectType(value: Value, asmType: Type?, shouldCheckType: Boolean): Boolean {
179179
if (!shouldCheckType || asmType == null || value.asmType == asmType) return true
180-
if (project == null) return false
181180

182181
if (asmType == OBJECT_TYPE) return true
183182

184183
if ((value.obj() as? com.sun.jdi.ObjectReference)?.referenceType().isSubclass(asmType.className)) {
185184
return true
186185
}
187186

188-
val thisDesc = value.asmType.getClassDescriptor(project)
189-
val expDesc = asmType.getClassDescriptor(project)
187+
val thisDesc = value.asmType.getClassDescriptor(scope)
188+
val expDesc = asmType.getClassDescriptor(scope)
190189
return thisDesc != null && expDesc != null && runReadAction { DescriptorUtils.isSubclass(thisDesc, expDesc) }
191190
}
192191

idea/src/org/jetbrains/kotlin/idea/debugger/evaluate/KotlinCodeFragmentFactory.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ class KotlinCodeFragmentFactory: CodeFragmentFactory() {
262262
}
263263

264264
private fun createKotlinProperty(project: Project, variableName: String, variableTypeName: String, value: Value): String? {
265-
val actualClassDescriptor = value.asValue().asmType.getClassDescriptor(project)
265+
val actualClassDescriptor = value.asValue().asmType.getClassDescriptor(GlobalSearchScope.allScope(project))
266266
if (actualClassDescriptor != null && actualClassDescriptor.defaultType.arguments.isEmpty()) {
267267
val renderedType = IdeDescriptorRenderers.SOURCE_CODE.renderType(actualClassDescriptor.defaultType.makeNullable())
268268
return "val ${variableName.quoteIfNeeded()}: $renderedType = null"

idea/src/org/jetbrains/kotlin/idea/debugger/evaluate/KotlinDebuggerCaches.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ class KotlinDebuggerCaches(private val project: Project) {
187187
val value = frameVisitor.findValue(name, asmType = null, checkType = false, failIfNotFound = false)
188188
if (value == null) return@all false
189189

190-
val thisDescriptor = value.asmType.getClassDescriptor(project)
190+
val thisDescriptor = value.asmType.getClassDescriptor(context.debugProcess.searchScope)
191191
val superClassDescriptor = jetType.constructor.declarationDescriptor as? ClassDescriptor
192192
return@all thisDescriptor != null && superClassDescriptor != null && runReadAction { DescriptorUtils.isSubclass(thisDescriptor, superClassDescriptor) }
193193
}

idea/src/org/jetbrains/kotlin/idea/debugger/evaluate/KotlinEvaluationBuilder.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import com.intellij.diagnostic.LogMessageEx
2626
import com.intellij.openapi.diagnostic.Attachment
2727
import com.intellij.openapi.diagnostic.Logger
2828
import com.intellij.openapi.progress.ProcessCanceledException
29-
import com.intellij.openapi.project.Project
3029
import com.intellij.openapi.vfs.CharsetToolkit
3130
import com.intellij.psi.JavaPsiFacade
3231
import com.intellij.psi.PsiDocumentManager
@@ -552,7 +551,7 @@ private fun SuspendContext.getInvokePolicy(): Int {
552551
return if (suspendPolicy == EventRequest.SUSPEND_EVENT_THREAD) ObjectReference.INVOKE_SINGLE_THREADED else 0
553552
}
554553

555-
fun Type.getClassDescriptor(project: Project): ClassDescriptor? {
554+
fun Type.getClassDescriptor(scope: GlobalSearchScope): ClassDescriptor? {
556555
if (AsmUtil.isPrimitive(this)) return null
557556

558557
val jvmName = JvmClassName.byInternalName(internalName).fqNameForClassNameWithoutDollars
@@ -561,7 +560,7 @@ fun Type.getClassDescriptor(project: Project): ClassDescriptor? {
561560
if (platformClasses.isNotEmpty()) return platformClasses.first()
562561

563562
return runReadAction {
564-
val classes = JavaPsiFacade.getInstance(project).findClasses(jvmName.asString(), GlobalSearchScope.allScope(project))
563+
val classes = JavaPsiFacade.getInstance(scope.project).findClasses(jvmName.asString(), scope)
565564
if (classes.isEmpty()) null
566565
else {
567566
classes.first().getJavaClassDescriptor()

idea/src/org/jetbrains/kotlin/idea/debugger/evaluate/KotlinRuntimeTypeEvaluator.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ import com.intellij.debugger.ui.EditorEvaluationCommand
2929
import com.intellij.openapi.editor.Editor
3030
import com.intellij.openapi.progress.ProcessCanceledException
3131
import com.intellij.openapi.progress.ProgressIndicator
32-
import com.intellij.openapi.project.Project
3332
import com.intellij.psi.CommonClassNames
33+
import com.intellij.psi.search.GlobalSearchScope
3434
import com.sun.jdi.ClassType
3535
import com.sun.jdi.Value
3636
import org.jetbrains.eval4j.jdi.asValue
@@ -73,16 +73,16 @@ abstract class KotlinRuntimeTypeEvaluator(
7373

7474
val value = evaluator.evaluate(evaluationContext)
7575
if (value != null) {
76-
return getCastableRuntimeType(project, value)
76+
return getCastableRuntimeType(evaluationContext.debugProcess.searchScope, value)
7777
}
7878

7979
throw EvaluateExceptionUtil.createEvaluateException(DebuggerBundle.message("evaluation.error.surrounded.expression.null"))
8080
}
8181

8282
companion object {
83-
private fun getCastableRuntimeType(project: Project, value: Value): KotlinType? {
83+
private fun getCastableRuntimeType(scope: GlobalSearchScope, value: Value): KotlinType? {
8484
val myValue = value.asValue()
85-
var psiClass = myValue.asmType.getClassDescriptor(project)
85+
var psiClass = myValue.asmType.getClassDescriptor(scope)
8686
if (psiClass != null) {
8787
return psiClass.defaultType
8888
}
@@ -91,14 +91,14 @@ abstract class KotlinRuntimeTypeEvaluator(
9191
if (type is ClassType) {
9292
val superclass = type.superclass()
9393
if (superclass != null && CommonClassNames.JAVA_LANG_OBJECT != superclass.name()) {
94-
psiClass = AsmType.getType(superclass.signature()).getClassDescriptor(project)
94+
psiClass = AsmType.getType(superclass.signature()).getClassDescriptor(scope)
9595
if (psiClass != null) {
9696
return psiClass.defaultType
9797
}
9898
}
9999

100100
for (interfaceType in type.interfaces()) {
101-
psiClass = AsmType.getType(interfaceType.signature()).getClassDescriptor(project)
101+
psiClass = AsmType.getType(interfaceType.signature()).getClassDescriptor(scope)
102102
if (psiClass != null) {
103103
return psiClass.defaultType
104104
}

0 commit comments

Comments
 (0)