Restore and deprecate Function{n}/ExtensionFunction{n} classes for easier migration

Users' Java code will not break in common cases (when passing functions to
Kotlin), and deprecation warnings will be reported.

Provide an inspection with a quick fix which allows to replace deprecated
function class usages to the new classes. Include this fix to the "code
cleanup" action
This commit is contained in:
Alexander Udalov
2015-05-25 21:56:48 +03:00
parent 851007cc6c
commit d14e5b8a72
64 changed files with 1596 additions and 14 deletions
@@ -0,0 +1,43 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.generators.builtins
import org.jetbrains.kotlin.generators.builtins.generateBuiltIns.BuiltInsSourceGenerator
import java.io.PrintWriter
class GenerateDeprecatedJavaFunction(
out: PrintWriter,
private val arity: Int,
private val extension: Boolean
) : BuiltInsSourceGenerator(out) {
override val language = BuiltInsSourceGenerator.Language.JAVA
override fun generateBody() {
val params = (1..arity).map { "P$it" }
val generics = "<" + ((if (extension) listOf("E") else listOf()) + params + listOf("R")).join() + ">"
val name = if (extension) "ExtensionFunction" else "Function"
val superArity = if (extension) arity + 1 else arity
out.println("/**")
out.println(" * @deprecated Use the new function classes from the package kotlin.jvm.functions.")
out.println(" */")
out.println("@Deprecated")
out.println("public interface $name$arity$generics extends kotlin.jvm.functions.Function$superArity$generics {")
out.println("}")
}
}
@@ -16,6 +16,7 @@
package org.jetbrains.kotlin.generators.builtins.generateBuiltIns
import org.jetbrains.kotlin.generators.builtins.GenerateDeprecatedJavaFunction
import org.jetbrains.kotlin.generators.builtins.arrayIterators.GenerateArrayIterators
import org.jetbrains.kotlin.generators.builtins.arrays.GenerateArrays
import org.jetbrains.kotlin.generators.builtins.functionImpl.GenerateFunctionImpl
@@ -34,6 +35,7 @@ fun assertExists(file: File): Unit =
val BUILT_INS_NATIVE_DIR = File("core/builtins/native/")
val BUILT_INS_SRC_DIR = File("core/builtins/src/")
val RUNTIME_JVM_DIR = File("core/runtime.jvm/src/")
val FUNCTIONS_MIGRATION_DIR = File("core/functions.migration/src/")
abstract class BuiltInsSourceGenerator(val out: PrintWriter) {
protected abstract fun generateBody(): Unit
@@ -43,7 +45,7 @@ abstract class BuiltInsSourceGenerator(val out: PrintWriter) {
protected open val language: Language = Language.KOTLIN
enum class Language {
KOTLIN
KOTLIN,
JAVA
}
@@ -75,6 +77,13 @@ fun generateBuiltIns(generate: (File, (PrintWriter) -> BuiltInsSourceGenerator)
generate(File(BUILT_INS_SRC_DIR, "kotlin/ProgressionIterators.kt")) { GenerateProgressionIterators(it) }
generate(File(BUILT_INS_SRC_DIR, "kotlin/Progressions.kt")) { GenerateProgressions(it) }
generate(File(BUILT_INS_SRC_DIR, "kotlin/Ranges.kt")) { GenerateRanges(it) }
for (i in 0..22) {
generate(File(FUNCTIONS_MIGRATION_DIR, "kotlin/Function$i.java")) { GenerateDeprecatedJavaFunction(it, i, false) }
}
for (i in 0..21) {
generate(File(FUNCTIONS_MIGRATION_DIR, "kotlin/ExtensionFunction$i.java")) { GenerateDeprecatedJavaFunction(it, i, true) }
}
}
fun main(args: Array<String>) {