forked from JetBrains/kotlin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Inspection for migrating Kotlin stdlib API usages in Java code
- Loading branch information
Showing
12 changed files
with
233 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
idea/src/org/jetbrains/kotlin/idea/inspections/OldStdlibApiInspection.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
idea/src/org/jetbrains/kotlin/idea/inspections/StdlibMigrationMap.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
org.jetbrains.kotlin.idea.inspections.OldStdlibApiInspection |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
10
idea/testData/quickfix/migration/stdlib/basic.before.Main.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
10
idea/testData/quickfix/migration/stdlib/importStatic.after.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
idea/testData/quickfix/migration/stdlib/importStatic.before.Main.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
10
idea/testData/quickfix/migration/stdlib/method.before.Main.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters