Support property accessor in compatibility mode

This commit is contained in:
Mikhael Bogdanov
2018-05-28 20:47:12 +02:00
parent 7f4bd549aa
commit 60f2dbeb8a
5 changed files with 94 additions and 1 deletions
@@ -410,6 +410,13 @@ public abstract class CodegenContext<T extends DeclarationDescriptor> {
public <D extends CallableMemberDescriptor> D getAccessorForJvmDefaultCompatibility(@NotNull D descriptor) {
if (descriptor instanceof PropertyAccessorDescriptor) {
PropertyDescriptor propertyAccessor = getAccessor(((PropertyAccessorDescriptor) descriptor).getCorrespondingProperty(),
AccessorKind.JVM_DEFAULT_COMPATIBILITY, null, null,
descriptor instanceof PropertyGetterDescriptor,
descriptor instanceof PropertySetterDescriptor);
return descriptor instanceof PropertyGetterDescriptor ? (D) propertyAccessor.getGetter() : (D) propertyAccessor.getSetter();
}
return getAccessor(descriptor, AccessorKind.JVM_DEFAULT_COMPATIBILITY, null, null);
}
@@ -491,7 +498,7 @@ public abstract class CodegenContext<T extends DeclarationDescriptor> {
}
else if (descriptor instanceof PropertyDescriptor) {
PropertyDescriptor propertyDescriptor = (PropertyDescriptor) descriptor;
if (accessorKind == AccessorKind.NORMAL) {
if (accessorKind == AccessorKind.NORMAL || accessorKind == AccessorKind.JVM_DEFAULT_COMPATIBILITY) {
AccessorForPropertyDescriptorFactory factory =
new AccessorForPropertyDescriptorFactory(propertyDescriptor, contextDescriptor, superCallTarget, nameSuffix, accessorKind);
propertyAccessorFactories.put(key, factory);
@@ -0,0 +1,24 @@
// !API_VERSION: 1.3
// !JVM_DEFAULT_MODE: compatibility
// JVM_TARGET: 1.8
// WITH_REFLECT
annotation class Property(val value: String)
annotation class Accessor(val value: String)
interface Z {
@Property("OK")
@JvmDefault
val z: String
@Accessor("OK")
get() = "OK"
}
class Test : Z
fun box() : String {
val value = Z::z.annotations.filterIsInstance<Property>().single().value
if (value != "OK") return value
return (Z::z.getter.annotations.single() as Accessor).value
}
@@ -0,0 +1,29 @@
// !API_VERSION: 1.3
// !JVM_DEFAULT_MODE: compatibility
// JVM_TARGET: 1.8
// WITH_RUNTIME
interface Test {
@JvmDefault
val test: String
get() = "OK"
@JvmDefault
var test2: String
get() = "OK"
set(field) {}
}
// TESTED_OBJECT_KIND: function
// TESTED_OBJECTS: Test, access$getTest$jd$jd
// FLAGS: ACC_PUBLIC, ACC_STATIC, ACC_SYNTHETIC
// TESTED_OBJECT_KIND: function
// TESTED_OBJECTS: Test, access$getTest2$jd$jd
// FLAGS: ACC_PUBLIC, ACC_STATIC, ACC_SYNTHETIC
// TESTED_OBJECT_KIND: function
// TESTED_OBJECTS: Test, access$setTest2$jd$jd
// FLAGS: ACC_PUBLIC, ACC_STATIC, ACC_SYNTHETIC
@@ -518,6 +518,11 @@ public class BlackBoxWithJava8CodegenTestGenerated extends AbstractBlackBoxCodeg
runTest("compiler/testData/codegen/java8/box/jvm8/defaults/compatibility/inheritedJvmDefault.kt");
}
@TestMetadata("propertyAnnotation.kt")
public void testPropertyAnnotation() throws Exception {
runTest("compiler/testData/codegen/java8/box/jvm8/defaults/compatibility/propertyAnnotation.kt");
}
@TestMetadata("simpleFunction.kt")
public void testSimpleFunction() throws Exception {
runTest("compiler/testData/codegen/java8/box/jvm8/defaults/compatibility/simpleFunction.kt");
@@ -60,5 +60,33 @@ public class WriteFlagsTestGenerated extends AbstractWriteFlagsTest {
public void testDefaultProperty() throws Exception {
runTest("compiler/testData/codegen/java8/writeFlags/defaults/defaultProperty.kt");
}
@TestMetadata("propertyAnnotation.kt")
public void testPropertyAnnotation() throws Exception {
runTest("compiler/testData/codegen/java8/writeFlags/defaults/propertyAnnotation.kt");
}
@TestMetadata("compiler/testData/codegen/java8/writeFlags/defaults/compatibility")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Compatibility extends AbstractWriteFlagsTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
}
public void testAllFilesPresentInCompatibility() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/java8/writeFlags/defaults/compatibility"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("propertyAccessors.kt")
public void testPropertyAccessors() throws Exception {
runTest("compiler/testData/codegen/java8/writeFlags/defaults/compatibility/propertyAccessors.kt");
}
@TestMetadata("propertyAnnotation.kt")
public void testPropertyAnnotation() throws Exception {
runTest("compiler/testData/codegen/java8/writeFlags/defaults/compatibility/propertyAnnotation.kt");
}
}
}
}