Search in sources :

Example 1 with ExtractInterfaceHandler

use of com.intellij.refactoring.extractInterface.ExtractInterfaceHandler in project intellij-community by JetBrains.

the class PullAsAbstractUpFix method registerQuickFix.

public static void registerQuickFix(@NotNull PsiMethod methodWithOverrides, @NotNull QuickFixActionRegistrar registrar) {
    PsiClass containingClass = methodWithOverrides.getContainingClass();
    if (containingClass == null)
        return;
    final PsiManager manager = containingClass.getManager();
    boolean canBePulledUp = true;
    String name = "Pull method \'" + methodWithOverrides.getName() + "\' up";
    if (containingClass instanceof PsiAnonymousClass) {
        final PsiClassType baseClassType = ((PsiAnonymousClass) containingClass).getBaseClassType();
        final PsiClass baseClass = baseClassType.resolve();
        if (baseClass == null)
            return;
        if (!manager.isInProject(baseClass))
            return;
        if (!baseClass.hasModifierProperty(PsiModifier.ABSTRACT)) {
            name = "Pull method \'" + methodWithOverrides.getName() + "\' up and make it abstract";
        }
    } else {
        final LinkedHashSet<PsiClass> classesToPullUp = new LinkedHashSet<>();
        collectClassesToPullUp(manager, classesToPullUp, containingClass.getExtendsListTypes());
        collectClassesToPullUp(manager, classesToPullUp, containingClass.getImplementsListTypes());
        if (classesToPullUp.isEmpty()) {
            name = "Extract method \'" + methodWithOverrides.getName() + "\' to new interface";
            canBePulledUp = false;
        } else if (classesToPullUp.size() == 1) {
            final PsiClass baseClass = classesToPullUp.iterator().next();
            name = "Pull method \'" + methodWithOverrides.getName() + "\' to \'" + baseClass.getName() + "\'";
            if (!baseClass.hasModifierProperty(PsiModifier.ABSTRACT)) {
                name += " and make it abstract";
            }
        }
        registrar.register(new RunRefactoringAction(new ExtractInterfaceHandler(), "Extract interface"));
        registrar.register(new RunRefactoringAction(new ExtractSuperclassHandler(), "Extract superclass"));
    }
    if (canBePulledUp) {
        registrar.register(new RunRefactoringAction(new JavaPullUpHandler(), "Pull members up"));
    }
    registrar.register(new PullAsAbstractUpFix(methodWithOverrides, name));
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ExtractInterfaceHandler(com.intellij.refactoring.extractInterface.ExtractInterfaceHandler) RunRefactoringAction(com.intellij.codeInsight.intention.impl.RunRefactoringAction) JavaPullUpHandler(com.intellij.refactoring.memberPullUp.JavaPullUpHandler) ExtractSuperclassHandler(com.intellij.refactoring.extractSuperclass.ExtractSuperclassHandler)

Example 2 with ExtractInterfaceHandler

use of com.intellij.refactoring.extractInterface.ExtractInterfaceHandler in project intellij-community by JetBrains.

the class PullAsAbstractUpFix method invoke.

@Override
public void invoke(@NotNull Project project, @NotNull PsiFile file, @Nullable("is null when called from inspection") Editor editor, @NotNull PsiElement startElement, @NotNull PsiElement endElement) {
    final PsiMethod method = (PsiMethod) startElement;
    if (!FileModificationService.getInstance().prepareFileForWrite(method.getContainingFile()))
        return;
    final PsiClass containingClass = method.getContainingClass();
    LOG.assertTrue(containingClass != null);
    PsiManager manager = containingClass.getManager();
    if (containingClass instanceof PsiAnonymousClass) {
        final PsiClassType baseClassType = ((PsiAnonymousClass) containingClass).getBaseClassType();
        final PsiClass baseClass = baseClassType.resolve();
        if (baseClass != null && manager.isInProject(baseClass)) {
            pullUp(method, containingClass, baseClass);
        }
    } else {
        final LinkedHashSet<PsiClass> classesToPullUp = new LinkedHashSet<>();
        collectClassesToPullUp(manager, classesToPullUp, containingClass.getExtendsListTypes());
        collectClassesToPullUp(manager, classesToPullUp, containingClass.getImplementsListTypes());
        if (classesToPullUp.isEmpty()) {
            //check visibility
            new ExtractInterfaceHandler().invoke(project, new PsiElement[] { containingClass }, null);
        } else if (classesToPullUp.size() == 1) {
            pullUp(method, containingClass, classesToPullUp.iterator().next());
        } else if (editor != null) {
            NavigationUtil.getPsiElementPopup(classesToPullUp.toArray(new PsiClass[classesToPullUp.size()]), new PsiClassListCellRenderer(), "Choose super class", new PsiElementProcessor<PsiClass>() {

                @Override
                public boolean execute(@NotNull PsiClass aClass) {
                    pullUp(method, containingClass, aClass);
                    return false;
                }
            }, classesToPullUp.iterator().next()).showInBestPositionFor(editor);
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ExtractInterfaceHandler(com.intellij.refactoring.extractInterface.ExtractInterfaceHandler) PsiClassListCellRenderer(com.intellij.ide.util.PsiClassListCellRenderer) NotNull(org.jetbrains.annotations.NotNull) PsiElementProcessor(com.intellij.psi.search.PsiElementProcessor)

Aggregations

ExtractInterfaceHandler (com.intellij.refactoring.extractInterface.ExtractInterfaceHandler)2 LinkedHashSet (java.util.LinkedHashSet)2 RunRefactoringAction (com.intellij.codeInsight.intention.impl.RunRefactoringAction)1 PsiClassListCellRenderer (com.intellij.ide.util.PsiClassListCellRenderer)1 PsiElementProcessor (com.intellij.psi.search.PsiElementProcessor)1 ExtractSuperclassHandler (com.intellij.refactoring.extractSuperclass.ExtractSuperclassHandler)1 JavaPullUpHandler (com.intellij.refactoring.memberPullUp.JavaPullUpHandler)1 NotNull (org.jetbrains.annotations.NotNull)1