summaryrefslogtreecommitdiff
path: root/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/inspections/jdk2k/ReplaceJavaStaticMethodWithKotlinAnalogInspection.kt
blob: 6a8481066f1927f37477d6cba8d6882c61b6c4d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.

package org.jetbrains.kotlin.idea.inspections.jdk2k

import com.intellij.codeInspection.LocalQuickFix
import com.intellij.codeInspection.ProblemDescriptor
import com.intellij.codeInspection.ProblemsHolder
import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.idea.KotlinBundle
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.caches.resolve.resolveToCall
import org.jetbrains.kotlin.idea.inspections.AbstractKotlinInspection
import org.jetbrains.kotlin.idea.inspections.collections.isCalling
import org.jetbrains.kotlin.idea.util.safeAnalyzeNonSourceRootCode
import org.jetbrains.kotlin.idea.util.textRangeIn
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi.KtCallExpression
import org.jetbrains.kotlin.psi.KtSimpleNameExpression
import org.jetbrains.kotlin.psi.callExpressionVisitor
import org.jetbrains.kotlin.psi.psiUtil.getReceiverExpression
import org.jetbrains.kotlin.resolve.calls.util.getResolvedCall
import org.jetbrains.kotlin.resolve.calls.util.getType
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
import org.jetbrains.kotlin.types.typeUtil.isChar

