From 1c3ce93275683612149af9b85e6f9ab244558cc1 Mon Sep 17 00:00:00 2001 From: Mikhael Bogdanov Date: Tue, 24 May 2016 16:11:14 +0300 Subject: [PATCH] Generate same delagation structure as in jvm 6 target until new binary compatibility design We need to make some decision about binary compatibility beetwen targets and semantics, so now old logic is used --- .../codegen/ImplementationBodyCodegen.java | 2 +- .../codegen/java8/box/jvm8/diamond.kt | 48 ++++++++++++++++ .../noDelegationToDefaultMethodInClass.kt | 15 ++--- .../noDelegationToDefaultMethodInInterface.kt | 15 ++--- ...noDelegationToDefaultMethodInInterface2.kt | 15 ++--- .../delegationToDefaultMethodInInterface2.kt | 34 +++++++---- .../jvm8against6/delegation/diamond.kt | 56 +++++++++++++++++++ .../jvm8against6/delegation/diamond2.kt | 50 +++++++++++++++++ .../jvm8against6/delegation/diamond3.kt | 52 +++++++++++++++++ ...BlackBoxWithJava8CodegenTestGenerated.java | 6 ++ ...mpileKotlinAgainstKotlinTestGenerated.java | 18 ++++++ 11 files changed, 278 insertions(+), 33 deletions(-) create mode 100644 compiler/testData/codegen/java8/box/jvm8/diamond.kt create mode 100644 compiler/testData/codegen/java8/compileKotlinAgainstKotlin/jvm8against6/delegation/diamond.kt create mode 100644 compiler/testData/codegen/java8/compileKotlinAgainstKotlin/jvm8against6/delegation/diamond2.kt create mode 100644 compiler/testData/codegen/java8/compileKotlinAgainstKotlin/jvm8against6/delegation/diamond3.kt diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/ImplementationBodyCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/ImplementationBodyCodegen.java index 28201d70fa8..1c09d6ad7f4 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/ImplementationBodyCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/ImplementationBodyCodegen.java @@ -1326,7 +1326,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen { for (Map.Entry entry : CodegenUtil.getNonPrivateTraitMethods(descriptor).entrySet()) { FunctionDescriptor traitFun = entry.getKey(); //skip java 8 default methods - if (!(traitFun instanceof JavaCallableMemberDescriptor || isJvm8InterfaceMember(traitFun, state))) { + if (!(traitFun instanceof JavaCallableMemberDescriptor)) { generateDelegationToTraitImpl(traitFun, entry.getValue()); } } diff --git a/compiler/testData/codegen/java8/box/jvm8/diamond.kt b/compiler/testData/codegen/java8/box/jvm8/diamond.kt new file mode 100644 index 00000000000..b9d638d2bd3 --- /dev/null +++ b/compiler/testData/codegen/java8/box/jvm8/diamond.kt @@ -0,0 +1,48 @@ +// KOTLIN_CONFIGURATION_FLAGS: +JVM.JVM_8_TARGET +// WITH_REFLECT +// FULL_JDK +interface Test { + fun test(): String { + return "Test" + } +} + +open class TestClass : Test { + +} + + +interface Test2 : Test { + override fun test(): String { + return "Test2" + } +} + +interface Test3 : Test2 { + +} + + +class TestClass2 : TestClass(), Test3 { + +} + +fun box(): String { + val test = TestClass2().test() + if (test != "Test2") return "fail 1: $test" +// checkNoMethod(TestClass::class.java, "test") +// checkNoMethod(Test3::class.java, "test") +// checkNoMethod(TestClass2::class.java, "test") + + return "OK" +} + +fun checkNoMethod(clazz: Class<*>, name: String) { + try { + clazz.getDeclaredMethod("test") + } + catch (e: NoSuchMethodException) { + return + } + throw java.lang.AssertionError("fail " + clazz) +} \ No newline at end of file diff --git a/compiler/testData/codegen/java8/box/jvm8/noDelegation/noDelegationToDefaultMethodInClass.kt b/compiler/testData/codegen/java8/box/jvm8/noDelegation/noDelegationToDefaultMethodInClass.kt index 9e53d8d7249..9320788869a 100644 --- a/compiler/testData/codegen/java8/box/jvm8/noDelegation/noDelegationToDefaultMethodInClass.kt +++ b/compiler/testData/codegen/java8/box/jvm8/noDelegation/noDelegationToDefaultMethodInClass.kt @@ -11,11 +11,12 @@ class TestClass : Test { } fun box(): String { - try { - TestClass::class.java.getDeclaredMethod("test") - } - catch (e: NoSuchMethodException) { - return "OK" - } - return "fail" +// try { +// TestClass::class.java.getDeclaredMethod("test") +// } +// catch (e: NoSuchMethodException) { +// return "OK" +// } +// return "fail" + return "OK" } \ No newline at end of file diff --git a/compiler/testData/codegen/java8/box/jvm8/noDelegation/noDelegationToDefaultMethodInInterface.kt b/compiler/testData/codegen/java8/box/jvm8/noDelegation/noDelegationToDefaultMethodInInterface.kt index 2c5148312ec..7828d579bb8 100644 --- a/compiler/testData/codegen/java8/box/jvm8/noDelegation/noDelegationToDefaultMethodInInterface.kt +++ b/compiler/testData/codegen/java8/box/jvm8/noDelegation/noDelegationToDefaultMethodInInterface.kt @@ -11,11 +11,12 @@ interface Test2 : Test { } fun box(): String { - try { - Test2::class.java.getDeclaredMethod("test") - } - catch (e: NoSuchMethodException) { - return "OK" - } - return "fail" +// try { +// Test2::class.java.getDeclaredMethod("test") +// } +// catch (e: NoSuchMethodException) { +// return "OK" +// } +// return "fail" + return "OK" } \ No newline at end of file diff --git a/compiler/testData/codegen/java8/box/jvm8/noDelegation/noDelegationToDefaultMethodInInterface2.kt b/compiler/testData/codegen/java8/box/jvm8/noDelegation/noDelegationToDefaultMethodInInterface2.kt index 29aae95f634..c8481d01b26 100644 --- a/compiler/testData/codegen/java8/box/jvm8/noDelegation/noDelegationToDefaultMethodInInterface2.kt +++ b/compiler/testData/codegen/java8/box/jvm8/noDelegation/noDelegationToDefaultMethodInInterface2.kt @@ -15,11 +15,12 @@ interface Test3 : Test2 { } fun box(): String { - try { - Test3::class.java.getDeclaredMethod("test") - } - catch (e: NoSuchMethodException) { - return "OK" - } - return "fail" +// try { +// Test3::class.java.getDeclaredMethod("test") +// } +// catch (e: NoSuchMethodException) { +// return "OK" +// } +// return "fail" + return "OK" } \ No newline at end of file diff --git a/compiler/testData/codegen/java8/compileKotlinAgainstKotlin/jvm8against6/delegation/delegationToDefaultMethodInInterface2.kt b/compiler/testData/codegen/java8/compileKotlinAgainstKotlin/jvm8against6/delegation/delegationToDefaultMethodInInterface2.kt index 1a831331dbe..32eafa4010c 100644 --- a/compiler/testData/codegen/java8/compileKotlinAgainstKotlin/jvm8against6/delegation/delegationToDefaultMethodInInterface2.kt +++ b/compiler/testData/codegen/java8/compileKotlinAgainstKotlin/jvm8against6/delegation/delegationToDefaultMethodInInterface2.kt @@ -3,7 +3,8 @@ // FILE: 1.kt interface Test { - fun test() { + fun test(): String { + return "OK" } } @@ -16,20 +17,31 @@ interface Test2 : Test { interface Test3 : Test2 { } +class TestClass : Test3 fun box(): String { - try { - Test2::class.java.getDeclaredMethod("test") - } - catch (e: NoSuchMethodException) { - return "fail 1" - } + checkPresent(Test2::class.java, "test") +// checkNoMethod(Test3::class.java, "test") + return TestClass().test() +} + + + +fun checkNoMethod(clazz: Class<*>, name: String) { try { - Test3::class.java.getDeclaredMethod("test") + clazz.getDeclaredMethod(name) + } catch (e: NoSuchMethodException) { + return } - catch (e: NoSuchMethodException) { - return "OK" + throw java.lang.AssertionError("Method $name exists in $clazz") +} + +fun checkPresent(clazz: Class<*>, name: String) { + try { + clazz.getDeclaredMethod(name) + } catch (e: NoSuchMethodException) { + throw java.lang.AssertionError("Method $name doesn't exist in $clazz") } - return "fail 2" + return } \ No newline at end of file diff --git a/compiler/testData/codegen/java8/compileKotlinAgainstKotlin/jvm8against6/delegation/diamond.kt b/compiler/testData/codegen/java8/compileKotlinAgainstKotlin/jvm8against6/delegation/diamond.kt new file mode 100644 index 00000000000..a6beb77c3ea --- /dev/null +++ b/compiler/testData/codegen/java8/compileKotlinAgainstKotlin/jvm8against6/delegation/diamond.kt @@ -0,0 +1,56 @@ +// WITH_REFLECT +// FULL_JDK + +// FILE: 1.kt +interface Test { + fun test(): String { + return "OK" + } +} + +// FILE: 2.kt +// KOTLIN_CONFIGURATION_FLAGS: +JVM.JVM_8_TARGET +interface Test2 : Test { + +} + +interface Test3 : Test { + +} + + +interface Test4 : Test2, Test3 { + +} + +class TestClass : Test4 { + +} + + +fun box(): String { + checkPresent(Test2::class.java, "test") + checkPresent(Test3::class.java, "test") + //checkNoMethod(Test4::class.java, "test") + + return TestClass().test() +} + + +fun checkNoMethod(clazz: Class<*>, name: String) { + try { + clazz.getDeclaredMethod(name) + } catch (e: NoSuchMethodException) { + return + } + throw java.lang.AssertionError("Method $name exists in $clazz") +} + +fun checkPresent(clazz: Class<*>, name: String) { + try { + clazz.getDeclaredMethod(name) + } catch (e: NoSuchMethodException) { + throw java.lang.AssertionError("Method $name doesn't exist in $clazz") + } + return +} \ No newline at end of file diff --git a/compiler/testData/codegen/java8/compileKotlinAgainstKotlin/jvm8against6/delegation/diamond2.kt b/compiler/testData/codegen/java8/compileKotlinAgainstKotlin/jvm8against6/delegation/diamond2.kt new file mode 100644 index 00000000000..1c4758561e0 --- /dev/null +++ b/compiler/testData/codegen/java8/compileKotlinAgainstKotlin/jvm8against6/delegation/diamond2.kt @@ -0,0 +1,50 @@ +// WITH_REFLECT +// FULL_JDK + +// FILE: 1.kt +interface Test { + fun test(): String { + return "OK" + } +} + +// FILE: 2.kt +// KOTLIN_CONFIGURATION_FLAGS: +JVM.JVM_8_TARGET +open class TestClass : Test { + +} + +interface Test2 : Test { + +} + + +class TestClass2 : TestClass(), Test2 { + +} + +fun box(): String { + checkPresent(TestClass::class.java, "test") + checkPresent(Test2::class.java, "test") + checkNoMethod(TestClass2::class.java, "test") + + return TestClass().test() +} + +fun checkNoMethod(clazz: Class<*>, name: String) { + try { + clazz.getDeclaredMethod(name) + } catch (e: NoSuchMethodException) { + return + } + throw java.lang.AssertionError("Method $name exists in $clazz") +} + +fun checkPresent(clazz: Class<*>, name: String) { + try { + clazz.getDeclaredMethod(name) + } catch (e: NoSuchMethodException) { + throw java.lang.AssertionError("Method $name doesn't exist in $clazz") + } + return +} \ No newline at end of file diff --git a/compiler/testData/codegen/java8/compileKotlinAgainstKotlin/jvm8against6/delegation/diamond3.kt b/compiler/testData/codegen/java8/compileKotlinAgainstKotlin/jvm8against6/delegation/diamond3.kt new file mode 100644 index 00000000000..c0aadd28cd9 --- /dev/null +++ b/compiler/testData/codegen/java8/compileKotlinAgainstKotlin/jvm8against6/delegation/diamond3.kt @@ -0,0 +1,52 @@ +// WITH_REFLECT +// FULL_JDK + +// FILE: 1.kt +interface Test { + fun test(): String { + return "fail" + } +} + +// FILE: 2.kt +// KOTLIN_CONFIGURATION_FLAGS: +JVM.JVM_8_TARGET +open class TestClass : Test { + +} + +interface Test2 : Test { + override fun test(): String { + return "OK" + } +} + + +class TestClass2 : TestClass(), Test2 { + +} + +fun box(): String { + checkPresent(TestClass::class.java, "test") + checkPresent(Test2::class.java, "test") + checkPresent(TestClass2::class.java, "test") + + return TestClass2().test() +} + +fun checkNoMethod(clazz: Class<*>, name: String) { + try { + clazz.getDeclaredMethod(name) + } catch (e: NoSuchMethodException) { + return + } + throw java.lang.AssertionError("Method $name exists in $clazz") +} + +fun checkPresent(clazz: Class<*>, name: String) { + try { + clazz.getDeclaredMethod(name) + } catch (e: NoSuchMethodException) { + throw java.lang.AssertionError("Method $name doesn't exist in $clazz") + } + return +} \ No newline at end of file diff --git a/compiler/tests-java8/tests/org/jetbrains/kotlin/codegen/BlackBoxWithJava8CodegenTestGenerated.java b/compiler/tests-java8/tests/org/jetbrains/kotlin/codegen/BlackBoxWithJava8CodegenTestGenerated.java index 0cb18377821..706388203ec 100644 --- a/compiler/tests-java8/tests/org/jetbrains/kotlin/codegen/BlackBoxWithJava8CodegenTestGenerated.java +++ b/compiler/tests-java8/tests/org/jetbrains/kotlin/codegen/BlackBoxWithJava8CodegenTestGenerated.java @@ -139,6 +139,12 @@ public class BlackBoxWithJava8CodegenTestGenerated extends AbstractBlackBoxCodeg doTest(fileName); } + @TestMetadata("diamond.kt") + public void testDiamond() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/java8/box/jvm8/diamond.kt"); + doTest(fileName); + } + @TestMetadata("simpleCall.kt") public void testSimpleCall() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/java8/box/jvm8/simpleCall.kt"); diff --git a/compiler/tests-java8/tests/org/jetbrains/kotlin/codegen/CompileKotlinAgainstKotlinTestGenerated.java b/compiler/tests-java8/tests/org/jetbrains/kotlin/codegen/CompileKotlinAgainstKotlinTestGenerated.java index 66ad2ad615b..b4ccaea711c 100644 --- a/compiler/tests-java8/tests/org/jetbrains/kotlin/codegen/CompileKotlinAgainstKotlinTestGenerated.java +++ b/compiler/tests-java8/tests/org/jetbrains/kotlin/codegen/CompileKotlinAgainstKotlinTestGenerated.java @@ -95,6 +95,24 @@ public class CompileKotlinAgainstKotlinTestGenerated extends AbstractCompileKotl String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/java8/compileKotlinAgainstKotlin/jvm8against6/delegation/delegationToDefaultMethodInInterface2.kt"); doTest(fileName); } + + @TestMetadata("diamond.kt") + public void testDiamond() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/java8/compileKotlinAgainstKotlin/jvm8against6/delegation/diamond.kt"); + doTest(fileName); + } + + @TestMetadata("diamond2.kt") + public void testDiamond2() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/java8/compileKotlinAgainstKotlin/jvm8against6/delegation/diamond2.kt"); + doTest(fileName); + } + + @TestMetadata("diamond3.kt") + public void testDiamond3() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/java8/compileKotlinAgainstKotlin/jvm8against6/delegation/diamond3.kt"); + doTest(fileName); + } } } }