From abc7d5101daffbbd560a5cbaeb7511f19d4e5dcb Mon Sep 17 00:00:00 2001 From: Michael Bogdanov Date: Sun, 26 Jun 2016 18:19:02 +0300 Subject: [PATCH] Fix for KT-12106: import static of reified companion object method throws IllegalAccessError #KT-12106 Fixed --- .../kotlin/codegen/inline/InlineCodegen.java | 12 ++++++++++- .../kotlin/resolve/importedFromObject.kt | 2 +- .../boxInline/simple/funImportedFromObject.kt | 19 ++++++++++++++++++ .../simple/propImportedFromObject.kt | 20 +++++++++++++++++++ .../BlackBoxInlineCodegenTestGenerated.java | 12 +++++++++++ ...otlinAgainstInlineKotlinTestGenerated.java | 12 +++++++++++ 6 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 compiler/testData/codegen/boxInline/simple/funImportedFromObject.kt create mode 100644 compiler/testData/codegen/boxInline/simple/propImportedFromObject.kt diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InlineCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InlineCodegen.java index dbb38b3a657..620f52a52a7 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InlineCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InlineCodegen.java @@ -39,6 +39,7 @@ import org.jetbrains.kotlin.psi.*; import org.jetbrains.kotlin.resolve.BindingContext; import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils; import org.jetbrains.kotlin.resolve.DescriptorUtils; +import org.jetbrains.kotlin.resolve.ImportedFromObjectCallableDescriptor; import org.jetbrains.kotlin.resolve.annotations.AnnotationUtilKt; import org.jetbrains.kotlin.resolve.calls.callUtil.CallUtilKt; import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall; @@ -235,7 +236,7 @@ public class InlineCodegen extends CallGenerator { : jvmSignature.getAsmMethod(); MethodId methodId = new MethodId(DescriptorUtils.getFqNameSafe(functionDescriptor.getContainingDeclaration()), asmMethod); - final CallableMemberDescriptor directMember = JvmCodegenUtil.getDirectMember(functionDescriptor); + final CallableMemberDescriptor directMember = getDirectMemberAndCallableFromObject(functionDescriptor); if (!isBuiltInArrayIntrinsic(functionDescriptor) && !(directMember instanceof DeserializedCallableMemberDescriptor)) { return doCreateMethodNodeFromSource(functionDescriptor, jvmSignature, codegen, context, callDefault, state, asmMethod); } @@ -256,6 +257,15 @@ public class InlineCodegen extends CallGenerator { return resultInCache.copyWithNewNode(cloneMethodNode(resultInCache.getNode())); } + @NotNull + private static CallableMemberDescriptor getDirectMemberAndCallableFromObject(@NotNull FunctionDescriptor functionDescriptor) { + CallableMemberDescriptor directMember = JvmCodegenUtil.getDirectMember(functionDescriptor); + if (directMember instanceof ImportedFromObjectCallableDescriptor) { + return ((ImportedFromObjectCallableDescriptor) directMember).getCallableFromObject(); + } + return directMember; + } + @NotNull private static MethodNode cloneMethodNode(@NotNull MethodNode methodNode) { methodNode.instructions.resetLabels(); diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/importedFromObject.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/importedFromObject.kt index b77e897da27..5124fb69bd0 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/importedFromObject.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/importedFromObject.kt @@ -19,7 +19,7 @@ package org.jetbrains.kotlin.resolve import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.types.TypeSubstitutor -abstract class ImportedFromObjectCallableDescriptor( +abstract class ImportedFromObjectCallableDescriptor( val callableFromObject: TCallable ) : CallableDescriptor { val containingObject = callableFromObject.containingDeclaration as ClassDescriptor diff --git a/compiler/testData/codegen/boxInline/simple/funImportedFromObject.kt b/compiler/testData/codegen/boxInline/simple/funImportedFromObject.kt new file mode 100644 index 00000000000..6d1a9f4d4be --- /dev/null +++ b/compiler/testData/codegen/boxInline/simple/funImportedFromObject.kt @@ -0,0 +1,19 @@ +// FILE: 1.kt + +package test + +object Host { + // private final foo()V + inline fun foo(): String { + return "OK" + } +} + + +// FILE: 2.kt + +import test.Host.foo + +fun box(): String { + return foo() +} diff --git a/compiler/testData/codegen/boxInline/simple/propImportedFromObject.kt b/compiler/testData/codegen/boxInline/simple/propImportedFromObject.kt new file mode 100644 index 00000000000..9637b0a19a7 --- /dev/null +++ b/compiler/testData/codegen/boxInline/simple/propImportedFromObject.kt @@ -0,0 +1,20 @@ +// WITH_RUNTIME +// FILE: 1.kt + +package test + +object Host { + + inline val T.foo: String + get() = T::class.java.simpleName +} + +// FILE: 2.kt + +import test.Host.foo + +class OK + +fun box(): String { + return OK().foo +} diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxInlineCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxInlineCodegenTestGenerated.java index b5f8ce02f5f..3566c2fc1d4 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxInlineCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxInlineCodegenTestGenerated.java @@ -1789,12 +1789,24 @@ public class BlackBoxInlineCodegenTestGenerated extends AbstractBlackBoxInlineCo doTest(fileName); } + @TestMetadata("funImportedFromObject.kt") + public void testFunImportedFromObject() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/simple/funImportedFromObject.kt"); + doTest(fileName); + } + @TestMetadata("params.kt") public void testParams() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/simple/params.kt"); doTest(fileName); } + @TestMetadata("propImportedFromObject.kt") + public void testPropImportedFromObject() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/simple/propImportedFromObject.kt"); + doTest(fileName); + } + @TestMetadata("rootConstructor.kt") public void testRootConstructor() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/simple/rootConstructor.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/CompileKotlinAgainstInlineKotlinTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/CompileKotlinAgainstInlineKotlinTestGenerated.java index 62b573df4d0..9089e877f16 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/CompileKotlinAgainstInlineKotlinTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/CompileKotlinAgainstInlineKotlinTestGenerated.java @@ -1789,12 +1789,24 @@ public class CompileKotlinAgainstInlineKotlinTestGenerated extends AbstractCompi doTest(fileName); } + @TestMetadata("funImportedFromObject.kt") + public void testFunImportedFromObject() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/simple/funImportedFromObject.kt"); + doTest(fileName); + } + @TestMetadata("params.kt") public void testParams() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/simple/params.kt"); doTest(fileName); } + @TestMetadata("propImportedFromObject.kt") + public void testPropImportedFromObject() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/simple/propImportedFromObject.kt"); + doTest(fileName); + } + @TestMetadata("rootConstructor.kt") public void testRootConstructor() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/simple/rootConstructor.kt");