Skip to content

Commit 555ae1e

Browse files
committed
J2K: no redundant type casts
#KT-6794 Fixed
1 parent 7d92559 commit 555ae1e

File tree

8 files changed

+76
-30
lines changed

8 files changed

+76
-30
lines changed

idea/src/org/jetbrains/kotlin/idea/j2k/J2kPostProcessor.kt

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,34 @@
1717
package org.jetbrains.kotlin.idea.j2k
1818

1919
import com.intellij.psi.PsiElement
20+
import org.jetbrains.kotlin.diagnostics.Diagnostic
21+
import org.jetbrains.kotlin.diagnostics.Errors
2022
import org.jetbrains.kotlin.idea.caches.resolve.analyzeFullyAndGetResult
2123
import org.jetbrains.kotlin.idea.intentions.RemoveExplicitTypeArguments
2224
import org.jetbrains.kotlin.idea.intentions.SimplifyNegatedBinaryExpressionIntention
25+
import org.jetbrains.kotlin.idea.quickfix.RemoveRightPartOfBinaryExpressionFix
2326
import org.jetbrains.kotlin.j2k.PostProcessor
24-
import org.jetbrains.kotlin.psi.JetFile
25-
import org.jetbrains.kotlin.psi.JetPrefixExpression
26-
import org.jetbrains.kotlin.psi.JetTreeVisitorVoid
27-
import org.jetbrains.kotlin.psi.JetTypeArgumentList
27+
import org.jetbrains.kotlin.psi.*
2828
import org.jetbrains.kotlin.resolve.BindingContext
29+
import org.jetbrains.kotlin.utils.printAndReturn
2930
import java.util.ArrayList
3031

