Search in sources :

Example 1 with ExtractSuperclassHandler

use of com.intellij.refactoring.extractSuperclass.ExtractSuperclassHandler 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)

Aggregations

RunRefactoringAction (com.intellij.codeInsight.intention.impl.RunRefactoringAction)1 ExtractInterfaceHandler (com.intellij.refactoring.extractInterface.ExtractInterfaceHandler)1 ExtractSuperclassHandler (com.intellij.refactoring.extractSuperclass.ExtractSuperclassHandler)1 JavaPullUpHandler (com.intellij.refactoring.memberPullUp.JavaPullUpHandler)1 LinkedHashSet (java.util.LinkedHashSet)1