Search in sources :

Example 21 with MemberInfo

use of com.intellij.refactoring.util.classMembers.MemberInfo in project intellij-community by JetBrains.

the class JavaPushDownHandler method invoke.

@Override
public void invoke(@NotNull final Project project, @NotNull PsiElement[] elements, DataContext dataContext) {
    PsiClass aClass = PsiTreeUtil.getParentOfType(PsiTreeUtil.findCommonParent(elements), PsiClass.class, false);
    if (aClass == null)
        return;
    String qualifiedName = aClass.getQualifiedName();
    if (qualifiedName == null)
        return;
    final Editor editor = dataContext != null ? CommonDataKeys.EDITOR.getData(dataContext) : null;
    if (aClass.hasModifierProperty(PsiModifier.FINAL)) {
        CommonRefactoringUtil.showErrorHint(project, editor, RefactoringBundle.message("refactoring.cannot.be.performed") + ": Class " + aClass.getName() + " is final", REFACTORING_NAME, HelpID.MEMBERS_PUSH_DOWN);
        return;
    }
    if (!CommonRefactoringUtil.checkReadOnlyStatus(project, aClass))
        return;
    MemberInfoStorage memberInfoStorage = new MemberInfoStorage(aClass, element -> !(element instanceof PsiEnumConstant));
    List<MemberInfo> members = memberInfoStorage.getClassMemberInfos(aClass);
    for (MemberInfoBase<PsiMember> member : members) {
        for (PsiElement element : elements) {
            if (PsiTreeUtil.isAncestor(member.getMember(), element, false)) {
                member.setChecked(true);
                break;
            }
        }
    }
    PushDownDialog dialog = new PushDownDialog(project, members.toArray(new MemberInfo[members.size()]), aClass);
    dialog.show();
}
Also used : MemberInfo(com.intellij.refactoring.util.classMembers.MemberInfo) MemberInfoStorage(com.intellij.refactoring.util.classMembers.MemberInfoStorage) Editor(com.intellij.openapi.editor.Editor)

Example 22 with MemberInfo

use of com.intellij.refactoring.util.classMembers.MemberInfo in project intellij-community by JetBrains.

the class JavaPullUpHandler method checkConflicts.

@Override
public boolean checkConflicts(final PullUpDialog dialog) {
    final List<MemberInfo> infos = dialog.getSelectedMemberInfos();
    final MemberInfo[] memberInfos = infos.toArray(new MemberInfo[infos.size()]);
    final PsiClass superClass = dialog.getSuperClass();
    if (superClass == null || !checkWritable(superClass, memberInfos))
        return false;
    final MultiMap<PsiElement, String> conflicts = new MultiMap<>();
    if (!ProgressManager.getInstance().runProcessWithProgressSynchronously(() -> ApplicationManager.getApplication().runReadAction(() -> {
        final PsiDirectory targetDirectory = superClass.getContainingFile().getContainingDirectory();
        final PsiPackage targetPackage = targetDirectory != null ? JavaDirectoryService.getInstance().getPackage(targetDirectory) : null;
        if (targetDirectory != null && targetPackage != null) {
            conflicts.putAllValues(PullUpConflictsUtil.checkConflicts(memberInfos, mySubclass, superClass, targetPackage, targetDirectory, dialog.getContainmentVerifier()));
        }
    }), RefactoringBundle.message("detecting.possible.conflicts"), true, myProject))
        return false;
    if (!conflicts.isEmpty()) {
        ConflictsDialog conflictsDialog = new ConflictsDialog(myProject, conflicts);
        boolean ok = conflictsDialog.showAndGet();
        if (!ok && conflictsDialog.isShowConflicts())
            dialog.close(DialogWrapper.CANCEL_EXIT_CODE);
        return ok;
    }
    return true;
}
Also used : MultiMap(com.intellij.util.containers.MultiMap) MemberInfo(com.intellij.refactoring.util.classMembers.MemberInfo) ConflictsDialog(com.intellij.refactoring.ui.ConflictsDialog)

Example 23 with MemberInfo

use of com.intellij.refactoring.util.classMembers.MemberInfo in project intellij-community by JetBrains.

the class PullUpDialog method updateAbstractState.

private void updateAbstractState() {
    boolean hasJavadoc = false;
    for (MemberInfo info : myMemberInfos) {
        final PsiMember member = info.getMember();
        final boolean abstractWhenDisabled = myMemberInfoModel.isAbstractWhenDisabled(info);
        info.setToAbstract(abstractWhenDisabled);
        if (myMemberInfoModel.isAbstractEnabled(info) || abstractWhenDisabled) {
            if (!hasJavadoc && member instanceof PsiDocCommentOwner && ((PsiDocCommentOwner) member).getDocComment() != null) {
                hasJavadoc = true;
            }
        }
    }
    UIUtil.setEnabled(myJavaDocPanel, hasJavadoc, true);
}
Also used : MemberInfo(com.intellij.refactoring.util.classMembers.MemberInfo)

Example 24 with MemberInfo

use of com.intellij.refactoring.util.classMembers.MemberInfo in project intellij-community by JetBrains.

the class PullUpDialog method doAction.

