Search in sources :

Example 26 with MemberInfo

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

the class PushDownTest method doTestImplements.

private void doTestImplements(boolean toAbstract, boolean failure) {
    configureByFile(BASE_PATH + getTestName(false) + ".java");
    PsiClass currentClass = JavaPsiFacade.getInstance(getProject()).findClass("Test", GlobalSearchScope.projectScope(getProject()));
    MemberInfoStorage memberInfoStorage = new MemberInfoStorage(currentClass, element -> true);
    List<MemberInfo> members = memberInfoStorage.getClassMemberInfos(currentClass);
    for (MemberInfo member : members) {
        member.setChecked(true);
        if (toAbstract) {
            member.setToAbstract(toAbstract);
        }
    }
    new PushDownProcessor<MemberInfo, PsiMember, PsiClass>(currentClass, members, new DocCommentPolicy(DocCommentPolicy.ASIS)) {

        @Override
        protected boolean showConflicts(@NotNull MultiMap<PsiElement, String> conflicts, UsageInfo[] usages) {
            if (failure == conflicts.isEmpty()) {
                fail(failure ? "Conflict was not detected" : "False conflict was detected");
            }
            return true;
        }
    }.run();
    checkResultByFile(BASE_PATH + getTestName(false) + "_after.java");
}
Also used : MemberInfoStorage(com.intellij.refactoring.util.classMembers.MemberInfoStorage) MemberInfo(com.intellij.refactoring.util.classMembers.MemberInfo) DocCommentPolicy(com.intellij.refactoring.util.DocCommentPolicy) UsageInfo(com.intellij.usageView.UsageInfo)

Example 27 with MemberInfo

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

the class PushDownTest method doTest.

private void doTest(final boolean failure) {
    configureByFile(BASE_PATH + getTestName(false) + ".java");
    final PsiElement targetElement = TargetElementUtil.findTargetElement(getEditor(), TargetElementUtil.ELEMENT_NAME_ACCEPTED);
    assertTrue("<caret> is not on member name", targetElement instanceof PsiMember);
    final PsiMember psiMember = (PsiMember) targetElement;
    final PsiClass currentClass = psiMember.getContainingClass();
    assert currentClass != null;
    final List<MemberInfo> membersToMove = new ArrayList<>();
    final PsiField fieldByName = currentClass.findFieldByName("fieldToMove", false);
    if (fieldByName != null) {
        final MemberInfo memberInfo = new MemberInfo(fieldByName);
        memberInfo.setChecked(true);
        membersToMove.add(memberInfo);
    }
    final PsiClass classByName = currentClass.findInnerClassByName("ClassToMove", false);
    if (classByName != null) {
        final MemberInfo memberInfo = new MemberInfo(classByName);
        memberInfo.setChecked(true);
        membersToMove.add(memberInfo);
    }
    final MemberInfo memberInfo = new MemberInfo(psiMember);
    memberInfo.setChecked(true);
    membersToMove.add(memberInfo);
    new PushDownProcessor<MemberInfo, PsiMember, PsiClass>(currentClass, membersToMove, new DocCommentPolicy(DocCommentPolicy.ASIS)) {

        @Override
        protected boolean showConflicts(@NotNull MultiMap<PsiElement, String> conflicts, UsageInfo[] usages) {
            if (failure == conflicts.isEmpty()) {
                fail(failure ? "Conflict was not detected" : "False conflict was detected");
            }
            return true;
        }
    }.run();
    checkResultByFile(BASE_PATH + getTestName(false) + "_after.java");
}
Also used : ArrayList(java.util.ArrayList) MemberInfo(com.intellij.refactoring.util.classMembers.MemberInfo) DocCommentPolicy(com.intellij.refactoring.util.DocCommentPolicy) UsageInfo(com.intellij.usageView.UsageInfo)

Example 28 with MemberInfo

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

the class RemoveMiddleManTest method doTest.

