From 2fd794b883b7a12dbd34613e9682aab3ae0f44ea Mon Sep 17 00:00:00 2001 From: Dmitry Batkovich Date: Thu, 11 May 2017 14:40:04 +0300 Subject: [PATCH] frequently used super class inspection: fix npe when classes has the same method not defined in base class --- .../inspection/ChangeSuperClassFix.java | 48 ++++++++++--------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/java/compiler/impl/src/com/intellij/compiler/inspection/ChangeSuperClassFix.java b/java/compiler/impl/src/com/intellij/compiler/inspection/ChangeSuperClassFix.java index 8831d772c5ad..a7638061e1cc 100644 --- a/java/compiler/impl/src/com/intellij/compiler/inspection/ChangeSuperClassFix.java +++ b/java/compiler/impl/src/com/intellij/compiler/inspection/ChangeSuperClassFix.java @@ -26,7 +26,6 @@ import com.intellij.openapi.ui.DialogWrapper; import com.intellij.openapi.util.Pair; import com.intellij.psi.*; import com.intellij.psi.codeStyle.JavaCodeStyleManager; -import com.intellij.psi.impl.source.PsiExtensibleClass; import com.intellij.refactoring.ui.MemberSelectionPanel; import com.intellij.refactoring.util.classMembers.MemberInfo; import com.intellij.util.ArrayUtil; @@ -38,6 +37,7 @@ import org.jetbrains.annotations.TestOnly; import javax.swing.*; import java.util.Collections; import java.util.List; +import java.util.Objects; import java.util.Set; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -116,15 +116,15 @@ public class ChangeSuperClassFix implements LocalQuickFix, HighPriorityAction { private static void changeSuperClass(@NotNull final PsiClass aClass, @NotNull final PsiClass oldSuperClass, @NotNull final PsiClass newSuperClass) { - List ownMethods = ((PsiExtensibleClass)aClass).getOwnMethods(); + PsiMethod[] ownMethods = aClass.getMethods(); // first is own method, second is parent List>> oldOverridenMethods = - ownMethods.stream().map(m -> { + Stream.of(ownMethods).map(m -> { if (m.isConstructor()) return null; PsiMethod[] supers = m.findSuperMethods(oldSuperClass); if (supers.length == 0) return null; return Pair.create(m, ContainerUtil.set(supers)); - }).collect(Collectors.toList()); + }).filter(Objects::nonNull).collect(Collectors.toList()); JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(aClass.getProject()); PsiElementFactory factory = psiFacade.getElementFactory(); @@ -160,21 +160,28 @@ public class ChangeSuperClassFix implements LocalQuickFix, HighPriorityAction { JavaCodeStyleManager.getInstance(aClass.getProject()).shortenClassReferences(ref); }); - if (ownMethods.isEmpty()) { - // should not override methods from a new super class - return; - } - Stream memberInfos = oldOverridenMethods.stream().filter(m -> { + List memberInfos = oldOverridenMethods.stream().filter(m -> { Set newSupers = ContainerUtil.set(m.getFirst().findSuperMethods(newSuperClass)); return !newSupers.equals(m.getSecond()); - }).map(m -> m.getFirst()); + }).map(m -> m.getFirst()) + .map(m -> { + MemberInfo info = new MemberInfo(m); + info.setChecked(true); + return info; + }).collect(Collectors.toList()); + + if (memberInfos.isEmpty()) { + return; + } List toDelete = getOverridenMethodsToDelete(memberInfos, newSuperClass.getName(), aClass.getProject()); - WriteAction.run(() -> { - for (PsiMethod method : toDelete) { - method.delete(); - } - }); + if (!toDelete.isEmpty()) { + WriteAction.run(() -> { + for (PsiMethod method : toDelete) { + method.delete(); + } + }); + } } @NotNull @@ -183,26 +190,23 @@ public class ChangeSuperClassFix implements LocalQuickFix, HighPriorityAction { } @NotNull - private static List getOverridenMethodsToDelete(Stream candidates, + private static List getOverridenMethodsToDelete(List candidates, String newClassName, Project project) { MemberSelectionPanel panel = new MemberSelectionPanel("Choose members to delete since they are already defined in " + newClassName + "", - candidates.map(m -> { - MemberInfo info = new MemberInfo(m); - info.setChecked(true); - return info; - }).collect(Collectors.toList()), null); + candidates, + null); DialogWrapper dlg = new DialogWrapper(project, false) { { setOKButtonText("Remove"); setTitle("Choose Members"); + init(); } @NotNull @Override protected JComponent createCenterPanel() { - return panel; } };