From 713dc589e066e3db0b09bf45c3017d647203bc10 Mon Sep 17 00:00:00 2001 From: Yan Zhulanow Date: Mon, 17 Sep 2018 21:24:30 +0300 Subject: [PATCH] Debugger: Allow to evaluate private properties from companion objects (KT-26795) --- .../kotlin/codegen/ExpressionCodegen.java | 2 +- .../kotlin/codegen/JvmCodegenUtil.java | 2 +- .../privateFieldInCompanion.kt | 42 +++++++++++++++++++ .../privateFieldInCompanion.out | 13 ++++++ ...KotlinEvaluateExpressionTestGenerated.java | 5 +++ 5 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 idea/testData/debugger/tinyApp/src/evaluate/singleBreakpoint/privateFieldInCompanion.kt create mode 100644 idea/testData/debugger/tinyApp/src/evaluate/singleBreakpoint/privateFieldInCompanion.out diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java index ddecf7a05e7..f16658303b7 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java @@ -2049,7 +2049,7 @@ public class ExpressionCodegen extends KtVisitor impleme (forceField || (Visibilities.isPrivate(propertyDescriptor.getVisibility()) && isDefaultAccessor(propertyDescriptor.getGetter()) && isDefaultAccessor(propertyDescriptor.getSetter())))) { - fieldAccessorKind = AccessorKind.IN_CLASS_COMPANION; + fieldAccessorKind = JvmCodegenUtil.isDebuggerContext(context) ? AccessorKind.NORMAL : AccessorKind.IN_CLASS_COMPANION; } else if ((syntheticBackingField && context.getFirstCrossInlineOrNonInlineContext().getParentContext().getContextDescriptor() != containingDeclaration)) { diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/JvmCodegenUtil.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/JvmCodegenUtil.java index 3b1a4fef03e..d8e05e08698 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/JvmCodegenUtil.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/JvmCodegenUtil.java @@ -247,7 +247,7 @@ public class JvmCodegenUtil { return Visibilities.isPrivate(accessor.getVisibility()) || accessor.getModality() == FINAL; } - private static boolean isDebuggerContext(@NotNull CodegenContext context) { + public static boolean isDebuggerContext(@NotNull CodegenContext context) { KtFile file = DescriptorToSourceUtils.getContainingFile(context.getContextDescriptor()); return file != null && CodeFragmentUtilKt.getSuppressDiagnosticsInDebugMode(file); } diff --git a/idea/testData/debugger/tinyApp/src/evaluate/singleBreakpoint/privateFieldInCompanion.kt b/idea/testData/debugger/tinyApp/src/evaluate/singleBreakpoint/privateFieldInCompanion.kt new file mode 100644 index 00000000000..9b717751098 --- /dev/null +++ b/idea/testData/debugger/tinyApp/src/evaluate/singleBreakpoint/privateFieldInCompanion.kt @@ -0,0 +1,42 @@ +package privateFieldInCompanion + +class Foo { + companion object { + private const val PRIVATE_CONST = 1 + const val PUBLIC_CONST = 2 + + private val PRIVATE_VAL = 3 + val PUBLIC_VAL = 4 + + private val PRIVATE_STATIC = 5 + val PUBLIC_STATIC = 6 + } + + + fun foo() { + //Breakpoint! + val a = 5 + } +} + +fun main(args: Array) { + Foo().foo() +} + +// EXPRESSION: PRIVATE_CONST +// RESULT: 1: I + +// EXPRESSION: PUBLIC_CONST +// RESULT: 2: I + +// EXPRESSION: PRIVATE_VAL +// RESULT: 3: I + +// EXPRESSION: PUBLIC_VAL +// RESULT: 4: I + +// EXPRESSION: PRIVATE_STATIC +// RESULT: 5: I + +// EXPRESSION: PUBLIC_STATIC +// RESULT: 6: I diff --git a/idea/testData/debugger/tinyApp/src/evaluate/singleBreakpoint/privateFieldInCompanion.out b/idea/testData/debugger/tinyApp/src/evaluate/singleBreakpoint/privateFieldInCompanion.out new file mode 100644 index 00000000000..94bb925a573 --- /dev/null +++ b/idea/testData/debugger/tinyApp/src/evaluate/singleBreakpoint/privateFieldInCompanion.out @@ -0,0 +1,13 @@ +LineBreakpoint created at privateFieldInCompanion.kt:18 +Run Java +Connected to the target VM +privateFieldInCompanion.kt:18 +Compile bytecode for PRIVATE_CONST +Compile bytecode for PUBLIC_CONST +Compile bytecode for PRIVATE_VAL +Compile bytecode for PUBLIC_VAL +Compile bytecode for PRIVATE_STATIC +Compile bytecode for PUBLIC_STATIC +Disconnected from the target VM + +Process finished with exit code 0 diff --git a/idea/tests/org/jetbrains/kotlin/idea/debugger/evaluate/KotlinEvaluateExpressionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/debugger/evaluate/KotlinEvaluateExpressionTestGenerated.java index f35d247e1a2..50446a91c57 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/debugger/evaluate/KotlinEvaluateExpressionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/debugger/evaluate/KotlinEvaluateExpressionTestGenerated.java @@ -281,6 +281,11 @@ public class KotlinEvaluateExpressionTestGenerated extends AbstractKotlinEvaluat runTest("idea/testData/debugger/tinyApp/src/evaluate/singleBreakpoint/privateClass.kt"); } + @TestMetadata("privateFieldInCompanion.kt") + public void testPrivateFieldInCompanion() throws Exception { + runTest("idea/testData/debugger/tinyApp/src/evaluate/singleBreakpoint/privateFieldInCompanion.kt"); + } + @TestMetadata("privateMember.kt") public void testPrivateMember() throws Exception { runTest("idea/testData/debugger/tinyApp/src/evaluate/singleBreakpoint/privateMember.kt");