Skip to content

Commit 407e4cd

Browse files
committed
Don't create a default constructor for annotations, or classes that explicitly declare a no-arg constructor.
1 parent 1c0494e commit 407e4cd

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

java/kotlin-extractor/src/main/kotlin/KotlinFileExtractor.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1095,7 +1095,14 @@ open class KotlinFileExtractor(
10951095
}
10961096

10971097
if (!f.hasAnnotation(jvmOverloadsFqName)) {
1098-
if (f is IrConstructor && f.valueParameters.isNotEmpty() && f.valueParameters.all { it.defaultValue != null }) {
1098+
if (f is IrConstructor &&
1099+
f.valueParameters.isNotEmpty() &&
1100+
f.valueParameters.all { it.defaultValue != null } &&
1101+
f.parentClassOrNull?.let {
1102+
// Don't create a default constructor for an annotation class, or a class that explicitly declares a no-arg constructor.
1103+
!it.isAnnotationClass &&
1104+
it.declarations.none { d -> d is IrConstructor && d.valueParameters.isEmpty() }
1105+
} == true) {
10991106
// Per https://kotlinlang.org/docs/classes.html#creating-instances-of-classes, a single default overload gets created specifically
11001107
// when we have all default parameters, regardless of `@JvmOverloads`.
11011108
extractGeneratedOverload(f.valueParameters.map { _ -> null })
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
@AllDefaultsAnnotation
12
public class User {
23

3-
public static void test() { new AllDefaultsConstructor(); }
4+
public static void test() { new AllDefaultsConstructor(); new AllDefaultsExplicitNoargConstructor(); }
45

56
}

java/ql/integration-tests/all-platforms/kotlin/jvmoverloads-external-class/test.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,11 @@ public class Test {
55
}
66

77
public class AllDefaultsConstructor(val x: Int = 1, val y: Int = 2) { }
8+
9+
public annotation class AllDefaultsAnnotation(val x: Int = 1, val y: Int = 2) { }
10+
11+
public class AllDefaultsExplicitNoargConstructor(val x: Int = 1, val y: Int = 2) {
12+
13+
constructor() : this(3, 4) { }
14+
15+
}

0 commit comments

Comments
 (0)