J2K: no redundant type casts
#KT-6794 Fixed
This commit is contained in:
@@ -17,15 +17,16 @@
|
||||
package org.jetbrains.kotlin.idea.j2k
|
||||
|
||||
import com.intellij.psi.PsiElement
|
||||
import org.jetbrains.kotlin.diagnostics.Diagnostic
|
||||
import org.jetbrains.kotlin.diagnostics.Errors
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyzeFullyAndGetResult
|
||||
import org.jetbrains.kotlin.idea.intentions.RemoveExplicitTypeArguments
|
||||
import org.jetbrains.kotlin.idea.intentions.SimplifyNegatedBinaryExpressionIntention
|
||||
import org.jetbrains.kotlin.idea.quickfix.RemoveRightPartOfBinaryExpressionFix
|
||||
import org.jetbrains.kotlin.j2k.PostProcessor
|
||||
import org.jetbrains.kotlin.psi.JetFile
|
||||
import org.jetbrains.kotlin.psi.JetPrefixExpression
|
||||
import org.jetbrains.kotlin.psi.JetTreeVisitorVoid
|
||||
import org.jetbrains.kotlin.psi.JetTypeArgumentList
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.resolve.BindingContext
|
||||
import org.jetbrains.kotlin.utils.printAndReturn
|
||||
import java.util.ArrayList
|
||||
|
||||
public class J2kPostProcessor(override val contextToAnalyzeIn: PsiElement) : PostProcessor {
|
||||
@@ -33,6 +34,17 @@ public class J2kPostProcessor(override val contextToAnalyzeIn: PsiElement) : Pos
|
||||
return file.analyzeFullyAndGetResult().bindingContext
|
||||
}
|
||||
|
||||
override fun fixForProblem(problem: Diagnostic): (() -> Unit)? {
|
||||
val psiElement = problem.getPsiElement()
|
||||
return when (problem.getFactory()) {
|
||||
Errors.USELESS_CAST, Errors.USELESS_CAST_STATIC_ASSERT_IS_FINE -> { ->
|
||||
RemoveRightPartOfBinaryExpressionFix(psiElement as JetBinaryExpressionWithTypeRHS, "").invoke()
|
||||
}
|
||||
|
||||
else -> super.fixForProblem(problem)
|
||||
}
|
||||
}
|
||||
|
||||
override fun doAdditionalProcessing(file: JetFile) {
|
||||
val redundantTypeArgs = ArrayList<JetTypeArgumentList>()
|
||||
file.accept(object : JetTreeVisitorVoid(){
|
||||
|
||||
@@ -45,6 +45,10 @@ public class RemoveRightPartOfBinaryExpressionFix<T extends JetExpression> exten
|
||||
|
||||
@Override
|
||||
public void invoke(@NotNull Project project, Editor editor, JetFile file) throws IncorrectOperationException {
|
||||
invoke();
|
||||
}
|
||||
|
||||
public void invoke() throws IncorrectOperationException {
|
||||
PsiElement newElement = null;
|
||||
|
||||
if (element instanceof JetBinaryExpression) {
|
||||
|
||||
@@ -32,7 +32,7 @@ class AfterConversionPass(val project: Project, val postProcessor: PostProcessor
|
||||
val bindingContext = postProcessor.analyzeFile(kotlinFile)
|
||||
|
||||
val fixes = bindingContext.getDiagnostics().map {
|
||||
val fix = fixForProblem(it)
|
||||
val fix = postProcessor.fixForProblem(it)
|
||||
if (fix != null) it.getPsiElement() to fix else null
|
||||
}.filterNotNull()
|
||||
|
||||
@@ -46,24 +46,4 @@ class AfterConversionPass(val project: Project, val postProcessor: PostProcessor
|
||||
|
||||
return kotlinFile.getText()!!
|
||||
}
|
||||
|
||||
private fun fixForProblem(problem: Diagnostic): (() -> Unit)? {
|
||||
val psiElement = problem.getPsiElement()
|
||||
return when (problem.getFactory()) {
|
||||
Errors.UNNECESSARY_NOT_NULL_ASSERTION -> { ->
|
||||
val exclExclOp = psiElement as JetSimpleNameExpression
|
||||
val exclExclExpr = exclExclOp.getParent() as JetUnaryExpression
|
||||
exclExclExpr.replace(exclExclExpr.getBaseExpression()!!)
|
||||
}
|
||||
|
||||
Errors.VAL_REASSIGNMENT -> { ->
|
||||
val property = (psiElement as? JetSimpleNameExpression)?.getReference()?.resolve() as? JetProperty
|
||||
if (property != null && !property.isVar()) {
|
||||
property.getValOrVarNode().getPsi()!!.replace(JetPsiFactory(project).createVarNode().getPsi()!!)
|
||||
}
|
||||
}
|
||||
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,16 +16,18 @@
|
||||
|
||||
package org.jetbrains.kotlin.j2k
|
||||
|
||||
import com.intellij.psi.PsiElement
|
||||
import com.intellij.openapi.progress.ProcessCanceledException
|
||||
import org.jetbrains.kotlin.j2k.ast.Element
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.psi.PsiJavaFile
|
||||
import org.jetbrains.kotlin.psi.JetFile
|
||||
import org.jetbrains.kotlin.resolve.BindingContext
|
||||
import com.intellij.openapi.diagnostic.Logger
|
||||
import java.util.ArrayList
|
||||
import com.intellij.openapi.progress.ProcessCanceledException
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.psi.PsiElement
|
||||
import com.intellij.psi.PsiJavaFile
|
||||
import org.jetbrains.kotlin.diagnostics.Diagnostic
|
||||
import org.jetbrains.kotlin.diagnostics.Errors
|
||||
import org.jetbrains.kotlin.j2k.ast.Element
|
||||
import org.jetbrains.kotlin.j2k.usageProcessing.UsageProcessing
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.resolve.BindingContext
|
||||
import java.util.ArrayList
|
||||
import java.util.HashMap
|
||||
|
||||
public trait ConversionScope {
|
||||
@@ -39,6 +41,28 @@ public class FilesConversionScope(val files: Collection<PsiJavaFile>) : Conversi
|
||||
public trait PostProcessor {
|
||||
public val contextToAnalyzeIn: PsiElement
|
||||
public fun analyzeFile(file: JetFile): BindingContext
|
||||
|
||||
public open fun fixForProblem(problem: Diagnostic): (() -> Unit)? {
|
||||
val psiElement = problem.getPsiElement()
|
||||
return when (problem.getFactory()) {
|
||||
Errors.UNNECESSARY_NOT_NULL_ASSERTION -> { ->
|
||||
val exclExclOp = psiElement as JetSimpleNameExpression
|
||||
val exclExclExpr = exclExclOp.getParent() as JetUnaryExpression
|
||||
exclExclExpr.replace(exclExclExpr.getBaseExpression()!!)
|
||||
}
|
||||
|
||||
Errors.VAL_REASSIGNMENT -> { ->
|
||||
val property = (psiElement as? JetSimpleNameExpression)?.getReference()?.resolve() as? JetProperty
|
||||
if (property != null && !property.isVar()) {
|
||||
val factory = JetPsiFactory(contextToAnalyzeIn.getProject())
|
||||
property.getValOrVarNode().getPsi()!!.replace(factory.createVarNode().getPsi()!!)
|
||||
}
|
||||
}
|
||||
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
|
||||
public fun doAdditionalProcessing(file: JetFile)
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
class C {
|
||||
void foo(Object o) {
|
||||
if (o instanceof String) {
|
||||
int l = ((String) o).length();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
class C {
|
||||
fun foo(o: Any) {
|
||||
if (o is String) {
|
||||
val l = o.length()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -987,6 +987,12 @@ public class JavaToKotlinConverterForWebDemoTestGenerated extends AbstractJavaTo
|
||||
String fileName = JetTestUtils.navigationMetadata("j2k/testData/fileOrElement/codeSimplifications/NotIs.java");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("RedundantTypeCast.java")
|
||||
public void testRedundantTypeCast() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("j2k/testData/fileOrElement/codeSimplifications/RedundantTypeCast.java");
|
||||
doTest(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("j2k/testData/fileOrElement/comments")
|
||||
|
||||
@@ -987,6 +987,12 @@ public class JavaToKotlinConverterSingleFileTestGenerated extends AbstractJavaTo
|
||||
String fileName = JetTestUtils.navigationMetadata("j2k/testData/fileOrElement/codeSimplifications/NotIs.java");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("RedundantTypeCast.java")
|
||||
public void testRedundantTypeCast() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("j2k/testData/fileOrElement/codeSimplifications/RedundantTypeCast.java");
|
||||
doTest(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("j2k/testData/fileOrElement/comments")
|
||||
|
||||
Reference in New Issue
Block a user