Java: disallow inlining record to anonymous class (IDEA-359857)

GitOrigin-RevId: f477736ff39ea95cf9c9c5088c2dd0af9399f605
This commit is contained in:
Bas Leijdekkers
2024-09-28 16:57:08 +02:00
committed by intellij-monorepo-bot
parent 1c4ecd3c3c
commit 7be0072137
4 changed files with 19 additions and 3 deletions

View File

@@ -1,4 +1,4 @@
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.refactoring.inline;
import com.intellij.codeInsight.TargetElementUtil;
@@ -192,6 +192,9 @@ public final class InlineToAnonymousClassHandler extends JavaInlineActionHandler
if (psiClass.isEnum()) {
return JavaBundle.message("enums.cannot.be.inlined");
}
if (psiClass.isRecord()) {
return JavaBundle.message("records.cannot.be.inlined");
}
if (psiClass.hasModifierProperty(PsiModifier.ABSTRACT)) {
return JavaRefactoringBundle.message("inline.to.anonymous.no.abstract");
}
@@ -373,7 +376,6 @@ public final class InlineToAnonymousClassHandler extends JavaInlineActionHandler
}
}
@Nullable
@Override
public String getActionName(PsiElement element) {
return JavaRefactoringBundle.message("inline.to.anonymous.refactoring");

View File

@@ -0,0 +1,9 @@
class A {
private R r = new R<caret>();
}
record R() {
public String toString() {
return "A";
}
}

View File

@@ -283,6 +283,10 @@ public class InlineToAnonymousClassTest extends LightRefactoringTestCase {
public void testNoInlineAnnotationType() {
doTestNoInline("Annotation types cannot be inlined");
}
public void testNoInlineRecordJava21() {
doTestNoInline("Record classes cannot be inlined");
}
public void testNoInlineMultipleInterfaces() {
doTestNoInline("Classes which implement multiple interfaces cannot be inlined");
@@ -437,7 +441,7 @@ public class InlineToAnonymousClassTest extends LightRefactoringTestCase {
TargetElementUtil.REFERENCED_ELEMENT_ACCEPTED);
assertInstanceOf(element, PsiClass.class);
assertEquals(null, InlineToAnonymousClassHandler.getCannotInlineMessage((PsiClass) element));
assertNull(InlineToAnonymousClassHandler.getCannotInlineMessage((PsiClass)element));
return new InlineToAnonymousClassProcessor(getProject(), (PsiClass) element, null, false, false, false);
}

View File

@@ -1666,6 +1666,7 @@ class.cannot.be.inlined.because.its.superclass.cannot.be.resolved=Class cannot b
library.classes.cannot.be.inlined=Library classes cannot be inlined
enums.cannot.be.inlined=Enums cannot be inlined
interfaces.cannot.be.inlined=Interfaces cannot be inlined
records.cannot.be.inlined=Record classes cannot be inlined
annotation.types.cannot.be.inlined=Annotation types cannot be inlined
type.parameters.cannot.be.inlined=Type parameters cannot be inlined
postfix.template.editor.choose.class.title=Choose Class