Search in sources :

Example 1 with MoveMemberListener

use of com.intellij.refactoring.listeners.MoveMemberListener in project intellij-community by JetBrains.

the class PullUpTest method doTest.

private void doTest(final boolean checkMembersMovedCount, String conflictMessage, RefactoringTestUtil.MemberDescriptor... membersToFind) {
    final MultiMap<PsiElement, String> conflictsMap = new MultiMap<>();
    configureByFile(BASE_PATH + getTestName(false) + ".java");
    PsiElement elementAt = getFile().findElementAt(getEditor().getCaretModel().getOffset());
    final PsiClass sourceClass = PsiTreeUtil.getParentOfType(elementAt, PsiClass.class);
    assertNotNull(sourceClass);
    PsiClass targetClass = sourceClass.getSuperClass();
    assertNotNull(targetClass);
    if (!targetClass.isWritable()) {
        final PsiClass[] interfaces = sourceClass.getInterfaces();
        assertEquals(1, interfaces.length);
        assertTrue(interfaces[0].isWritable());
        targetClass = interfaces[0];
    }
    final MemberInfo[] infos = RefactoringTestUtil.findMembers(sourceClass, membersToFind);
    final int[] countMoved = { 0 };
    final MoveMemberListener listener = (aClass, member) -> {
        assertEquals(sourceClass, aClass);
        countMoved[0]++;
    };
    JavaRefactoringListenerManager.getInstance(getProject()).addMoveMembersListener(listener);
    final PsiDirectory targetDirectory = targetClass.getContainingFile().getContainingDirectory();
    final PsiPackage targetPackage = targetDirectory != null ? JavaDirectoryService.getInstance().getPackage(targetDirectory) : null;
    conflictsMap.putAllValues(PullUpConflictsUtil.checkConflicts(infos, sourceClass, targetClass, targetPackage, targetDirectory, psiMethod -> PullUpProcessor.checkedInterfacesContain(Arrays.asList(infos), psiMethod)));
    final PullUpProcessor helper = new PullUpProcessor(sourceClass, targetClass, infos, new DocCommentPolicy(DocCommentPolicy.ASIS));
    helper.run();
    UIUtil.dispatchAllInvocationEvents();
    JavaRefactoringListenerManager.getInstance(getProject()).removeMoveMembersListener(listener);
    if (conflictMessage != null && conflictsMap.isEmpty()) {
        fail("Conflict was not detected");
    }
    if (conflictMessage == null && !conflictsMap.isEmpty()) {
        fail(conflictsMap.values().iterator().next());
    }
    if (conflictMessage != null && !IGNORE_CONFLICTS.equals(conflictMessage)) {
        TreeSet<String> conflicts = new TreeSet<>(conflictsMap.values());
        assertEquals(conflictMessage, conflicts.iterator().next());
        return;
    }
    if (checkMembersMovedCount) {
        assertEquals(countMoved[0], membersToFind.length);
    }
    checkResultByFile(BASE_PATH + getTestName(false) + "_after.java");
}
Also used : PullUpConflictsUtil(com.intellij.refactoring.memberPullUp.PullUpConflictsUtil) UIUtil(com.intellij.util.ui.UIUtil) LanguageLevel(com.intellij.pom.java.LanguageLevel) MemberInfo(com.intellij.refactoring.util.classMembers.MemberInfo) Arrays(java.util.Arrays) JavaTestUtil(com.intellij.JavaTestUtil) DocCommentPolicy(com.intellij.refactoring.util.DocCommentPolicy) PullUpProcessor(com.intellij.refactoring.memberPullUp.PullUpProcessor) TreeSet(java.util.TreeSet) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) MoveMemberListener(com.intellij.refactoring.listeners.MoveMemberListener) JavaRefactoringListenerManager(com.intellij.refactoring.listeners.JavaRefactoringListenerManager) com.intellij.psi(com.intellij.psi) NotNull(org.jetbrains.annotations.NotNull) MultiMap(com.intellij.util.containers.MultiMap) MoveMemberListener(com.intellij.refactoring.listeners.MoveMemberListener) MultiMap(com.intellij.util.containers.MultiMap) MemberInfo(com.intellij.refactoring.util.classMembers.MemberInfo) TreeSet(java.util.TreeSet) PullUpProcessor(com.intellij.refactoring.memberPullUp.PullUpProcessor) DocCommentPolicy(com.intellij.refactoring.util.DocCommentPolicy)

Aggregations

JavaTestUtil (com.intellij.JavaTestUtil)1 LanguageLevel (com.intellij.pom.java.LanguageLevel)1 com.intellij.psi (com.intellij.psi)1 PsiTreeUtil (com.intellij.psi.util.PsiTreeUtil)1 JavaRefactoringListenerManager (com.intellij.refactoring.listeners.JavaRefactoringListenerManager)1 MoveMemberListener (com.intellij.refactoring.listeners.MoveMemberListener)1 PullUpConflictsUtil (com.intellij.refactoring.memberPullUp.PullUpConflictsUtil)1 PullUpProcessor (com.intellij.refactoring.memberPullUp.PullUpProcessor)1 DocCommentPolicy (com.intellij.refactoring.util.DocCommentPolicy)1 MemberInfo (com.intellij.refactoring.util.classMembers.MemberInfo)1 MultiMap (com.intellij.util.containers.MultiMap)1 UIUtil (com.intellij.util.ui.UIUtil)1 Arrays (java.util.Arrays)1 TreeSet (java.util.TreeSet)1 NotNull (org.jetbrains.annotations.NotNull)1