Finish off old deprecated function/extension function classes

This reverts commit d14e5b8a72.
This commit is contained in:
Alexander Udalov
2015-06-09 21:38:19 +03:00
parent 5394abc83e
commit 64b60718e3
64 changed files with 5 additions and 1582 deletions
-6
View File
@@ -1046,12 +1046,6 @@
cleanupTool="true"
level="WARNING"/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.ReplaceDeprecatedFunctionClassUsages"
displayName="Usage of deprecated function classes in Java"
groupName="Kotlin"
enabledByDefault="true"
level="WARNING"/>
<project.converterProvider implementation="org.jetbrains.kotlin.idea.converters.JetRunConfigurationSettingsFormatConverterProvider"/>
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
@@ -23,7 +23,6 @@ import com.intellij.openapi.progress.ProcessCanceledException
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import com.intellij.psi.PsiJavaFile
import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.diagnostics.rendering.DefaultErrorMessages
@@ -46,17 +45,10 @@ public class KotlinCleanupInspection(): LocalInspectionTool(), CleanupLocalInspe
override fun getDisplayName(): String = "Usage of redundant or deprecated syntax or deprecated symbols"
override fun checkFile(file: PsiFile, manager: InspectionManager, isOnTheFly: Boolean): Array<out ProblemDescriptor>? {
if (isOnTheFly || !ProjectRootsUtil.isInProjectSource(file)) {
if (isOnTheFly || file !is JetFile || !ProjectRootsUtil.isInProjectSource(file)) {
return null
}
return when (file) {
is JetFile -> checkKotlinFile(file, manager)
is PsiJavaFile -> checkJavaFile(file, manager)
else -> null
}
}
private fun checkKotlinFile(file: JetFile, manager: InspectionManager): Array<out ProblemDescriptor>? {
val analysisResult = file.analyzeFullyAndGetResult()
if (analysisResult.isError()) {
throw ProcessCanceledException(analysisResult.error)
@@ -86,10 +78,6 @@ public class KotlinCleanupInspection(): LocalInspectionTool(), CleanupLocalInspe
return problems.sortBy { it.depth }.map { it.problemDescriptor }.toTypedArray()
}
private fun checkJavaFile(file: PsiJavaFile, manager: InspectionManager): Array<out ProblemDescriptor>? {
return ReplaceDeprecatedFunctionClassUsages().checkFile(file, manager)?.let { arrayOf(it) }
}
private fun Diagnostic.isCleanup() = getFactory() in cleanupDiagnosticsFactories || isObsoleteLabel()
private val cleanupDiagnosticsFactories = setOf(
@@ -1,91 +0,0 @@
/*
* 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.project.Project
import com.intellij.psi.*
import com.intellij.psi.codeStyle.JavaCodeStyleManager
import com.intellij.psi.impl.compiled.ClsClassImpl
import org.jetbrains.kotlin.idea.util.ProjectRootsUtil
import org.jetbrains.kotlin.load.java.lazy.DeprecatedFunctionClassFqNameParser
import java.util.ArrayList
public class ReplaceDeprecatedFunctionClassUsages : LocalInspectionTool() {
override fun checkFile(file: PsiFile, manager: InspectionManager, isOnTheFly: Boolean): Array<ProblemDescriptor>? {
if (isOnTheFly || !ProjectRootsUtil.isInProjectSource(file) || file !is PsiJavaFile) {
return null
}
return checkFile(file, manager)?.let { arrayOf(it) }
}
public fun checkFile(file: PsiJavaFile, manager: InspectionManager): ProblemDescriptor? {
val references = ArrayList<PsiJavaCodeReferenceElement>(0)
file.acceptChildren(object : JavaRecursiveElementVisitor() {
override fun visitReferenceElement(reference: PsiJavaCodeReferenceElement) {
if ("Function" in reference.getText() && extractFunctionClassFqName(reference) != null) {
references.add(reference)
}
super.visitElement(reference)
}
override fun visitImportList(list: PsiImportList?) {
// Skip import list for simplicity, update all other references instead and invoke "Optimize Imports"
}
})
return if (references.isEmpty()) null else manager.createProblemDescriptor(
file, MESSAGE, false, arrayOf(Fix(references)), ProblemHighlightType.GENERIC_ERROR_OR_WARNING
)
}
private class Fix(val references: List<PsiJavaCodeReferenceElement>) : LocalQuickFix {
override fun getName() = MESSAGE
override fun getFamilyName() = MESSAGE
override fun applyFix(project: Project, problem: ProblemDescriptor) {
val psiFacade = JavaPsiFacade.getInstance(project)
val codeStyleManager = JavaCodeStyleManager.getInstance(project)
val newReferences = ArrayList<PsiElement>(references.size())
for (reference in references) {
val newFqName = extractFunctionClassFqName(reference) ?: continue
val newClass = psiFacade.findClass(newFqName, reference.getResolveScope())
if (newClass != null) {
newReferences.add(reference.bindToElement(newClass))
}
}
codeStyleManager.optimizeImports(problem.getPsiElement().getContainingFile())
for (reference in newReferences) {
codeStyleManager.shortenClassReferences(reference)
}
}
}
companion object {
val MESSAGE = "Replace usages of deprecated Kotlin function classes in Java sources"
fun extractFunctionClassFqName(reference: PsiReference): String? {
val fqName = (reference.resolve() as? ClsClassImpl)?.getQualifiedName() ?: return null
return DeprecatedFunctionClassFqNameParser.extractOldAndNewFqName(fqName)?.second
}
}
}
@@ -1,33 +0,0 @@
import kotlin.Function0;
import kotlin.Function1;
import kotlin.KotlinPackage;
import kotlin.ExtensionFunction0;
import kotlin.Unit;
import kotlin.jvm.functions.Function2;
public class DeprecatedFunctionClasses {
void f1(Function0 f) {
f.invoke();
}
void f2() {
KotlinPackage.map(new int[]{}, new Function1<Integer, Object>() {
@Override
public Object invoke(Integer integer) {
return null;
}
});
}
void f3(Function2<String, String, String> f) {
}
void f4(kotlin.Function0<Unit> g) {
}
void f5(kotlin.jvm.functions.Function1 f) {
}
void f6(ExtensionFunction0<String, Integer> e) {
}
}
@@ -1,32 +0,0 @@
import kotlin.KotlinPackage;
import kotlin.Unit;
import kotlin.jvm.functions.Function0;
import kotlin.jvm.functions.Function1;
import kotlin.jvm.functions.Function2;
public class DeprecatedFunctionClasses {
void f1(Function0 f) {
f.invoke();
}
void f2() {
KotlinPackage.map(new int[]{}, new Function1<Integer, Object>() {
@Override
public Object invoke(Integer integer) {
return null;
}
});
}
void f3(Function2<String, String, String> f) {
}
void f4(Function0<Unit> g) {
}
void f5(kotlin.jvm.functions.Function1 f) {
}
void f6(Function1<String, Integer> e) {
}
}
@@ -48,8 +48,4 @@ class KotlinCleanupInspectionTest(): JetLightCodeInsightFixtureTestCase() {
public fun testCleanup() {
doTest("cleanup.kt.after", "cleanup.kt", "JavaAnn.java", "deprecatedSymbols.kt")
}
public fun testDeprecatedFunctionClasses() {
doTest("DeprecatedFunctionClasses.java.after", "DeprecatedFunctionClasses.java")
}
}