Skip to content

Commit

Permalink
Inspection for migrating Kotlin stdlib API usages in Java code
Browse files Browse the repository at this point in the history
  • Loading branch information
yole committed Dec 15, 2015
1 parent 3dfb9d2 commit c83b6ed
Show file tree
Hide file tree
Showing 12 changed files with 233 additions and 0 deletions.
5 changes: 5 additions & 0 deletions idea/resources/inspectionDescriptions/OldStdlibApi.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<body>
This inspection located pre-1.0 usages of the Kotlin standard library APIs from Java code and replaces them with up-to-date APIs.
</body>
</html>
7 changes: 7 additions & 0 deletions idea/src/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1301,6 +1301,13 @@
enabledByDefault="true"
level="WARNING"/>

<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.OldStdlibApiInspection"
displayName="Java usages of old Kotlin standard library APIs"
groupName="Kotlin"
enabledByDefault="true"
cleanupTool="true"
level="WARNING"/>

<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>

<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* 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.idea.inspections

import com.intellij.codeInspection.*
import com.intellij.openapi.module.ModuleUtil
import com.intellij.openapi.project.Project
import com.intellij.psi.*
import com.intellij.psi.codeStyle.JavaCodeStyleManager
import com.intellij.psi.search.GlobalSearchScope
import org.jetbrains.kotlin.idea.KotlinLanguage
import org.jetbrains.kotlin.idea.search.allScope
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType

class OldStdlibApiInspection : AbstractKotlinInspection(), CleanupLocalInspectionTool {
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean, session: LocalInspectionToolSession): PsiElementVisitor {
return object : PsiElementVisitor() {
override fun visitElement(element: PsiElement) {
if (element.language == KotlinLanguage.INSTANCE) return
val importStatement = element.getParentOfType<PsiImportStatement>(false)
if (importStatement != null) return

for (reference in element.references) {
checkReference(reference)?.let {
holder.registerProblem(element, "Usage of the Kotlin standard library through a deprecated qualified name",
ProblemHighlightType.LIKE_DEPRECATED, it)

}
}
}
}
}

private fun checkReference(reference: PsiReference): LocalQuickFix? {
val resolveResult = reference.resolve()
if (resolveResult is PsiClass) {
val fqName = resolveResult.qualifiedName
val newFqName = StdlibMigrationMap.classMap[fqName]
if (newFqName != null) {
return OldStdlibApiFix { project, scope ->
JavaPsiFacade.getInstance(project).findClass(newFqName, scope)
}
}
}
else if (resolveResult is PsiMethod) {
val containingClass = resolveResult.containingClass ?: return null
val fqName = MethodFQName(containingClass.qualifiedName ?: return null, resolveResult.name)
val newFqName = StdlibMigrationMap.methodMap[fqName]
if (newFqName != null) {
return OldStdlibApiFix { project, scope ->
JavaPsiFacade.getInstance(project).findClass(newFqName.className, scope)
?.findMethodsByName(newFqName.methodName, false)
?.singleOrNull()
}
}
}
return null
}
}

public class OldStdlibApiFix(val newElementCallback: (Project, GlobalSearchScope) -> PsiElement?) : LocalQuickFix {
override fun getName(): String = "Replace with new qualified name"
override fun getFamilyName(): String = name

override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
val element = descriptor.psiElement
val reference = element.reference ?: return
val module = ModuleUtil.findModuleForPsiElement(element)
val scope = module?.moduleWithLibrariesScope ?: project.allScope()

val newElement = newElementCallback(project, scope) ?: return
val newReference = reference.bindToElement(newElement)

val javaFile = newReference.containingFile as? PsiJavaFile
if (javaFile != null) {
JavaCodeStyleManager.getInstance(project).removeRedundantImports(javaFile)
}

JavaCodeStyleManager.getInstance(project).shortenClassReferences(newReference)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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.idea.inspections

data class MethodFQName(val className: String, val methodName: String)

object StdlibMigrationMap {
val methodMap = hashMapOf(
MethodFQName("kotlin.jvm.ClassMapping", "getKotlin") to MethodFQName("kotlin.jvm.JvmClassMappingKt", "getKotlinClass"),
MethodFQName("kotlin.jvm.ClassMapping", "getJava") to MethodFQName("kotlin.jvm.JvmClassMappingKt", "getJavaClass")
)

val classMap = hashMapOf(
"kotlin.ArraysKt" to "kotlin.collections.ArraysKt",
"kotlin.CharsKt" to "kotlin.text.CharsKt",
"kotlin.CollectionsKt" to "kotlin.collections.CollectionsKt",
"kotlin.MapsKt" to "kotlin.collections.MapsKt",
"kotlin.RangesKt" to "kotlin.ranges.RangesKt",
"kotlin.SequencesKt" to "kotlin.sequences.SequencesKt",
"kotlin.SetsKt" to "kotlin.collections.SetsKt",
"kotlin.StringsKt" to "kotlin.text.StringsKt",
"kotlin.support.AbstractIterator" to "kotlin.collections.AbstractIterator"
)
}
1 change: 1 addition & 0 deletions idea/testData/quickfix/migration/stdlib/.inspection
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.inspections.OldStdlibApiInspection
10 changes: 10 additions & 0 deletions idea/testData/quickfix/migration/stdlib/basic.after.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// "Replace with new qualified name" "true"
// WITH_RUNTIME

import kotlin.collections.ArraysKt;

class C {
public void foo(byte[] bytes) {
ArraysKt.component1(bytes);
}
}
10 changes: 10 additions & 0 deletions idea/testData/quickfix/migration/stdlib/basic.before.Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// "Replace with new qualified name" "true"
// WITH_RUNTIME

import kotlin.ArraysKt;

class C {
public void foo(byte[] bytes) {
<caret>ArraysKt.component1(bytes);
}
}
10 changes: 10 additions & 0 deletions idea/testData/quickfix/migration/stdlib/importStatic.after.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// "Replace with new qualified name" "true"
// WITH_RUNTIME

import static kotlin.collections.ArraysKt<caret>.component1;

class C {
public void foo(byte[] bytes) {
component1(bytes);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// "Replace with new qualified name" "true"
// WITH_RUNTIME

import static kotlin.ArraysKt<caret>.component1;

class C {
public void foo(byte[] bytes) {
component1(bytes);
}
}
10 changes: 10 additions & 0 deletions idea/testData/quickfix/migration/stdlib/method.after.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// "Replace with new qualified name" "true"
// WITH_RUNTIME

import kotlin.jvm.JvmClassMappingKt;

class C {
public void foo(Class cls) {
<caret>JvmClassMappingKt.getKotlinClass(cls);
}
}
10 changes: 10 additions & 0 deletions idea/testData/quickfix/migration/stdlib/method.before.Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// "Replace with new qualified name" "true"
// WITH_RUNTIME

import kotlin.jvm.ClassMapping;

class C {
public void foo(Class cls) {
<caret>ClassMapping.getKotlin(cls);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1387,6 +1387,33 @@ public void testManyFilesMuitliple() throws Exception {
}
}

@TestMetadata("idea/testData/quickfix/migration/stdlib")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Stdlib extends AbstractQuickFixMultiFileTest {
public void testAllFilesPresentInStdlib() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/migration/stdlib"), Pattern.compile("^(\\w+)\\.((before\\.Main\\.\\w+)|(test))$"), true);
}

@TestMetadata("basic.before.Main.java")
public void testBasic() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/migration/stdlib/basic.before.Main.java");
doTestWithExtraFile(fileName);
}

@TestMetadata("importStatic.before.Main.java")
public void testImportStatic() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/migration/stdlib/importStatic.before.Main.java");
doTestWithExtraFile(fileName);
}

@TestMetadata("method.before.Main.java")
public void testMethod() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/migration/stdlib/method.before.Main.java");
doTestWithExtraFile(fileName);
}
}

}

@TestMetadata("idea/testData/quickfix/modifiers")
Expand Down

0 comments on commit c83b6ed

Please sign in to comment.