IDEA-231427 Extract method produces incompilable code when extracting expression that produces a pattern variable

GitOrigin-RevId: 3fbd29e6543e21af934c4de8899bd6ed5bda6965
This commit is contained in:
Tagir Valeev
2020-01-27 06:33:15 +00:00
committed by intellij-monorepo-bot
parent f22fa4063b
commit 186596b687
6 changed files with 53 additions and 0 deletions
@@ -1599,6 +1599,15 @@ class ControlFlowAnalyzer extends JavaElementVisitor {
final PsiExpression operand = expression.getOperand();
operand.accept(this);
PsiPattern pattern = expression.getPattern();
if (pattern instanceof PsiTypeTestPattern) {
PsiPatternVariable variable = ((PsiTypeTestPattern)pattern).getPatternVariable();
if (variable != null) {
myCurrentFlow.addInstruction(new WriteVariableInstruction(variable));
}
}
finishElement(expression);
}
@@ -0,0 +1,7 @@
class X {
void test(Object obj) {
if (<selection>obj instanceof String s</selection> && s.length() > 5) {
System.out.println(s);
}
}
}
@@ -0,0 +1,7 @@
class X {
void test(Object obj) {
if (<selection>obj instanceof String s && s.length() > 5</selection>) {
System.out.println(s);
}
}
}
@@ -0,0 +1,7 @@
class X {
void test(Object obj) {
if (<selection>obj instanceof String s && s.length() > 5</selection>) {
System.out.println("found");
}
}
}
@@ -0,0 +1,11 @@
class X {
void test(Object obj) {
if (newMethod(obj)) {
System.out.println("found");
}
}
private boolean newMethod(Object obj) {
return obj instanceof String s && s.length() > 5;
}
}
@@ -945,6 +945,18 @@ public class ExtractMethodTest extends LightJavaCodeInsightTestCase {
doTestWithLanguageLevel(LanguageLevel.JDK_14_PREVIEW);
}
public void testPatternVariableIntroduced() throws Exception {
doExitPointsTest(false);
}
public void testPatternVariableIntroduced2() throws Exception {
doExitPointsTest(false);
}
public void testPatternVariableIntroduced3() throws Exception {
doTestWithLanguageLevel(LanguageLevel.JDK_14_PREVIEW);
}
public void testSuggestChangeSignatureWithChangedParameterName() throws Exception {
configureByFile(BASE_PATH + getTestName(false) + ".java");
boolean success = performExtractMethod(true, true, getEditor(), getFile(), getProject(), false, null, false, "p");