class ReplaceJavaStaticMethodWithKotlinAnalogInspection : AbstractKotlinInspection() {
    override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean) = callExpressionVisitor(fun(call) {
        val callee = call.calleeExpression ?: return
        val replacements = REPLACEMENTS[callee.text]
            ?.filter { it.filter(call) && it.transformation.isApplicable(call) }
            ?.takeIf { it.isNotEmpty() }
            ?.let { list ->
                val context = call.safeAnalyzeNonSourceRootCode(BodyResolveMode.PARTIAL)
                val callDescriptor = call.getResolvedCall(context) ?: return
                list.filter {
                    callDescriptor.isCalling(FqName(it.javaMethodFqName)) && it.transformation.isApplicableInContext(
                        call,
                        context
                    )
                }
            }
            ?.takeIf { it.isNotEmpty() }
            ?.map(::ReplaceWithKotlinAnalogFunction)
            ?.toTypedArray() ?: return

        holder.registerProblem(
            call,
            callee.textRangeIn(call),
            KotlinBundle.message("should.be.replaced.with.kotlin.function"),
            *replacements
        )
    })

    private class ReplaceWithKotlinAnalogFunction(private val replacement: Replacement) : LocalQuickFix {
        override fun getName() = KotlinBundle.message("replace.with.kotlin.analog.function.text", replacement.kotlinFunctionShortName)

        override fun getFamilyName() = KotlinBundle.message("replace.with.kotlin.analog.function.family.name")

        override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
            val callExpression = descriptor.psiElement as? KtCallExpression ?: return
            replacement.transformation(callExpression, replacement)
        }
    }

    companion object {
        private val JAVA_PRIMITIVES = listOf(
            "Integer" to "Int",
            "Long" to "Long",
            "Byte" to "Byte",
            "Character" to "Char",
            "Short" to "Short",
            "Double" to "Double",
            "Float" to "Float"
        ).flatMap { (javaPrimitive, kotlinPrimitive) ->
            listOf(
                Replacement("java.lang.$javaPrimitive.toString", "kotlin.text.toString", ToExtensionFunctionWithNonNullableReceiver) {
                    it.valueArguments.size == 2
                },
                Replacement(
                    "java.lang.$javaPrimitive.toString",
                    "kotlin.primitives.$kotlinPrimitive.toString",
                    ToExtensionFunctionWithNullableReceiver
                ) { call ->
                    val valueArguments = call.valueArguments
                    when {
                        valueArguments.size != 1 -> false
                        javaPrimitive != "Character" -> true
                        else -> {
                            val singleArgument = valueArguments.single().getArgumentExpression()
                            if (singleArgument != null) {
                                val context = call.analyze(BodyResolveMode.PARTIAL)
                                singleArgument.getType(context)?.isChar() == true
                            } else {
                                false
                            }
                        }
                    }
                },
                Replacement(
                    "java.lang.$javaPrimitive.compare",
                    "kotlin.primitives.$kotlinPrimitive.compareTo",
                    ToExtensionFunctionWithNonNullableReceiver
                )
            )
        }

        private val JAVA_IO = listOf(
            Replacement("java.io.PrintStream.print", "kotlin.io.print", filter = ::isJavaSystemOut),
            Replacement("java.io.PrintStream.println", "kotlin.io.println", filter = ::isJavaSystemOut)
        )

        // TODO: implement [java.lang.System.arraycopy]
        private val JAVA_SYSTEM = listOf(
            Replacement("java.lang.System.exit", "kotlin.system.exitProcess")
        )

        private val JAVA_MATH = listOf(
            Replacement("java.lang.Math.abs", "kotlin.math.abs"),
            Replacement("java.lang.Math.acos", "kotlin.math.acos"),
            Replacement("java.lang.Math.asin", "kotlin.math.asin"),
            Replacement("java.lang.Math.atan", "kotlin.math.atan"),
            Replacement("java.lang.Math.atan2", "kotlin.math.atan2"),
            Replacement("java.lang.Math.ceil", "kotlin.math.ceil"),
            Replacement("java.lang.Math.cos", "kotlin.math.cos"),
            Replacement("java.lang.Math.cosh", "kotlin.math.cosh"),
            Replacement("java.lang.Math.exp", "kotlin.math.exp"),
            Replacement("java.lang.Math.expm1", "kotlin.math.expm1"),
            Replacement("java.lang.Math.floor", "kotlin.math.floor"),
            Replacement("java.lang.Math.hypot", "kotlin.math.hypot"),
            Replacement("java.lang.Math.IEEEremainder", "kotlin.math.IEEErem", ToExtensionFunctionWithNonNullableReceiver),
            Replacement("java.lang.Math.log", "kotlin.math.ln"),
            Replacement("java.lang.Math.log1p", "kotlin.math.ln1p"),
            Replacement("java.lang.Math.log10", "kotlin.math.log10"),
            Replacement("java.lang.Math.max", "kotlin.math.max"),
            Replacement("java.lang.Math.max", "kotlin.ranges.coerceAtLeast", ToExtensionFunctionWithNonNullableReceiver),
            Replacement("java.lang.Math.min", "kotlin.math.min"),
            Replacement("java.lang.Math.min", "kotlin.ranges.coerceAtMost", ToExtensionFunctionWithNonNullableReceiver),
            Replacement("java.lang.Math.nextDown", "kotlin.math.nextDown", ToExtensionFunctionWithNonNullableReceiver),
            Replacement("java.lang.Math.nextAfter", "kotlin.math.nextTowards", ToExtensionFunctionWithNonNullableReceiver),
            Replacement("java.lang.Math.nextUp", "kotlin.math.nextUp", ToExtensionFunctionWithNonNullableReceiver),
            Replacement("java.lang.Math.pow", "kotlin.math.pow", ToExtensionFunctionWithNonNullableReceiver),
            Replacement("java.lang.Math.rint", "kotlin.math.round"),
            Replacement("java.lang.Math.round", "kotlin.math.roundToLong", ToExtensionFunctionWithNonNullableReceiver),
            Replacement("java.lang.Math.round", "kotlin.math.roundToInt", ToExtensionFunctionWithNonNullableReceiver),
            Replacement("java.lang.Math.signum", "kotlin.math.sign"),
            Replacement("java.lang.Math.sin", "kotlin.math.sin"),
            Replacement("java.lang.Math.sinh", "kotlin.math.sinh"),
            Replacement("java.lang.Math.sqrt", "kotlin.math.sqrt"),
            Replacement("java.lang.Math.tan", "kotlin.math.tan"),
            Replacement("java.lang.Math.tanh", "kotlin.math.tanh"),
            Replacement("java.lang.Math.copySign", "kotlin.math.withSign", ToExtensionFunctionWithNonNullableReceiver)
        )

        // TODO: implement [java.util.Arrays.binarySearch, java.util.Arrays.fill, java.util.Arrays.sort]
        private val JAVA_COLLECTIONS = listOf(
            Replacement("java.util.Arrays.copyOf", "kotlin.collections.copyOf", ToExtensionFunctionWithNonNullableReceiver) {
                it.valueArguments.size == 2
            },
            Replacement("java.util.Arrays.copyOfRange", "kotlin.collections.copyOfRange", ToExtensionFunctionWithNonNullableReceiver),
            Replacement("java.util.Arrays.equals", "kotlin.collections.contentEquals", ToExtensionFunctionWithNonNullableArguments) {
                it.valueArguments.size == 2
            },
            Replacement("java.util.Arrays.deepEquals", "kotlin.collections.contentDeepEquals", ToExtensionFunctionWithNonNullableArguments),
            Replacement(
                "java.util.Arrays.deepHashCode",
                "kotlin.collections.contentDeepHashCode",
                ToExtensionFunctionWithNonNullableReceiver
            ),
            Replacement("java.util.Arrays.hashCode", "kotlin.collections.contentHashCode", ToExtensionFunctionWithNonNullableReceiver),
            Replacement(
                "java.util.Arrays.deepToString",
                "kotlin.collections.contentDeepToString",
                ToExtensionFunctionWithNonNullableReceiver
            ),
            Replacement("java.util.Arrays.toString", "kotlin.collections.contentToString", ToExtensionFunctionWithNonNullableReceiver),
            Replacement("java.util.Arrays.asList", "kotlin.collections.listOf"),
            Replacement("java.util.Arrays.asList", "kotlin.collections.mutableListOf"),
            Replacement("java.util.Set.of", "kotlin.collections.setOf"),
            Replacement("java.util.Set.of", "kotlin.collections.mutableSetOf"),
            Replacement("java.util.List.of", "kotlin.collections.listOf"),
            Replacement("java.util.List.of", "kotlin.collections.mutableListOf")
        )

        private val REPLACEMENTS = (JAVA_MATH + JAVA_SYSTEM + JAVA_IO + JAVA_PRIMITIVES + JAVA_COLLECTIONS)
            .groupBy { it.javaMethodShortName }
    }
}

data class Replacement(
    val javaMethodFqName: String,
    val kotlinFunctionFqName: String,
    val transformation: Transformation = WithoutAdditionalTransformation,
    val filter: (KtCallExpression) -> Boolean = { true }
) {
    private fun String.shortName() = takeLastWhile { it != '.' }

    val javaMethodShortName = javaMethodFqName.shortName()

    val kotlinFunctionShortName = kotlinFunctionFqName.shortName()
}

private fun isJavaSystemOut(callExpression: KtCallExpression): Boolean =
    (callExpression.calleeExpression as? KtSimpleNameExpression)
        ?.getReceiverExpression()
        ?.resolveToCall()
        ?.isCalling(FqName("java.lang.System.out")) ?: false