diff --git a/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/CodeUtils.kt b/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/CodeUtils.kt index d1c2602fb77..f7e728e727b 100644 --- a/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/CodeUtils.kt +++ b/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/CodeUtils.kt @@ -68,7 +68,7 @@ private val charactersAllowedInKotlinStringLiterals: Set = mutableSetOf) = block(header, lines.asSequence()) diff --git a/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/ObjCStubs.kt b/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/ObjCStubs.kt index 9a9948e4dec..bb7bbd2b5bf 100644 --- a/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/ObjCStubs.kt +++ b/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/ObjCStubs.kt @@ -86,6 +86,14 @@ class ObjCMethodStub(private val stubGenerator: StubGenerator, when (container) { is ObjCClass -> { + result.add(0, + deprecatedInit( + container.kotlinClassName(method.isClass), + kotlinParameters.map { it.name }, + factory = false + ) + ) + // TODO: consider generating non-designated initializers as factories. val designated = isDesignatedInitializer || stubGenerator.configuration.disableDesignatedInitializerChecks @@ -95,13 +103,20 @@ class ObjCMethodStub(private val stubGenerator: StubGenerator, result.add("constructor($parameters) {}") } is ObjCCategory -> { - assert(!method.isClass) - val tBound = stubGenerator.declarationMapper + val className = stubGenerator.declarationMapper .getKotlinClassFor(container.clazz, isMeta = false).type .render(kotlinScope) + result.add(0, + deprecatedInit( + className, + kotlinParameters.map { it.name }, + factory = true + ) + ) + // TODO: add support for type parameters to [KotlinType] etc. val receiver = kotlinScope.reference(KotlinTypes.objCClassOf) + "" @@ -115,7 +130,7 @@ class ObjCMethodStub(private val stubGenerator: StubGenerator, result.add("") result.add("@ObjCFactory".applyToStrings(bridgeName)) - result.add("external fun $receiver.create($parameters): $returnType") + result.add("external fun $receiver.create($parameters): $returnType") } is ObjCProtocol -> {} // Nothing to do. } @@ -274,6 +289,19 @@ class ObjCMethodStub(private val stubGenerator: StubGenerator, } } +private fun deprecatedInit(className: String, initParameterNames: List, factory: Boolean): String { + val replacement = if (factory) "$className.create" else className + val replacementKind = if (factory) "factory method" else "constructor" + val replaceWith = "$replacement(${initParameterNames.joinToString()})" + + return deprecated("Use $replacementKind instead", replaceWith) +} + +private fun deprecated(message: String, replaceWith: String): String = + "@Deprecated(${message.quoteAsKotlinLiteral()}, " + + "ReplaceWith(${replaceWith.quoteAsKotlinLiteral()}), " + + "DeprecationLevel.ERROR)" + private val ObjCContainer.classOrProtocol: ObjCClassOrProtocol get() = when (this) { is ObjCClassOrProtocol -> this diff --git a/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/jvm/StubGenerator.kt b/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/jvm/StubGenerator.kt index 4aef4c745e1..c53143809cf 100644 --- a/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/jvm/StubGenerator.kt +++ b/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/jvm/StubGenerator.kt @@ -925,6 +925,7 @@ class StubGenerator( add("EXTENSION_SHADOWED_BY_MEMBER") // For Objective-C categories represented as extensions. add("REDUNDANT_NULLABLE") // This warning appears due to Obj-C typedef nullability incomplete support. add("DEPRECATION") // For uncheckedCast. + add("DEPRECATION_ERROR") // For initializers. } }