mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Java: allow introducing static field inside a record (IDEA-312819)
GitOrigin-RevId: 0f71260012ef1e16f02c7203ab4ef6ed74179936
This commit is contained in:
committed by
intellij-monorepo-bot
parent
c9e5cd0ed9
commit
eeed0d8a1f
+2
-2
@@ -166,7 +166,7 @@ public abstract class BaseExpressionToFieldHandler extends IntroduceHandlerBase
|
||||
}
|
||||
}
|
||||
|
||||
if (!validClass(myParentClass, editor)) {
|
||||
if (!validClass(myParentClass, selectedExpr, editor)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -262,7 +262,7 @@ public abstract class BaseExpressionToFieldHandler extends IntroduceHandlerBase
|
||||
return myParentClass;
|
||||
}
|
||||
|
||||
protected abstract boolean validClass(PsiClass parentClass, Editor editor);
|
||||
protected abstract boolean validClass(PsiClass parentClass, PsiExpression selectedExpr, Editor editor);
|
||||
|
||||
private static PsiElement getNormalizedAnchor(PsiElement anchorElement) {
|
||||
PsiElement child = anchorElement;
|
||||
|
||||
+1
-1
@@ -257,7 +257,7 @@ public class IntroduceConstantHandler extends BaseExpressionToFieldHandler imple
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean validClass(PsiClass parentClass, Editor editor) {
|
||||
protected boolean validClass(PsiClass parentClass, PsiExpression selectedExpr, Editor editor) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
+11
-4
@@ -35,10 +35,17 @@ public class IntroduceFieldHandler extends BaseExpressionToFieldHandler implemen
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean validClass(PsiClass parentClass, Editor editor) {
|
||||
if (parentClass.isInterface() || parentClass.isRecord()) {
|
||||
String message = RefactoringBundle.getCannotRefactorMessage(
|
||||
JavaRefactoringBundle.message(parentClass.isRecord() ? "cannot.introduce.field.in.record" : "cannot.introduce.field.in.interface"));
|
||||
protected boolean validClass(PsiClass parentClass, PsiExpression selectedExpr, Editor editor) {
|
||||
if (parentClass.isInterface()) {
|
||||
String message = RefactoringBundle.getCannotRefactorMessage(JavaRefactoringBundle.message("cannot.introduce.field.in.interface"));
|
||||
CommonRefactoringUtil.showErrorHint(parentClass.getProject(), editor, message, getRefactoringNameText(), getHelpID());
|
||||
return false;
|
||||
}
|
||||
else if (parentClass.isRecord()) {
|
||||
final PsiModifierListOwner staticParentElement = PsiUtil.getEnclosingStaticElement(selectedExpr, parentClass);
|
||||
boolean declareStatic = staticParentElement != null;
|
||||
if (declareStatic) return true;
|
||||
String message = RefactoringBundle.getCannotRefactorMessage(JavaRefactoringBundle.message("cannot.introduce.field.in.record"));
|
||||
CommonRefactoringUtil.showErrorHint(parentClass.getProject(), editor, message, getRefactoringNameText(), getHelpID());
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
record R() {
|
||||
public static final String string = "";
|
||||
|
||||
private static void x(int i) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
record R() {
|
||||
private void x(int i) {
|
||||
<selection>""</selection>
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
record R() {
|
||||
private static void x(int i) {
|
||||
<selection>""</selection>
|
||||
}
|
||||
}
|
||||
+15
@@ -116,6 +116,21 @@ public class IntroduceFieldInSameClassTest extends LightJavaCodeInsightTestCase
|
||||
}
|
||||
}
|
||||
|
||||
public void testDisallowFieldInRecord() {
|
||||
configureByFile("beforeDisallowFieldInRecord.java");
|
||||
try {
|
||||
performRefactoring(BaseExpressionToFieldHandler.InitializationPlace.IN_FIELD_DECLARATION, false);
|
||||
fail("Should not proceed");
|
||||
}
|
||||
catch (CommonRefactoringUtil.RefactoringErrorHintException e) {
|
||||
assertEquals("Cannot perform refactoring.\nInstance fields are not allowed in a record.", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void testStaticFieldInRecord() {
|
||||
doTest(BaseExpressionToFieldHandler.InitializationPlace.IN_FIELD_DECLARATION, true);
|
||||
}
|
||||
|
||||
public void testAcceptIntroduceFieldFromExprInThisCall() {
|
||||
doTest(BaseExpressionToFieldHandler.InitializationPlace.IN_FIELD_DECLARATION, true);
|
||||
}
|
||||
|
||||
@@ -57,8 +57,8 @@ auto.rename.module.entity=Java Module
|
||||
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=Instance fields are not allowed in the interface
|
||||
cannot.introduce.field.in.record=Instance fields are not allowed in the record
|
||||
cannot.introduce.field.in.interface=Non-constant fields are not allowed in an interface.
|
||||
cannot.introduce.field.in.record=Instance fields are not allowed in a record.
|
||||
cannot.move=Cannot Move
|
||||
cannot.move.inner.class.0.into.itself=Cannot move inner class {0} into itself
|
||||
cannot.move.package.into.itself=Cannot move package into itself
|
||||
|
||||
Reference in New Issue
Block a user