private void doTest(final String conflict) throws Exception {
    doTest((rootDir, rootAfter) -> {
        PsiClass aClass = myJavaFacade.findClass("Test", GlobalSearchScope.allScope(getProject()));
        if (aClass == null)
            aClass = myJavaFacade.findClass("p.Test", GlobalSearchScope.allScope(getProject()));
        assertNotNull("Class Test not found", aClass);
        final PsiField field = aClass.findFieldByName("myField", false);
        final Set<PsiMethod> methods = DelegationUtils.getDelegatingMethodsForField(field);
        List<MemberInfo> infos = new ArrayList<>();
        for (PsiMethod method : methods) {
            final MemberInfo info = new MemberInfo(method);
            info.setChecked(true);
            info.setToAbstract(true);
            infos.add(info);
        }
        try {
            RemoveMiddlemanProcessor processor = new RemoveMiddlemanProcessor(field, infos);
            processor.run();
            LocalFileSystem.getInstance().refresh(false);
            FileDocumentManager.getInstance().saveAllDocuments();
            if (conflict != null)
                fail("Conflict expected");
        } catch (BaseRefactoringProcessor.ConflictsInTestsException e) {
            if (conflict == null)
                throw e;
            assertEquals(conflict, e.getMessage());
        }
    });
}
Also used : RemoveMiddlemanProcessor(com.intellij.refactoring.removemiddleman.RemoveMiddlemanProcessor) MemberInfo(com.intellij.refactoring.util.classMembers.MemberInfo) PsiMethod(com.intellij.psi.PsiMethod) PsiField(com.intellij.psi.PsiField) PsiClass(com.intellij.psi.PsiClass) ArrayList(java.util.ArrayList)

Example 29 with MemberInfo

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

the class RemoveMiddlemanDialog method createCenterPanel.

protected JComponent createCenterPanel() {
    final JPanel panel = new JPanel(new BorderLayout());
    panel.setBorder(BorderFactory.createEmptyBorder(10, 0, 5, 0));
    final MemberSelectionPanel selectionPanel = new MemberSelectionPanel("&Methods to inline", delegateMethods, "Delete");
    final MemberSelectionTable table = selectionPanel.getTable();
    table.setMemberInfoModel(new DelegatingMemberInfoModel<PsiMember, MemberInfo>(table.getMemberInfoModel()) {

        @Override
        public int checkForProblems(@NotNull final MemberInfo member) {
            return hasSuperMethods(member) ? ERROR : OK;
        }

        @Override
        public String getTooltipText(final MemberInfo member) {
            if (hasSuperMethods(member))
                return "Deletion will break type hierarchy";
            return super.getTooltipText(member);
        }

        private boolean hasSuperMethods(final MemberInfo member) {
            if (member.isChecked() && member.isToAbstract()) {
                final PsiMember psiMember = member.getMember();
                if (psiMember instanceof PsiMethod && ((PsiMethod) psiMember).findDeepestSuperMethods().length > 0) {
                    return true;
                }
            }
            return false;
        }
    });
    panel.add(selectionPanel, BorderLayout.CENTER);
    return panel;
}
Also used : MemberSelectionTable(com.intellij.refactoring.ui.MemberSelectionTable) MemberInfo(com.intellij.refactoring.util.classMembers.MemberInfo) PsiMethod(com.intellij.psi.PsiMethod) MemberSelectionPanel(com.intellij.refactoring.ui.MemberSelectionPanel) PsiMember(com.intellij.psi.PsiMember)

Example 30 with MemberInfo

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

the class RemoveMiddlemanHandler method invoke.

private static void invoke(final PsiField field, Editor editor) {
    final Project project = field.getProject();
    final Set<PsiMethod> delegating = DelegationUtils.getDelegatingMethodsForField(field);
    if (delegating.isEmpty()) {
        final String message = RefactorJBundle.message("cannot.perform.the.refactoring") + RefactorJBundle.message("field.selected.is.not.used.as.a.delegate");
        CommonRefactoringUtil.showErrorHint(project, editor, message, REFACTORING_NAME, getHelpID());
        return;
    }
    MemberInfo[] infos = new MemberInfo[delegating.size()];
    int i = 0;
    for (PsiMethod method : delegating) {
        final MemberInfo memberInfo = new MemberInfo(method);
        memberInfo.setChecked(true);
        memberInfo.setToAbstract(method.findDeepestSuperMethods().length == 0);
        infos[i++] = memberInfo;
    }
    new RemoveMiddlemanDialog(field, infos).show();
}
Also used : Project(com.intellij.openapi.project.Project) MemberInfo(com.intellij.refactoring.util.classMembers.MemberInfo) PsiMethod(com.intellij.psi.PsiMethod)

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