Properly calculate outermost parent class for sam wrapper

#KT-22906 Fixed
This commit is contained in:
Mikhael Bogdanov
2018-02-19 09:37:47 +01:00
parent 6d035f1d28
commit cc4ab832b7
8 changed files with 168 additions and 4 deletions
@@ -52,6 +52,7 @@ public class SpecialFiles {
excludedFiles.add("kt17091.kt");
excludedFiles.add("kt17091_2.kt");
excludedFiles.add("kt17091_3.kt");
excludedFiles.add("kt22906.kt");
// Reflection is used to check full class name
excludedFiles.add("native");
@@ -17,6 +17,7 @@
package org.jetbrains.kotlin.codegen;
import kotlin.collections.CollectionsKt;
import kotlin.text.StringsKt;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.backend.common.CodegenUtil;
import org.jetbrains.kotlin.codegen.state.GenerationState;
@@ -26,10 +27,8 @@ import org.jetbrains.kotlin.descriptors.impl.ClassDescriptorImpl;
import org.jetbrains.kotlin.descriptors.impl.SimpleFunctionDescriptorImpl;
import org.jetbrains.kotlin.fileClasses.JvmFileClassUtil;
import org.jetbrains.kotlin.incremental.components.NoLookupLocation;
import org.jetbrains.kotlin.load.java.descriptors.JavaClassDescriptor;
import org.jetbrains.kotlin.name.FqName;
import org.jetbrains.kotlin.name.Name;
import org.jetbrains.kotlin.psi.KtDeclaration;
import org.jetbrains.kotlin.psi.KtFile;
import org.jetbrains.kotlin.resolve.DescriptorUtils;
import org.jetbrains.kotlin.resolve.descriptorUtil.DescriptorUtilsKt;
@@ -200,9 +199,10 @@ public class SamWrapperCodegen {
outermostOwner = filePartFqName;
}
else {
ClassifierDescriptor outermostClassifier = DescriptorUtils.getParentOfType(contextDescriptor, ClassDescriptor.class);
ClassifierDescriptor outermostClassifier = getOutermostParentClass(contextDescriptor);
if (outermostClassifier == null) throw new IllegalStateException("Can't find outermost parent class for " + contextDescriptor);
outermostOwner = filePartFqName.parent().child(outermostClassifier.getName());
String internalName = typeMapper.mapType(outermostClassifier).getInternalName();
outermostOwner = filePartFqName.parent().child(Name.identifier(StringsKt.substringAfterLast(internalName, '/', internalName)));
}
String shortName = String.format(
@@ -213,4 +213,15 @@ public class SamWrapperCodegen {
);
return outermostOwner.parent().child(Name.identifier(shortName));
}
private static ClassDescriptor getOutermostParentClass(CallableMemberDescriptor contextDescriptor) {
ClassDescriptor parent = DescriptorUtils.getParentOfType(contextDescriptor, ClassDescriptor.class, true);
ClassDescriptor next;
do {
next = DescriptorUtils.getParentOfType(parent, ClassDescriptor.class, true);
if (next != null) parent = next;
}
while (next != null);
return parent;
}
}
+36
View File
@@ -0,0 +1,36 @@
// TARGET_BACKEND: JVM
// WITH_RUNTIME
// FILE: Foo.kt
@file:JvmMultifileClass
@file:JvmName("testX")
package test
class A2 {
fun doWork(job: () -> Unit) {
Runnable(job)
Runnable(job)
Runnable(job)
}
fun foo(job: () -> Unit) {
Runnable(job)
Runnable(job)
Runnable(job)
}
}
// FILE: kt17091_3.kt
fun box(): String {
if (java.lang.Class.forName("Kt17091_3Kt\$sam\$java_util_concurrent_Callable$0") == null) return "fail: can't find sam wrapper"
if (java.lang.Class.forName("test.A2\$sam\$java_lang_Runnable$0") == null) return "fail 2: can't find sam wrapper"
return A().foo().call()
}
class A {
val f = {"OK"}
fun foo() = java.util.concurrent.Callable(f)
}
+26
View File
@@ -0,0 +1,26 @@
// TARGET_BACKEND: JVM
// WITH_RUNTIME
// FILE: kt22906_1.kt
package test
class C {
fun startTemplate(): String {
val y = object {
fun foo(): String {
val job = { "OK" }
return java.util.concurrent.Callable(job).call()
}
}
return y.foo()
}
}
// FILE: kt22906_2.kt
import test.*
fun box(): String {
if (java.lang.Class.forName("test.C\$sam\$java_util_concurrent_Callable\$0") == null) return "fail: can't find sam wrapper"
return C().startTemplate()
}
+36
View File
@@ -0,0 +1,36 @@
// TARGET_BACKEND: JVM
// WITH_RUNTIME
// FILE: kt22906_1.kt
package test
class C {
fun startTemplate(): String {
val y = object {
fun foo(): String {
val job = { "OK" }
return java.util.concurrent.Callable(job).call()
}
}
return y.foo()
}
fun foo() {
val y = object {
fun foo(): String {
val job = { "OK2" }
return java.util.concurrent.Callable(job).call()
}
}
}
}
// FILE: kt22906_2.kt
import test.*
fun box(): String {
if (java.lang.Class.forName("test.C\$sam\$java_util_concurrent_Callable\$0") == null) return "fail: can't find sam wrapper"
return C().startTemplate()
}
@@ -19503,6 +19503,24 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
doTest(fileName);
}
@TestMetadata("kt17091_4.kt")
public void testKt17091_4() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/sam/kt17091_4.kt");
doTest(fileName);
}
@TestMetadata("kt22906.kt")
public void testKt22906() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/sam/kt22906.kt");
doTest(fileName);
}
@TestMetadata("kt22906_2.kt")
public void testKt22906_2() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/sam/kt22906_2.kt");
doTest(fileName);
}
@TestMetadata("compiler/testData/codegen/box/sam/constructors")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -19503,6 +19503,24 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
doTest(fileName);
}
@TestMetadata("kt17091_4.kt")
public void testKt17091_4() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/sam/kt17091_4.kt");
doTest(fileName);
}
@TestMetadata("kt22906.kt")
public void testKt22906() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/sam/kt22906.kt");
doTest(fileName);
}
@TestMetadata("kt22906_2.kt")
public void testKt22906_2() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/sam/kt22906_2.kt");
doTest(fileName);
}
@TestMetadata("compiler/testData/codegen/box/sam/constructors")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -19503,6 +19503,24 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
doTest(fileName);
}
@TestMetadata("kt17091_4.kt")
public void testKt17091_4() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/sam/kt17091_4.kt");
doTest(fileName);
}
@TestMetadata("kt22906.kt")
public void testKt22906() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/sam/kt22906.kt");
doTest(fileName);
}
@TestMetadata("kt22906_2.kt")
public void testKt22906_2() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/sam/kt22906_2.kt");
doTest(fileName);
}
@TestMetadata("compiler/testData/codegen/box/sam/constructors")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)