[java-refactoring] IDEA-368729 Refactor -> introduce field fails if the expression contains unresolved type

GitOrigin-RevId: 564e2f82913f777c5c9e0b760b27fc0a859ec430
This commit is contained in:
Tagir Valeev
2025-03-07 11:16:13 +00:00
committed by intellij-monorepo-bot
parent 5aa8e16842
commit 99844b3f87
5 changed files with 28 additions and 0 deletions
@@ -60,6 +60,12 @@ public class IntroduceFieldHandler extends BaseExpressionToFieldHandler implemen
showErrorMessage(parentClass.getProject(), editor, message);
return false;
}
if (PsiTypes.nullType().equals(type) || type instanceof PsiLambdaParameterType || type instanceof PsiLambdaExpressionType ||
type instanceof PsiMethodReferenceType) {
String message = JavaRefactoringBundle.message("variable.type.unknown");
showErrorMessage(parentClass.getProject(), editor, message);
return false;
}
PsiClass aClass = PsiUtil.resolveClassInClassTypeOnly(type);
if (aClass != null && PsiUtil.isLocalClass(aClass) && !PsiTreeUtil.isAncestor(aClass, parentClass, false)) {
String message = JavaRefactoringBundle.message("0.is.not.visible.to.members.of.1",
@@ -0,0 +1,5 @@
class X {
void test() {
var <caret>f = Set.of(1, 2);
}
}
@@ -0,0 +1,5 @@
class X {
void test() {
var <caret>f = s -> {};
}
}
@@ -6,6 +6,7 @@ import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiExpression;
import com.intellij.psi.PsiLocalVariable;
import com.intellij.refactoring.introduceField.IntroduceFieldHandler;
import com.intellij.refactoring.util.CommonRefactoringUtil;
import com.intellij.testFramework.LightProjectDescriptor;
import org.jetbrains.annotations.NotNull;
@@ -87,6 +88,16 @@ public class InplaceIntroduceFieldTest extends AbstractJavaInplaceIntroduceTest
doTest(null);
}
public void testVarUnknownType() {
assertThrows(CommonRefactoringUtil.RefactoringErrorHintException.class,
"Cannot perform refactoring.\nVariable type is unknown", () -> doTest(null));
}
public void testVarUnknownType2() {
assertThrows(CommonRefactoringUtil.RefactoringErrorHintException.class,
"Cannot perform refactoring.\nVariable type is unknown", () -> doTest(null));
}
@Override
protected String getBasePath() {
return BASE_PATH;
@@ -63,6 +63,7 @@ boolean.method.result=boolean method result
can.t.restore.context.for.method.extraction=Can't restore context for method extraction
cannot.find.or.create.destination.directory=Cannot find or create destination directory
cannot.introduce.field.in.interface=Non-constant fields are not allowed in an interface.
variable.type.unknown=Variable type is unknown
0.is.not.visible.to.members.of.1={0} is not visible to members of {1}
cannot.move=Cannot Move
cannot.move.inner.class.0.into.itself=Cannot move inner class {0} into itself