Skip to content

Commit 1320f17

Browse files
authored
Treat Kotlin getters/setters as KnownImplicitlyDeclaredMethods #1338 #1339 (#1341)
1 parent d31e90f commit 1320f17

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

utbot-framework-api/src/main/kotlin/org/utbot/framework/plugin/api/util/IdUtil.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,20 @@ val ClassId.isDoubleType: Boolean
189189
val ClassId.isClassType: Boolean
190190
get() = this == classClassId
191191

192+
/**
193+
* Returns [Metadata] annotation if this is a Kotlin class, null otherwise
194+
*/
195+
val ClassId.kotlinMetadata: Metadata?
196+
get() = jClass.annotations.filterIsInstance<Metadata>().singleOrNull()
197+
198+
val ClassId.isFromKotlin: Boolean
199+
get() = kotlinMetadata != null
200+
192201
/**
193202
* Checks if the class is a Kotlin class with kind File (see [Metadata.kind] for more details)
194203
*/
195204
val ClassId.isKotlinFile: Boolean
196-
get() = jClass.annotations.filterIsInstance<Metadata>().singleOrNull()?.let {
205+
get() = kotlinMetadata?.let {
197206
KotlinClassHeader.Kind.getById(it.kind) == KotlinClassHeader.Kind.FILE_FACADE
198207
} ?: false
199208

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/util/ClassIdUtil.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package org.utbot.framework.codegen.model.util
22

33
import org.utbot.framework.plugin.api.ClassId
4+
import org.utbot.framework.plugin.api.ExecutableId
45
import org.utbot.framework.plugin.api.FieldId
5-
import org.utbot.framework.plugin.api.MethodId
66
import org.utbot.framework.plugin.api.util.isPackagePrivate
77
import org.utbot.framework.plugin.api.util.isProtected
88
import org.utbot.framework.plugin.api.util.isPublic
@@ -40,11 +40,11 @@ infix fun ClassId.isAccessibleFrom(packageName: String): Boolean {
4040
/**
4141
* Returns field of [this], such that [methodId] is a getter for it (or null if methodId doesn't represent a getter)
4242
*/
43-
internal fun ClassId.fieldThatIsGotWith(methodId: MethodId): FieldId? =
43+
internal fun ClassId.fieldThatIsGotWith(methodId: ExecutableId): FieldId? =
4444
allDeclaredFieldIds.singleOrNull { !it.isStatic && it.getter.describesSameMethodAs(methodId) }
4545

4646
/**
4747
* Returns field of [this], such that [methodId] is a setter for it (or null if methodId doesn't represent a setter)
4848
*/
49-
internal fun ClassId.fieldThatIsSetWith(methodId: MethodId): FieldId? =
49+
internal fun ClassId.fieldThatIsSetWith(methodId: ExecutableId): FieldId? =
5050
allDeclaredFieldIds.singleOrNull { !it.isStatic && it.setter.describesSameMethodAs(methodId) }

utbot-framework/src/main/kotlin/org/utbot/framework/util/ImplicitlyDeclaredMethods.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package org.utbot.framework.util
22

3+
import org.utbot.framework.codegen.model.util.fieldThatIsGotWith
4+
import org.utbot.framework.codegen.model.util.fieldThatIsSetWith
35
import org.utbot.framework.plugin.api.ExecutableId
46
import org.utbot.framework.plugin.api.util.isData
57
import org.utbot.framework.plugin.api.util.isEnum
8+
import org.utbot.framework.plugin.api.util.isFromKotlin
9+
import org.utbot.framework.plugin.api.util.isKotlinFile
610

711
/**
812
* Returns whether this method could be implicitly generated by compiler, or not.
@@ -14,11 +18,19 @@ import org.utbot.framework.plugin.api.util.isEnum
1418
val ExecutableId.isKnownImplicitlyDeclaredMethod: Boolean
1519
get() =
1620
when {
21+
// this check is needed because reflection is not fully supported for file facades -- see KT-16479,
22+
// by now we assume that such classes can't contain autogenerated methods
23+
classId.isKotlinFile -> false
24+
isKotlinGetterOrSetter -> true
1725
classId.isEnum -> name in KnownImplicitlyDeclaredMethods.enumImplicitlyDeclaredMethodNames
1826
classId.isData -> KnownImplicitlyDeclaredMethods.dataClassImplicitlyDeclaredMethodNameRegexps.any { it.matches(name) }
1927
else -> false
2028
}
2129

30+
internal val ExecutableId.isKotlinGetterOrSetter: Boolean
31+
get() = classId.isFromKotlin &&
32+
(classId.fieldThatIsGotWith(this) != null || classId.fieldThatIsSetWith(this) != null)
33+
2234
/**
2335
* Contains names of methods that are always autogenerated by compiler and thus it is unlikely that
2436
* one would want to generate tests for them.

0 commit comments

Comments
 (0)