Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import com.intellij.java.language.psi.PsiClass;
import com.intellij.java.language.psi.PsiMethod;
import com.intellij.java.language.psi.PsiModifier;
import com.intellij.java.language.psi.search.searches.DeepestSuperMethodsSearch;
import consulo.annotation.access.RequiredReadAction;
import consulo.codeEditor.Editor;
Expand All @@ -28,6 +27,7 @@
import consulo.language.psi.resolve.PsiElementProcessor;
import consulo.language.psi.util.SymbolPresentationUtil;
import consulo.localize.LocalizeValue;
import consulo.ui.annotation.RequiredUIAccess;
import consulo.ui.ex.awt.DialogWrapper;
import consulo.ui.ex.awt.JBList;
import consulo.ui.ex.awt.Messages;
Expand All @@ -43,152 +43,162 @@
import java.util.Set;

public class SuperMethodWarningUtil {
private SuperMethodWarningUtil() {
}

@Nonnull
@RequiredReadAction
public static PsiMethod[] checkSuperMethods(PsiMethod method, String actionString) {
return checkSuperMethods(method, actionString, null);
}

@Nonnull
@RequiredReadAction
public static PsiMethod[] checkSuperMethods(PsiMethod method, String actionString, Collection<PsiElement> ignore) {
PsiClass aClass = method.getContainingClass();
if (aClass == null) return new PsiMethod[]{method};

Collection<PsiMethod> superMethods = DeepestSuperMethodsSearch.search(method).findAll();
if (ignore != null) {
superMethods.removeAll(ignore);
private SuperMethodWarningUtil() {
}

if (superMethods.isEmpty()) return new PsiMethod[]{method};

Set<String> superClasses = new HashSet<>();
boolean superAbstract = false;
boolean parentInterface = false;
for (PsiMethod superMethod : superMethods) {
PsiClass containingClass = superMethod.getContainingClass();
superClasses.add(containingClass.getQualifiedName());
boolean isInterface = containingClass.isInterface();
superAbstract |= isInterface || superMethod.hasModifierProperty(PsiModifier.ABSTRACT);
parentInterface |= isInterface;
@Nonnull
@RequiredUIAccess
public static PsiMethod[] checkSuperMethods(PsiMethod method, String actionString) {
return checkSuperMethods(method, actionString, null);
}

SuperMethodWarningDialog dialog = new SuperMethodWarningDialog(
method.getProject(),
DescriptiveNameUtil.getDescriptiveName(method),
actionString,
superAbstract,
parentInterface,
aClass.isInterface(),
ArrayUtil.toStringArray(superClasses)
);
dialog.show();

if (dialog.getExitCode() == DialogWrapper.OK_EXIT_CODE) {
return superMethods.toArray(new PsiMethod[superMethods.size()]);
}
if (dialog.getExitCode() == SuperMethodWarningDialog.NO_EXIT_CODE) {
return new PsiMethod[]{method};
}

return PsiMethod.EMPTY_ARRAY;
}


@RequiredReadAction
public static PsiMethod checkSuperMethod(PsiMethod method, String actionString) {
PsiClass aClass = method.getContainingClass();
if (aClass == null) return method;

PsiMethod superMethod = method.findDeepestSuperMethod();
if (superMethod == null) return method;

if (method.getApplication().isUnitTestMode()) return superMethod;

PsiClass containingClass = superMethod.getContainingClass();

SuperMethodWarningDialog dialog = new SuperMethodWarningDialog(
method.getProject(),
DescriptiveNameUtil.getDescriptiveName(method),
actionString,
containingClass.isInterface() || superMethod.hasModifierProperty(PsiModifier.ABSTRACT),
containingClass.isInterface(),
aClass.isInterface(),
containingClass.getQualifiedName()
);
dialog.show();

if (dialog.getExitCode() == DialogWrapper.OK_EXIT_CODE) return superMethod;
if (dialog.getExitCode() == SuperMethodWarningDialog.NO_EXIT_CODE) return method;

return null;
}

@RequiredReadAction
public static void checkSuperMethod(
PsiMethod method,
String actionString,
PsiElementProcessor<PsiMethod> processor,
Editor editor
) {
PsiClass aClass = method.getContainingClass();
if (aClass == null) {
processor.execute(method);
return;
@Nonnull
@RequiredUIAccess
public static PsiMethod[] checkSuperMethods(PsiMethod method, String actionString, Collection<PsiElement> ignore) {
PsiClass aClass = method.getContainingClass();
if (aClass == null) {
return new PsiMethod[]{method};
}

Collection<PsiMethod> superMethods = DeepestSuperMethodsSearch.search(method).findAll();
if (ignore != null) {
superMethods.removeAll(ignore);
}

if (superMethods.isEmpty()) {
return new PsiMethod[]{method};
}

Set<String> superClasses = new HashSet<>();
boolean superAbstract = false;
boolean parentInterface = false;
for (PsiMethod superMethod : superMethods) {
PsiClass containingClass = superMethod.getContainingClass();
superClasses.add(containingClass.getQualifiedName());
boolean isInterface = containingClass.isInterface();
superAbstract |= isInterface || superMethod.isAbstract();
parentInterface |= isInterface;
}

SuperMethodWarningDialog dialog = new SuperMethodWarningDialog(
method.getProject(),
DescriptiveNameUtil.getDescriptiveName(method),
actionString,
superAbstract,
parentInterface,
aClass.isInterface(),
ArrayUtil.toStringArray(superClasses)
);
dialog.show();

if (dialog.getExitCode() == DialogWrapper.OK_EXIT_CODE) {
return superMethods.toArray(new PsiMethod[superMethods.size()]);
}
if (dialog.getExitCode() == SuperMethodWarningDialog.NO_EXIT_CODE) {
return new PsiMethod[]{method};
}

return PsiMethod.EMPTY_ARRAY;
}

PsiMethod superMethod = method.findDeepestSuperMethod();
if (superMethod == null) {
processor.execute(method);
return;
@RequiredUIAccess
public static PsiMethod checkSuperMethod(PsiMethod method, String actionString) {
PsiClass aClass = method.getContainingClass();
if (aClass == null) {
return method;
}

PsiMethod superMethod = method.findDeepestSuperMethod();
if (superMethod == null) {
return method;
}

if (method.getApplication().isUnitTestMode()) {
return superMethod;
}

PsiClass containingClass = superMethod.getContainingClass();

SuperMethodWarningDialog dialog = new SuperMethodWarningDialog(
method.getProject(),
DescriptiveNameUtil.getDescriptiveName(method),
actionString,
containingClass.isInterface() || superMethod.isAbstract(),
containingClass.isInterface(),
aClass.isInterface(),
containingClass.getQualifiedName()
);
dialog.show();

if (dialog.getExitCode() == DialogWrapper.OK_EXIT_CODE) {
return superMethod;
}
if (dialog.getExitCode() == SuperMethodWarningDialog.NO_EXIT_CODE) {
return method;
}

return null;
}

PsiClass containingClass = superMethod.getContainingClass();
if (containingClass == null) {
processor.execute(method);
return;
@RequiredReadAction
public static void checkSuperMethod(PsiMethod method, String actionString, PsiElementProcessor<PsiMethod> processor, Editor editor) {
PsiClass aClass = method.getContainingClass();
if (aClass == null) {
processor.execute(method);
return;
}

PsiMethod superMethod = method.findDeepestSuperMethod();
if (superMethod == null) {
processor.execute(method);
return;
}

PsiClass containingClass = superMethod.getContainingClass();
if (containingClass == null) {
processor.execute(method);
return;
}

if (method.getApplication().isUnitTestMode()) {
processor.execute(superMethod);
return;
}

PsiMethod[] methods = new PsiMethod[]{superMethod, method};
String renameBase = actionString + " base method";
String renameCurrent = actionString + " only current method";
JBList<String> list = new JBList<>(renameBase, renameCurrent);
JBPopup popup = ((AWTPopupFactory) JBPopupFactory.getInstance()).createListPopupBuilder(list)
.setItemChoosenCallback(() -> {
Object value = list.getSelectedValue();
if (value instanceof String) {
processor.execute(methods[value.equals(renameBase) ? 0 : 1]);
}
})
.setMovable(false)
.setTitle(
method.getName() + (containingClass.isInterface() && !aClass.isInterface() ? " implements" : " overrides") +
" method of " + SymbolPresentationUtil.getSymbolPresentableText(containingClass)
)
.setResizable(false)
.setRequestFocus(true)
.createPopup();

EditorPopupHelper.getInstance().showPopupInBestPositionFor(editor, popup);
}

if (method.getApplication().isUnitTestMode()) {
processor.execute(superMethod);
return;
@RequiredUIAccess
public static int askWhetherShouldAnnotateBaseMethod(@Nonnull PsiMethod method, @Nonnull PsiMethod superMethod) {
LocalizeValue implement = !method.isAbstract() && superMethod.isAbstract()
? InspectionLocalize.inspectionAnnotateQuickfixImplements()
: InspectionLocalize.inspectionAnnotateQuickfixOverrides();
LocalizeValue message = InspectionLocalize.inspectionAnnotateQuickfixOverriddenMethodMessages(
DescriptiveNameUtil.getDescriptiveName(method),
implement,
DescriptiveNameUtil.getDescriptiveName(superMethod)
);
LocalizeValue title = InspectionLocalize.inspectionAnnotateQuickfixOverriddenMethodWarning();
return Messages.showYesNoCancelDialog(method.getProject(), message.get(), title.get(), UIUtil.getQuestionIcon());
}

PsiMethod[] methods = new PsiMethod[]{superMethod, method};
String renameBase = actionString + " base method";
String renameCurrent = actionString + " only current method";
JBList<String> list = new JBList<>(renameBase, renameCurrent);
JBPopup popup = ((AWTPopupFactory) JBPopupFactory.getInstance()).createListPopupBuilder(list)
.setItemChoosenCallback(() -> {
Object value = list.getSelectedValue();
if (value instanceof String) {
processor.execute(methods[value.equals(renameBase) ? 0 : 1]);
}
}).setMovable(false)
.setTitle(
method.getName() + (containingClass.isInterface() && !aClass.isInterface() ? " implements" : " overrides") + " method of " +
SymbolPresentationUtil.getSymbolPresentableText(containingClass)
)
.setResizable(false)
.setRequestFocus(true).createPopup();

EditorPopupHelper.getInstance().showPopupInBestPositionFor(editor, popup);
}

@RequiredReadAction
public static int askWhetherShouldAnnotateBaseMethod(@Nonnull PsiMethod method, @Nonnull PsiMethod superMethod) {
LocalizeValue implement = !method.hasModifierProperty(PsiModifier.ABSTRACT) && superMethod.hasModifierProperty(PsiModifier.ABSTRACT)
? InspectionLocalize.inspectionAnnotateQuickfixImplements()
: InspectionLocalize.inspectionAnnotateQuickfixOverrides();
LocalizeValue message = InspectionLocalize.inspectionAnnotateQuickfixOverriddenMethodMessages(
DescriptiveNameUtil.getDescriptiveName(method),
implement,
DescriptiveNameUtil.getDescriptiveName(superMethod)
);
LocalizeValue title = InspectionLocalize.inspectionAnnotateQuickfixOverriddenMethodWarning();
return Messages.showYesNoCancelDialog(method.getProject(), message.get(), title.get(), UIUtil.getQuestionIcon());
}
}
Loading
Loading