Make enums with fields with class bodies implicitly sealed

GitOrigin-RevId: c1077073a038df9853e3a38a33672bf4dc21c28c
This commit is contained in:
Roman.Ivanov
2020-07-09 03:04:40 +00:00
committed by intellij-monorepo-bot
parent cd624b4c86
commit be3b895bb2
7 changed files with 41 additions and 3 deletions
@@ -1091,6 +1091,12 @@ public class HighlightClassUtil {
if (psiClass.hasModifierProperty(PsiModifier.SEALED) && psiClass.getPermitsListTypes().length == 0) {
PsiIdentifier nameIdentifier = psiClass.getNameIdentifier();
if (nameIdentifier == null) return null;
if (psiClass.isEnum() && StreamEx.of(psiClass.getAllFields())
.select(PsiEnumConstant.class)
.anyMatch(field -> field.getInitializingClass() != null)
) {
return null;
}
if (!DirectClassInheritorsSearch.search(psiClass, new LocalSearchScope(psiClass.getContainingFile())).anyMatch(c -> !PsiUtil.isLocalOrAnonymousClass(c))) {
return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR)
.range(nameIdentifier)
@@ -1107,7 +1113,9 @@ public class HighlightClassUtil {
!aClass.hasModifierProperty(PsiModifier.SEALED) &&
!aClass.hasModifierProperty(PsiModifier.NON_SEALED) &&
!aClass.hasModifierProperty(PsiModifier.FINAL) &&
Arrays.stream(aClass.getSuperTypes()).map(type -> type.resolve()).anyMatch(superClass -> superClass != null && superClass.hasModifierProperty(PsiModifier.SEALED))) {
Arrays.stream(aClass.getSuperTypes())
.map(type -> type.resolve())
.anyMatch(superClass -> superClass != null && superClass.hasModifierProperty(PsiModifier.SEALED) && !superClass.isEnum())) {
HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(nameIdentifier)
.descriptionAndTooltip(JavaErrorBundle.message("sealed.type.inheritor.expected.modifiers", PsiModifier.SEALED, PsiModifier.NON_SEALED, PsiModifier.FINAL)).create();
if (!aClass.isInterface()) {
@@ -134,7 +134,10 @@ public class PsiModifierListImpl extends JavaStubPsiElement<PsiModifierListStub>
List<PsiField> fields = parent instanceof PsiExtensibleClass ? ((PsiExtensibleClass)parent).getOwnFields()
: Arrays.asList(((PsiClass)parent).getFields());
boolean hasSubClass = ContainerUtil.find(fields, field -> field instanceof PsiEnumConstant && ((PsiEnumConstant)field).getInitializingClass() != null) != null;
if (!hasSubClass) {
if (hasSubClass) {
implicitModifiers.add(SEALED);
}
else {
implicitModifiers.add(FINAL);
}
@@ -17,3 +17,7 @@ class <error descr="sealed, non-sealed or final modifiers expected">ICSameFile</
sealed interface ID0 {}
non-sealed interface ID1 extends ID0 {}
<error descr="Modifier 'non-sealed' not allowed here">non-sealed</error> interface ID extends ID1 {}
enum ImplicitlySealedEnum {
A {}
}
@@ -0,0 +1,9 @@
// "Seal class" "true"
sealed interface Parent {
}
enum Foo implements Parent {
A {}
}
@@ -0,0 +1,5 @@
// "Seal class" "false"
enum Fo<caret>o {
A {}
}
@@ -0,0 +1,9 @@
// "Seal class" "true"
interface Par<caret>ent {
}
enum Foo implements Parent {
A {}
}
@@ -1,4 +1,4 @@
public enum Style {
public sealed enum Style {
SHEET("foo") {
@java.lang.Override
public fun getExitAnimation() : java.lang.String {