Don't generate setters for trivial private property setters
This change will prevent the compiler for generating Java bytecode for private property setters that are trivial. Since Kotlin uses direct field access for private properties, it will result in the private setter never been used and since it cannot be accessed by any other class without reflection, the setter cannot be covered by code coverage tools. See https://youtrack.jetbrains.com/issue/KT-20344 for the related YouTrack issue.
This commit is contained in:
committed by
Alexander Udalov
parent
b1e82c78da
commit
05f6ed40f1
@@ -124,9 +124,10 @@ public class PropertyGenTest extends CodegenTestCase {
|
||||
getFoo.setAccessible(true);
|
||||
assertTrue((getFoo.getModifiers() & Modifier.PROTECTED) != 0);
|
||||
assertEquals(349, getFoo.invoke(instance));
|
||||
Method setFoo = findDeclaredMethodByName(aClass, "setFoo");
|
||||
setFoo.setAccessible(true);
|
||||
assertTrue((setFoo.getModifiers() & Modifier.PRIVATE) != 0);
|
||||
// See KT-20344
|
||||
// Method setFoo = findDeclaredMethodByName(aClass, "setFoo");
|
||||
// setFoo.setAccessible(true);
|
||||
// assertTrue((setFoo.getModifiers() & Modifier.PRIVATE) != 0);
|
||||
Method setter = findDeclaredMethodByName(aClass, "setter");
|
||||
setter.invoke(instance);
|
||||
assertEquals(610, getFoo.invoke(instance));
|
||||
@@ -247,4 +248,12 @@ public class PropertyGenTest extends CodegenTestCase {
|
||||
assertNull("Property should not have a getter", findDeclaredMethodByNameOrNull(c, "getVarNoAccessors"));
|
||||
assertNull("Property should not have a setter", findDeclaredMethodByNameOrNull(c, "setVarNoAccessors"));
|
||||
}
|
||||
|
||||
// Properties with trivial private setters should not generate their setters since
|
||||
// the class will use direct field access instead for these properties.
|
||||
public void testKt20344() throws Exception {
|
||||
loadText("class Foo { public lateinit var x: String private set }");
|
||||
Class<?> aClass = generateClass("Foo");
|
||||
assertNull("Property should not have generated setter", findDeclaredMethodByNameOrNull(aClass, "setX"));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user