protected void doAction() {
    if (!myCallback.checkConflicts(this))
        return;
    JavaRefactoringSettings.getInstance().PULL_UP_MEMBERS_JAVADOC = myJavaDocPanel.getPolicy();
    final PsiClass superClass = getSuperClass();
    String name = superClass.getQualifiedName();
    if (name != null) {
        StatisticsManager.getInstance().incUseCount(new StatisticsInfo(PULL_UP_STATISTICS_KEY + myClass.getQualifiedName(), name));
    }
    List<MemberInfo> infos = getSelectedMemberInfos();
    invokeRefactoring(new PullUpProcessor(myClass, superClass, infos.toArray(new MemberInfo[infos.size()]), new DocCommentPolicy(getJavaDocPolicy())));
    close(OK_EXIT_CODE);
}
Also used : StatisticsInfo(com.intellij.psi.statistics.StatisticsInfo) MemberInfo(com.intellij.refactoring.util.classMembers.MemberInfo) DocCommentPolicy(com.intellij.refactoring.util.DocCommentPolicy)

Example 25 with MemberInfo

use of com.intellij.refactoring.util.classMembers.MemberInfo in project intellij-community by JetBrains.

the class PullUpMultifileTest method doTest.

private void doTest(final String... conflicts) throws Exception {
    final MultiMap<PsiElement, String> conflictsMap = new MultiMap<>();
    doTest((rootDir, rootAfter) -> {
        final PsiClass srcClass = myJavaFacade.findClass("a.A", GlobalSearchScope.allScope(myProject));
        assertTrue("Source class not found", srcClass != null);
        final PsiClass targetClass = myJavaFacade.findClass("b.B", GlobalSearchScope.allScope(myProject));
        assertTrue("Target class not found", targetClass != null);
        final PsiMethod[] methods = srcClass.getMethods();
        assertTrue("No methods found", methods.length > 0);
        final MemberInfo[] membersToMove = new MemberInfo[1];
        final MemberInfo memberInfo = new MemberInfo(methods[0]);
        memberInfo.setChecked(true);
        membersToMove[0] = memberInfo;
        final PsiDirectory targetDirectory = targetClass.getContainingFile().getContainingDirectory();
        final PsiPackage targetPackage = targetDirectory != null ? JavaDirectoryService.getInstance().getPackage(targetDirectory) : null;
        conflictsMap.putAllValues(PullUpConflictsUtil.checkConflicts(membersToMove, srcClass, targetClass, targetPackage, targetDirectory, psiMethod -> PullUpProcessor.checkedInterfacesContain(Arrays.asList(membersToMove), psiMethod)));
        new PullUpProcessor(srcClass, targetClass, membersToMove, new DocCommentPolicy(DocCommentPolicy.ASIS)).run();
    });
    if (conflicts.length != 0 && conflictsMap.isEmpty()) {
        fail("Conflict was not detected");
    }
    final HashSet<String> values = new HashSet<>(conflictsMap.values());
    final HashSet<String> expected = new HashSet<>(Arrays.asList(conflicts));
    assertEquals(expected.size(), values.size());
    for (String value : values) {
        if (!expected.contains(value)) {
            fail("Conflict: " + value + " is unexpectedly reported");
        }
    }
}
Also used : PullUpConflictsUtil(com.intellij.refactoring.memberPullUp.PullUpConflictsUtil) HashSet(java.util.HashSet) MemberInfo(com.intellij.refactoring.util.classMembers.MemberInfo) Arrays(java.util.Arrays) JavaTestUtil(com.intellij.JavaTestUtil) DocCommentPolicy(com.intellij.refactoring.util.DocCommentPolicy) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) com.intellij.psi(com.intellij.psi) PullUpProcessor(com.intellij.refactoring.memberPullUp.PullUpProcessor) NotNull(org.jetbrains.annotations.NotNull) MultiMap(com.intellij.util.containers.MultiMap) MultiMap(com.intellij.util.containers.MultiMap) MemberInfo(com.intellij.refactoring.util.classMembers.MemberInfo) PullUpProcessor(com.intellij.refactoring.memberPullUp.PullUpProcessor) DocCommentPolicy(com.intellij.refactoring.util.DocCommentPolicy) HashSet(java.util.HashSet)

Aggregations

MemberInfo (com.intellij.refactoring.util.classMembers.MemberInfo)45 DocCommentPolicy (com.intellij.refactoring.util.DocCommentPolicy)12 PsiMethod (com.intellij.psi.PsiMethod)6 MultiMap (com.intellij.util.containers.MultiMap)6 ArrayList (java.util.ArrayList)6 Project (com.intellij.openapi.project.Project)5 com.intellij.psi (com.intellij.psi)5 MemberSelectionPanel (com.intellij.refactoring.ui.MemberSelectionPanel)5 IncorrectOperationException (com.intellij.util.IncorrectOperationException)5 HashSet (java.util.HashSet)5 Editor (com.intellij.openapi.editor.Editor)4 PsiClass (com.intellij.psi.PsiClass)4 PullUpProcessor (com.intellij.refactoring.memberPullUp.PullUpProcessor)4 DialogWrapper (com.intellij.openapi.ui.DialogWrapper)3 PsiMember (com.intellij.psi.PsiMember)3 MemberSelectionTable (com.intellij.refactoring.ui.MemberSelectionTable)3 MemberInfoStorage (com.intellij.refactoring.util.classMembers.MemberInfoStorage)3 UsageInfo (com.intellij.usageView.UsageInfo)3 NotNull (org.jetbrains.annotations.NotNull)3 Nullable (org.jetbrains.annotations.Nullable)3