3132
public class J2kPostProcessor(override val contextToAnalyzeIn: PsiElement) : PostProcessor {
3233
override fun analyzeFile(file: JetFile): BindingContext {
3334
return file.analyzeFullyAndGetResult().bindingContext
3435
}
3536

37+
override fun fixForProblem(problem: Diagnostic): (() -> Unit)? {
38+
val psiElement = problem.getPsiElement()
39+
return when (problem.getFactory()) {
40+
Errors.USELESS_CAST, Errors.USELESS_CAST_STATIC_ASSERT_IS_FINE -> { ->
41+
RemoveRightPartOfBinaryExpressionFix(psiElement as JetBinaryExpressionWithTypeRHS, "").invoke()
42+
}
43+
44+
else -> super.fixForProblem(problem)
45+
}
46+
}
47+
3648
override fun doAdditionalProcessing(file: JetFile) {
3749
val redundantTypeArgs = ArrayList<JetTypeArgumentList>()
3850
file.accept(object : JetTreeVisitorVoid(){

idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveRightPartOfBinaryExpressionFix.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ public String getFamilyName() {
4545

4646
@Override
4747
public void invoke(@NotNull Project project, Editor editor, JetFile file) throws IncorrectOperationException {
48+
invoke();
49+
}
50+
51+
public void invoke() throws IncorrectOperationException {
4852
PsiElement newElement = null;
4953

5054
if (element instanceof JetBinaryExpression) {

j2k/src/org/jetbrains/kotlin/j2k/AfterConversionPass.kt

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class AfterConversionPass(val project: Project, val postProcessor: PostProcessor
3232
val bindingContext = postProcessor.analyzeFile(kotlinFile)
3333

3434
val fixes = bindingContext.getDiagnostics().map {
35-
val fix = fixForProblem(it)
35+
val fix = postProcessor.fixForProblem(it)
3636
if (fix != null) it.getPsiElement() to fix else null
3737
}.filterNotNull()
3838

@@ -46,24 +46,4 @@ class AfterConversionPass(val project: Project, val postProcessor: PostProcessor
4646

4747
return kotlinFile.getText()!!
4848
}
49-
50-
private fun fixForProblem(problem: Diagnostic): (() -> Unit)? {
51-
val psiElement = problem.getPsiElement()
52-
return when (problem.getFactory()) {
53-
Errors.UNNECESSARY_NOT_NULL_ASSERTION -> { ->
54-
val exclExclOp = psiElement as JetSimpleNameExpression
55-
val exclExclExpr = exclExclOp.getParent() as JetUnaryExpression
56-
exclExclExpr.replace(exclExclExpr.getBaseExpression()!!)
57-
}
58-
59-
Errors.VAL_REASSIGNMENT -> { ->
60-
val property = (psiElement as? JetSimpleNameExpression)?.getReference()?.resolve() as? JetProperty
61-
if (property != null && !property.isVar()) {
62-
property.getValOrVarNode().getPsi()!!.replace(JetPsiFactory(project).createVarNode().getPsi()!!)
63-
}
64-
}
65-
66-
else -> null
67-
}
68-
}
6949
}

j2k/src/org/jetbrains/kotlin/j2k/JavaToKotlinConverter.kt

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,18 @@
1616

1717
package org.jetbrains.kotlin.j2k
1818

19-
import com.intellij.psi.PsiElement
19+
import com.intellij.openapi.diagnostic.Logger
2020
import com.intellij.openapi.progress.ProcessCanceledException
21-
import org.jetbrains.kotlin.j2k.ast.Element
2221
import com.intellij.openapi.project.Project
22+
import com.intellij.psi.PsiElement
2323
import com.intellij.psi.PsiJavaFile
24-
import org.jetbrains.kotlin.psi.JetFile
24+
import org.jetbrains.kotlin.diagnostics.Diagnostic
25+
import org.jetbrains.kotlin.diagnostics.Errors
26+
import org.jetbrains.kotlin.j2k.ast.Element
27+
import org.jetbrains.kotlin.j2k.usageProcessing.UsageProcessing
28+
import org.jetbrains.kotlin.psi.*
2529
import org.jetbrains.kotlin.resolve.BindingContext
26-
import com.intellij.openapi.diagnostic.Logger
2730
import java.util.ArrayList
28-
import org.jetbrains.kotlin.j2k.usageProcessing.UsageProcessing
2931
import java.util.HashMap
3032

3133
public trait ConversionScope {
@@ -39,6 +41,28 @@ public class FilesConversionScope(val files: Collection<PsiJavaFile>) : Conversi
3941
public trait PostProcessor {
4042
public val contextToAnalyzeIn: PsiElement
4143
public fun analyzeFile(file: JetFile): BindingContext
44+
45+
public open fun fixForProblem(problem: Diagnostic): (() -> Unit)? {
46+
val psiElement = problem.getPsiElement()
47+
return when (problem.getFactory()) {
48+
Errors.UNNECESSARY_NOT_NULL_ASSERTION -> { ->
49+
val exclExclOp = psiElement as JetSimpleNameExpression
50+
val exclExclExpr = exclExclOp.getParent() as JetUnaryExpression
51+
exclExclExpr.replace(exclExclExpr.getBaseExpression()!!)
52+
}
53+
54+
Errors.VAL_REASSIGNMENT -> { ->
55+
val property = (psiElement as? JetSimpleNameExpression)?.getReference()?.resolve() as? JetProperty
56+
if (property != null && !property.isVar()) {
57+
val factory = JetPsiFactory(contextToAnalyzeIn.getProject())
58+
property.getValOrVarNode().getPsi()!!.replace(factory.createVarNode().getPsi()!!)
59+
}
60+
}
61+
62+
else -> null
63+
}
64+
}
65+
4266
public fun doAdditionalProcessing(file: JetFile)
4367
}
4468

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class C {
2+
void foo(Object o) {
3+
if (o instanceof String) {
4+
int l = ((String) o).length();
5+
}
6+
}
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class C {
2+
fun foo(o: Any) {
3+
if (o is String) {
4+
val l = o.length()
5+
}
6+
}
7+
}

j2k/tests/org/jetbrains/kotlin/j2k/JavaToKotlinConverterForWebDemoTestGenerated.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -987,6 +987,12 @@ public void testNotIs() throws Exception {
987987
String fileName = JetTestUtils.navigationMetadata("j2k/testData/fileOrElement/codeSimplifications/NotIs.java");
988988
doTest(fileName);
989989
}
990+
991+
@TestMetadata("RedundantTypeCast.java")
992+
public void testRedundantTypeCast() throws Exception {
993+
String fileName = JetTestUtils.navigationMetadata("j2k/testData/fileOrElement/codeSimplifications/RedundantTypeCast.java");
994+
doTest(fileName);
995+
}
990996
}
991997

992998
@TestMetadata("j2k/testData/fileOrElement/comments")

j2k/tests/org/jetbrains/kotlin/j2k/JavaToKotlinConverterSingleFileTestGenerated.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -987,6 +987,12 @@ public void testNotIs() throws Exception {
987987
String fileName = JetTestUtils.navigationMetadata("j2k/testData/fileOrElement/codeSimplifications/NotIs.java");
988988
doTest(fileName);
989989
}
990+
991+
@TestMetadata("RedundantTypeCast.java")
992+
public void testRedundantTypeCast() throws Exception {
993+
String fileName = JetTestUtils.navigationMetadata("j2k/testData/fileOrElement/codeSimplifications/RedundantTypeCast.java");
994+
doTest(fileName);
995+
}
990996
}
991997

992998
@TestMetadata("j2k/testData/fileOrElement/comments")

0 commit comments

Comments
 (0)