J2K: inlining intermediate variable after removing type cast
This commit is contained in:
@@ -17,6 +17,8 @@
|
|||||||
package org.jetbrains.kotlin.idea.j2k
|
package org.jetbrains.kotlin.idea.j2k
|
||||||
|
|
||||||
import com.intellij.psi.PsiElement
|
import com.intellij.psi.PsiElement
|
||||||
|
import com.intellij.psi.search.LocalSearchScope
|
||||||
|
import com.intellij.psi.search.searches.ReferencesSearch
|
||||||
import org.jetbrains.kotlin.diagnostics.Diagnostic
|
import org.jetbrains.kotlin.diagnostics.Diagnostic
|
||||||
import org.jetbrains.kotlin.diagnostics.Errors
|
import org.jetbrains.kotlin.diagnostics.Errors
|
||||||
import org.jetbrains.kotlin.idea.caches.resolve.analyzeFullyAndGetResult
|
import org.jetbrains.kotlin.idea.caches.resolve.analyzeFullyAndGetResult
|
||||||
@@ -39,7 +41,17 @@ public class J2kPostProcessor(override val contextToAnalyzeIn: PsiElement) : Pos
|
|||||||
val psiElement = problem.getPsiElement()
|
val psiElement = problem.getPsiElement()
|
||||||
return when (problem.getFactory()) {
|
return when (problem.getFactory()) {
|
||||||
Errors.USELESS_CAST, Errors.USELESS_CAST_STATIC_ASSERT_IS_FINE -> { ->
|
Errors.USELESS_CAST, Errors.USELESS_CAST_STATIC_ASSERT_IS_FINE -> { ->
|
||||||
RemoveRightPartOfBinaryExpressionFix(psiElement as JetBinaryExpressionWithTypeRHS, "").invoke()
|
val expression = RemoveRightPartOfBinaryExpressionFix(psiElement as JetBinaryExpressionWithTypeRHS, "").invoke()
|
||||||
|
|
||||||
|
val variable = expression.getParent() as? JetProperty
|
||||||
|
if (variable != null && expression == variable.getInitializer() && variable.isLocal()) {
|
||||||
|
val refs = ReferencesSearch.search(variable, LocalSearchScope(variable.getContainingFile())).findAll()
|
||||||
|
for (ref in refs) {
|
||||||
|
val usage = ref.getElement() as? JetSimpleNameExpression ?: continue
|
||||||
|
usage.replace(expression)
|
||||||
|
}
|
||||||
|
variable.delete()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
else -> super.fixForProblem(problem)
|
else -> super.fixForProblem(problem)
|
||||||
|
|||||||
+9
-6
@@ -48,21 +48,24 @@ public class RemoveRightPartOfBinaryExpressionFix<T extends JetExpression> exten
|
|||||||
invoke();
|
invoke();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void invoke() throws IncorrectOperationException {
|
@NotNull
|
||||||
PsiElement newElement = null;
|
public JetExpression invoke() throws IncorrectOperationException {
|
||||||
|
JetExpression newExpression = null;
|
||||||
|
|
||||||
if (element instanceof JetBinaryExpression) {
|
if (element instanceof JetBinaryExpression) {
|
||||||
//noinspection ConstantConditions
|
//noinspection ConstantConditions
|
||||||
newElement = element.replace(((JetBinaryExpression) element.copy()).getLeft());
|
newExpression = (JetExpression) element.replace(((JetBinaryExpression) element.copy()).getLeft());
|
||||||
}
|
}
|
||||||
else if (element instanceof JetBinaryExpressionWithTypeRHS) {
|
else if (element instanceof JetBinaryExpressionWithTypeRHS) {
|
||||||
newElement = element.replace(((JetBinaryExpressionWithTypeRHS) element.copy()).getLeft());
|
newExpression = (JetExpression) element.replace(((JetBinaryExpressionWithTypeRHS) element.copy()).getLeft());
|
||||||
}
|
}
|
||||||
|
|
||||||
PsiElement parent = newElement != null ? newElement.getParent() : null;
|
PsiElement parent = newExpression != null ? newExpression.getParent() : null;
|
||||||
if (parent instanceof JetParenthesizedExpression && JetPsiUtil.areParenthesesUseless((JetParenthesizedExpression) parent)) {
|
if (parent instanceof JetParenthesizedExpression && JetPsiUtil.areParenthesesUseless((JetParenthesizedExpression) parent)) {
|
||||||
parent.replace(newElement);
|
newExpression = (JetExpression) parent.replace(newExpression);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return newExpression;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static JetSingleIntentionActionFactory createRemoveCastFactory() {
|
public static JetSingleIntentionActionFactory createRemoveCastFactory() {
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
import java.lang.String;
|
||||||
|
|
||||||
|
class C {
|
||||||
|
void foo(Object o) {
|
||||||
|
if (o instanceof String) {
|
||||||
|
String s = (String) o;
|
||||||
|
int l = s.length();
|
||||||
|
String substring = s.substring(l - 2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
class C {
|
||||||
|
fun foo(o: Any) {
|
||||||
|
if (o is String) {
|
||||||
|
val l = o.length()
|
||||||
|
val substring = o.substring(l - 2)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1005,6 +1005,12 @@ public class JavaToKotlinConverterForWebDemoTestGenerated extends AbstractJavaTo
|
|||||||
String fileName = JetTestUtils.navigationMetadata("j2k/testData/fileOrElement/codeSimplifications/RedundantTypeCast.java");
|
String fileName = JetTestUtils.navigationMetadata("j2k/testData/fileOrElement/codeSimplifications/RedundantTypeCast.java");
|
||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("RedundantTypeCastAndInline.java")
|
||||||
|
public void testRedundantTypeCastAndInline() throws Exception {
|
||||||
|
String fileName = JetTestUtils.navigationMetadata("j2k/testData/fileOrElement/codeSimplifications/RedundantTypeCastAndInline.java");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@TestMetadata("j2k/testData/fileOrElement/comments")
|
@TestMetadata("j2k/testData/fileOrElement/comments")
|
||||||
|
|||||||
@@ -1005,6 +1005,12 @@ public class JavaToKotlinConverterSingleFileTestGenerated extends AbstractJavaTo
|
|||||||
String fileName = JetTestUtils.navigationMetadata("j2k/testData/fileOrElement/codeSimplifications/RedundantTypeCast.java");
|
String fileName = JetTestUtils.navigationMetadata("j2k/testData/fileOrElement/codeSimplifications/RedundantTypeCast.java");
|
||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("RedundantTypeCastAndInline.java")
|
||||||
|
public void testRedundantTypeCastAndInline() throws Exception {
|
||||||
|
String fileName = JetTestUtils.navigationMetadata("j2k/testData/fileOrElement/codeSimplifications/RedundantTypeCastAndInline.java");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@TestMetadata("j2k/testData/fileOrElement/comments")
|
@TestMetadata("j2k/testData/fileOrElement/comments")
|
||||||
|
|||||||
Reference in New Issue
Block a user