SealClassAction: show error if no inheritors for interface + cleanup

GitOrigin-RevId: b5a6dfbf1531dd788eed14a4364cd5431ce8e28e
This commit is contained in:
Roman.Ivanov
2020-07-09 04:35:50 +00:00
committed by intellij-monorepo-bot
parent 79938e9f8c
commit f2a37677b6
4 changed files with 14 additions and 6 deletions
@@ -71,7 +71,6 @@ public class SealClassAction extends BaseElementAtCaretIntentionAction {
if (aClass == null) return;
if (!isAvailable(aClass, editor)) return;
PsiJavaFile parentFile = (PsiJavaFile)aClass.getContainingFile();
FileModificationService.getInstance().prepareFileForWrite(parentFile);
if (aClass.isInterface()) {
if (FunctionalExpressionSearch.search(aClass).findFirst() != null) {
showError(project, editor, "intention.error.make.sealed.class.is.used.in.functional.expression");
@@ -119,12 +118,14 @@ public class SealClassAction extends BaseElementAtCaretIntentionAction {
}
else {
if (aClass.isInterface()) {
modifier = PsiModifier.SEALED;
showError(project, editor, "intention.error.make.sealed.class.interface.has.no.inheritors");
return;
}
else {
modifier = PsiModifier.FINAL;
}
}
FileModificationService.getInstance().prepareFileForWrite(parentFile);
ApplicationManager.getApplication().runWriteAction(() -> {
PsiModifierList modifierList = Objects.requireNonNull(aClass.getModifierList());
modifierList.setModifierProperty(modifier, true);
@@ -150,7 +151,7 @@ public class SealClassAction extends BaseElementAtCaretIntentionAction {
}
public void setInheritorsModifiers(@NotNull Project project, List<PsiClass> inheritors) {
String title = JavaBundle.message("intention.error.make.sealed.class.task.title.set.inheritors.modifiers");
String title = JavaBundle.message("intention.make.sealed.class.task.title.set.inheritors.modifiers");
SequentialModalProgressTask task = new SequentialModalProgressTask(project, title, true);
task.setTask(new SequentialTask() {
private final FileModificationService myFileModificationService = FileModificationService.getInstance();
@@ -207,6 +208,6 @@ public class SealClassAction extends BaseElementAtCaretIntentionAction {
}
private static String getErrorTitle() {
return JavaBundle.message("intention.error.make.sealed.class.hint.title");
return JavaBundle.message("intention.make.sealed.class.hint.title");
}
}
@@ -28,6 +28,10 @@ public class SealClassActionFailingTest extends LightJavaCodeInsightFixtureTestC
checkErrorMessage(JavaBundle.message("intention.error.make.sealed.class.has.anonymous.or.local.inheritors"));
}
public void testInterfaceWithoutInheritors() {
checkErrorMessage(JavaBundle.message("intention.error.make.sealed.class.interface.has.no.inheritors"));
}
public void testDifferentPackages() {
myFixture.addFileToProject("foo.java", "package other;\n class Other extends Parent {}");
checkErrorMessage(JavaBundle.message("intention.error.make.sealed.class.different.packages"));
@@ -1264,9 +1264,10 @@ intention.name.collapse.into.loop=Collapse into loop
intention.family.name.make.sealed=Seal class
intention.name.make.sealed=Make sealed
intention.error.make.sealed.class.is.used.in.functional.expression=Class is used in functional expression
intention.error.make.sealed.class.hint.title=Make Sealed
intention.make.sealed.class.hint.title=Make Sealed
intention.error.make.sealed.class.has.anonymous.or.local.inheritors=Some of the inheritors are anonymous or local
intention.error.make.sealed.class.different.packages=Module is unnamed and some of the inheritors are in the different package
intention.error.make.sealed.class.inheritors.not.in.java.file=Some of the inheritors are not in java files
intention.error.make.sealed.class.different.modules=Some of the inheritors are in different modules
intention.error.make.sealed.class.task.title.set.inheritors.modifiers=Setting inheritors modifiers
intention.error.make.sealed.class.interface.has.no.inheritors=Interface has no inheritors
intention.make.sealed.class.task.title.set.inheritors.modifiers=Setting inheritors modifiers