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));
}
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);
}
}
}
Aggregations