[java-highlighting] IDEA-334683 (EA-921945) anonymous classes and local classes must not inherit sealed classes

- fix for completion
- fix for error highlighting

GitOrigin-RevId: 337131aab1bc7473711ffc078175b5adba34f30d
This commit is contained in:
Mikhail Pyltsin
2023-10-12 11:53:56 +00:00
committed by intellij-monorepo-bot
parent 83ca4601f9
commit 299b7e468b
6 changed files with 67 additions and 1 deletions
@@ -656,7 +656,7 @@ public class SwitchBlockHighlightingModel {
break;
}
}
return new SealedResult(missingClasses, coveredClasses) ;
return new SealedResult(missingClasses, coveredClasses);
}
static boolean isAbstractSealed(@Nullable PsiClass psiClass) {
@@ -1385,6 +1385,8 @@ public class SwitchBlockHighlightingModel {
if (permitsList == null) {
results = SyntaxTraverser.psiTraverser(psiClass.getContainingFile())
.filter(PsiClass.class)
//local classes and anonymous classes must not extend sealed
.filter(cls -> !(cls instanceof PsiAnonymousClass || PsiUtil.isLocalClass(cls)))
.filter(cls -> cls.isInheritor(psiClass, false))
.toList();
}
@@ -89,6 +89,8 @@ public final class SealedUtils {
GlobalSearchScope fileScope = GlobalSearchScope.fileScope(psiClass.getContainingFile().getOriginalFile());
return DirectClassInheritorsSearch.search(psiClass, fileScope)
.filtering(inheritor -> !ArrayUtil.contains(inheritor, classesToExclude))
//local classes and anonymous classes must not extend sealed
.filtering(cls -> !(cls instanceof PsiAnonymousClass || PsiUtil.isLocalClass(cls)))
.mapping(mapper)
.findAll();
}
@@ -0,0 +1,25 @@
public class Main {
sealed interface I {
final class C1 implements I {
}
static void test() {
final class TT implements I {
}
I i2 = new I() {
};
I i = getI();
switch (i) {
<caret>
}
}
private static I getI() {
return null;
}
}
}
@@ -0,0 +1,27 @@
public class SealedWithLocalAndAnonymousClasses {
sealed interface I {
final class C1 implements I {
}
static void test() {
final class TT implements <error descr="Local classes must not extend sealed classes">I</error> {
}
I i2 = new <error descr="Anonymous classes must not extend sealed classes">I</error>() {
};
I i = getI();
switch (i) {
case C1 c1 -> {
System.out.println("1");
}
}
}
private static I getI() {
return null;
}
}
}
@@ -78,6 +78,12 @@ public class NormalSwitchCompletionVariantsTest extends LightFixtureCompletionTe
assertDoesntContain(lookup, "case A");
assertContainsElements(lookup, "case B");
}
@NeedsIndex.Full
public void testCompletionAnonymousAndLocalNotShow() {
List<String> lookup = doTestAndGetLookup();
assertNotNull(lookup);
assertContainsElements(lookup, "case C1");
}
@NeedsIndex.Full
public void testCompletionSmartCase() {
@@ -208,6 +208,10 @@ public class LightPatternsForSwitchHighlightingTest extends LightJavaCodeInsight
doTest();
}
public void testSealedWithLocalAndAnonymousClasses() {
doTest();
}
private void doTest() {
myFixture.configureByFile(getTestName(false) + ".java");
myFixture.checkHighlighting();