Add Java toString to ReplaceJavaStaticMethodWithTopLevelFunctionInspection

This commit is contained in:
Dmitry Gridin
2019-04-11 12:26:21 +07:00
parent 6afc0367c0
commit 7f3c4a21d5
24 changed files with 162 additions and 32 deletions
@@ -39,10 +39,12 @@ class ReplaceJavaStaticMethodWithTopLevelFunctionInspection : AbstractKotlinInsp
val callee = call.calleeExpression ?: return
val dotQualified = call.getStrictParentOfType<KtDotQualifiedExpression>() ?: return
val calleeText = callee.text
val replacements = replacements[calleeText] ?: return
val replacement = replacements.first()
val replacements = REPLACEMENTS[calleeText]?.let { list ->
val callDescriptor = call.getResolvedCall(call.analyze(BodyResolveMode.PARTIAL)) ?: return
list.filter { callDescriptor.isCalling(FqName(it.javaMethodFqName)) }
} ?: return
if (call.getResolvedCall(call.analyze(BodyResolveMode.PARTIAL))?.isCalling(FqName(replacement.javaMethodFqName)) != true) return
val replacement = replacements.firstOrNull() ?: return
if (replacement.toExtensionFunction && call.valueArguments.isEmpty()) return
holder.registerProblem(
dotQualified,
@@ -112,15 +114,23 @@ class ReplaceJavaStaticMethodWithTopLevelFunctionInspection : AbstractKotlinInsp
val javaMethodShortName = javaMethodFqName.shortName()
val kotlinFunctionShortName = kotlinFunctionFqName.shortName()
override fun toString(): String = kotlinFunctionFqName
}
companion object {
private val replacements = listOf(
private val JAVA_PRIMITIVES = listOf("Integer", "Long", "Byte", "Character", "Short", "Double", "Float").map {
Replacement("java.lang.$it.toString", "kotlin.text.toString", toExtensionFunction = true)
}
private val JAVA_IO = listOf(
Replacement("java.io.PrintStream.print", "kotlin.io.print"),
Replacement("java.io.PrintStream.println", "kotlin.io.println"),
Replacement("java.lang.System.exit", "kotlin.system.exitProcess"),
Replacement("java.io.PrintStream.println", "kotlin.io.println")
)
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"),
@@ -152,9 +162,11 @@ class ReplaceJavaStaticMethodWithTopLevelFunctionInspection : AbstractKotlinInsp
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", toExtensionFunction = true),
Replacement("java.util.Arrays.copyOf", "kotlin.collections.copyOf", toExtensionFunction = true),
Replacement("java.lang.Integer.toString", "kotlin.text.toString", toExtensionFunction = true)
).groupBy { it.javaMethodShortName }
Replacement("java.lang.Math.copySign", "kotlin.math.withSign", toExtensionFunction = true)
)
private val JAVA_ARRAYS = listOf(Replacement("java.util.Arrays.copyOf", "kotlin.collections.copyOf", toExtensionFunction = true))
private val REPLACEMENTS = (JAVA_MATH + JAVA_SYSTEM + JAVA_IO + JAVA_PRIMITIVES + JAVA_ARRAYS).groupBy { it.javaMethodShortName }
}
}
@@ -0,0 +1,5 @@
// WITH_RUNTIME
fun foo() {
val t = java.lang.Byte.<caret>toString(5) + 6
}
@@ -0,0 +1,5 @@
// WITH_RUNTIME
fun foo() {
val t = 5.toString() + 6
}
@@ -0,0 +1,5 @@
// WITH_RUNTIME
fun foo() {
val t = java.lang.Character.<caret>toString('3') + 6
}
@@ -0,0 +1,5 @@
// WITH_RUNTIME
fun foo() {
val t = '3'.toString() + 6
}
@@ -0,0 +1,5 @@
// WITH_RUNTIME
fun foo() {
val t = java.lang.Double.<caret>toString(5.0)
}
@@ -0,0 +1,5 @@
// WITH_RUNTIME
fun foo() {
val t = 5.0.toString()
}
@@ -0,0 +1,5 @@
// WITH_RUNTIME
fun foo() {
val t = java.lang.Float.<caret>toString(5.0)
}
@@ -0,0 +1,5 @@
// WITH_RUNTIME
fun foo() {
val t = 5.0.toString()
}
@@ -0,0 +1,5 @@
// WITH_RUNTIME
fun foo() {
val t = java.lang.Long.<caret>toString(5) + 6
}
@@ -0,0 +1,5 @@
// WITH_RUNTIME
fun foo() {
val t = 5.toString() + 6
}
@@ -0,0 +1,5 @@
// WITH_RUNTIME
fun foo() {
val t = java.lang.Long.<caret>toString(5, 42) + 6
}
@@ -0,0 +1,5 @@
// WITH_RUNTIME
fun foo() {
val t = 5.toString(42) + 6
}
@@ -0,0 +1,5 @@
// WITH_RUNTIME
fun foo() {
val t = java.lang.Short.<caret>toString(42) + 6
}
@@ -0,0 +1,5 @@
// WITH_RUNTIME
fun foo() {
val t = 42.toString() + 6
}
@@ -7178,6 +7178,39 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
}
}
@TestMetadata("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/io")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Io extends AbstractLocalInspectionTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
}
public void testAllFilesPresentInIo() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/io"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
}
@TestMetadata("print.kt")
public void testPrint() throws Exception {
runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/io/print.kt");
}
@TestMetadata("println.kt")
public void testPrintln() throws Exception {
runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/io/println.kt");
}
@TestMetadata("println_2.kt")
public void testPrintln_2() throws Exception {
runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/io/println_2.kt");
}
@TestMetadata("println_3.kt")
public void testPrintln_3() throws Exception {
runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/io/println_3.kt");
}
}
@TestMetadata("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/math")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -7362,26 +7395,6 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
public void testExit() throws Exception {
runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/system/exit.kt");
}
@TestMetadata("print.kt")
public void testPrint() throws Exception {
runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/system/print.kt");
}
@TestMetadata("println.kt")
public void testPrintln() throws Exception {
runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/system/println.kt");
}
@TestMetadata("println_2.kt")
public void testPrintln_2() throws Exception {
runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/system/println_2.kt");
}
@TestMetadata("println_3.kt")
public void testPrintln_3() throws Exception {
runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/system/println_3.kt");
}
}
@TestMetadata("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/toString")
@@ -7396,6 +7409,26 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/toString"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
}
@TestMetadata("byteToString.kt")
public void testByteToString() throws Exception {
runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/toString/byteToString.kt");
}
@TestMetadata("charToString.kt")
public void testCharToString() throws Exception {
runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/toString/charToString.kt");
}
@TestMetadata("doubleToString.kt")
public void testDoubleToString() throws Exception {
runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/toString/doubleToString.kt");
}
@TestMetadata("floatToString.kt")
public void testFloatToString() throws Exception {
runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/toString/floatToString.kt");
}
@TestMetadata("incorrectDescriptor.kt")
public void testIncorrectDescriptor() throws Exception {
runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/toString/incorrectDescriptor.kt");
@@ -7411,6 +7444,16 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/toString/intToString2.kt");
}
@TestMetadata("longToString.kt")
public void testLongToString() throws Exception {
runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/toString/longToString.kt");
}
@TestMetadata("longToString2.kt")
public void testLongToString2() throws Exception {
runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/toString/longToString2.kt");
}
@TestMetadata("replaceIntToString.kt")
public void testReplaceIntToString() throws Exception {
runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/toString/replaceIntToString.kt");
@@ -7430,6 +7473,11 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
public void testReplaceToStringWithRadix() throws Exception {
runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/toString/replaceToStringWithRadix.kt");
}
@TestMetadata("shortToString.kt")
public void testShortToString() throws Exception {
runTest("idea/testData/inspectionsLocal/replaceJavaStaticMethodWithTopLevelFunction/toString/shortToString.kt");
}
}
}