[java-refactoring] Introduce variable: do not try to add cast if the type is non-variable type

Found by property testing
Fixes EA-248598 - [ExtractVar] AE: JavaParserUtil.parseFragment

GitOrigin-RevId: 91d32dd33ef6c75ed750419b06654acffba6d684
This commit is contained in:
Tagir Valeev
2020-12-21 10:44:42 +00:00
committed by intellij-monorepo-bot
parent 9276b73b49
commit e6102de0d2
6 changed files with 66 additions and 9 deletions
@@ -23,8 +23,6 @@ import com.intellij.openapi.actionSystem.Shortcut;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.command.CommandProcessor;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.diagnostic.Attachment;
import com.intellij.openapi.diagnostic.RuntimeExceptionWithAttachments;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.RangeMarker;
@@ -370,13 +368,8 @@ public class JavaVariableInplaceIntroducer extends AbstractJavaInplaceIntroducer
final PsiExpression initializer = psiVariable.getInitializer();
LOG.assertTrue(initializer != null);
final PsiType type = initializer.getType();
if (((PsiReferenceExpression)parent).resolve() == null && type != null && !type.equals(psiVariable.getType())) {
if (LambdaUtil.notInferredType(type)) {
throw new RuntimeExceptionWithAttachments(
"Unexpected initializer type: " + type,
new Attachment("variable.txt", psiVariable.getText()), new Attachment("reference.txt", parent.getText()),
new Attachment("file.txt", file.getText()));
}
if (((PsiReferenceExpression)parent).resolve() == null && type != null && !type.equals(psiVariable.getType()) &&
!LambdaUtil.notInferredType(type) && !PsiType.NULL.equals(type)) {
final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(project);
final PsiExpression castedExpr =
elementFactory.createExpressionFromText("((" + type.getCanonicalText() + ")" + referenceExpression.getText() + ")", parent);
@@ -0,0 +1,10 @@
public class lambdaParameterAddCast {
void consume(String str) {}
void test() {
Object r = foo -> {
consume(<caret>foo);
System.out.println(foo.blahblah());
};
}
}
@@ -0,0 +1,11 @@
public class lambdaParameterAddCast {
void consume(String str) {}
void test() {
Object r = foo -> {
String foo1 = (String) foo;
consume(foo1);
System.out.println(foo1.blahblah());
};
}
}
@@ -0,0 +1,17 @@
public class NullTypeAddCast {
static class MyMegaClass {
int myMegaMethod() {
return 0;
}
}
interface MyInterface {}
void consume(MyInterface intf) {}
void test() {
var x = foo;
consume(x);
System.out.println(<selection>x</selection>.myMegaMethod());
}
}
@@ -0,0 +1,18 @@
public class NullTypeAddCast {
static class MyMegaClass {
int myMegaMethod() {
return 0;
}
}
interface MyInterface {}
void consume(MyInterface intf) {}
void test() {
var x = foo;
Object x1 = x;
consume(x1);
System.out.println(x1.myMegaMethod());
}
}
@@ -212,6 +212,14 @@ public class InplaceIntroduceVariableTest extends AbstractJavaInplaceIntroduceTe
doTest(null);
}
public void testNullTypeAddCast() {
doTestReplaceChoice("Replace all 0 occurrences");
}
public void testLambdaParameterAddCast() {
doTestReplaceChoice("Replace all 0 occurrences");
}
private void doTestStopEditing(Consumer<? super AbstractInplaceIntroducer> pass) {
String name = getTestName(true);
configureByFile(getBasePath() + name + getExtension());