Search in sources :

Example 1 with PullUpProcessor

use of com.intellij.refactoring.memberPullUp.PullUpProcessor in project intellij-community by JetBrains.

the class ExtractInterfaceHandler method extractInterface.

static PsiClass extractInterface(PsiDirectory targetDir, PsiClass aClass, String interfaceName, MemberInfo[] selectedMembers, DocCommentPolicy javaDocPolicy) throws IncorrectOperationException {
    final Project project = aClass.getProject();
    project.getMessageBus().syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).refactoringStarted(ExtractSuperClassUtil.REFACTORING_EXTRACT_SUPER_ID, ExtractSuperClassUtil.createBeforeData(aClass, selectedMembers));
    final PsiClass anInterface = JavaDirectoryService.getInstance().createInterface(targetDir, interfaceName);
    try {
        PsiJavaCodeReferenceElement ref = ExtractSuperClassUtil.createExtendingReference(anInterface, aClass, selectedMembers);
        final PsiReferenceList referenceList = aClass.isInterface() ? aClass.getExtendsList() : aClass.getImplementsList();
        assert referenceList != null;
        CodeStyleManager.getInstance(project).reformat(referenceList.add(ref));
        PullUpProcessor pullUpHelper = new PullUpProcessor(aClass, anInterface, selectedMembers, javaDocPolicy);
        pullUpHelper.moveMembersToBase();
        return anInterface;
    } finally {
        project.getMessageBus().syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).refactoringDone(ExtractSuperClassUtil.REFACTORING_EXTRACT_SUPER_ID, ExtractSuperClassUtil.createAfterData(anInterface));
    }
}
Also used : Project(com.intellij.openapi.project.Project) PullUpProcessor(com.intellij.refactoring.memberPullUp.PullUpProcessor)

Example 2 with PullUpProcessor

use of com.intellij.refactoring.memberPullUp.PullUpProcessor 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)

Example 3 with PullUpProcessor

use of com.intellij.refactoring.memberPullUp.PullUpProcessor in project intellij-community by JetBrains.

the class ExtractSuperClassUtil method extractSuperClass.

public static PsiClass extractSuperClass(final Project project, final PsiDirectory targetDirectory, final String superclassName, final PsiClass subclass, final MemberInfo[] selectedMemberInfos, final DocCommentPolicy javaDocPolicy) throws IncorrectOperationException {
    project.getMessageBus().syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).refactoringStarted(REFACTORING_EXTRACT_SUPER_ID, createBeforeData(subclass, selectedMemberInfos));
    final PsiClass superclass = JavaDirectoryService.getInstance().createClass(targetDirectory, superclassName);
    try {
        final PsiModifierList superClassModifierList = superclass.getModifierList();
        assert superClassModifierList != null;
        superClassModifierList.setModifierProperty(PsiModifier.FINAL, false);
        final PsiReferenceList subClassExtends = subclass.getExtendsList();
        if (subClassExtends != null) {
            copyPsiReferenceList(subClassExtends, superclass.getExtendsList());
        } else if (subclass instanceof PsiAnonymousClass) {
            PsiJavaCodeReferenceElement classReference = ((PsiAnonymousClass) subclass).getBaseClassReference();
            PsiElement baseClass = classReference.resolve();
            if (baseClass instanceof PsiClass && ((PsiClass) baseClass).isInterface()) {
                superclass.getImplementsList().add(classReference);
            } else {
                superclass.getExtendsList().add(classReference);
            }
        }
        // create constructors if neccesary
        PsiMethod[] constructors = getCalledBaseConstructors(subclass);
        if (constructors.length > 0) {
            createConstructorsByPattern(project, superclass, constructors);
        }
        // clear original class' "extends" list
        if (subClassExtends != null) {
            clearPsiReferenceList(subclass.getExtendsList());
        }
        // make original class extend extracted superclass
        PsiJavaCodeReferenceElement ref = createExtendingReference(superclass, subclass, selectedMemberInfos);
        final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
        if (subClassExtends != null) {
            codeStyleManager.reformat(subclass.getExtendsList().add(ref));
        } else if (subclass instanceof PsiAnonymousClass) {
            codeStyleManager.reformat(((PsiAnonymousClass) subclass).getBaseClassReference().replace(ref));
        }
        PullUpProcessor pullUpHelper = new PullUpProcessor(subclass, superclass, selectedMemberInfos, javaDocPolicy);
        pullUpHelper.moveMembersToBase();
        pullUpHelper.moveFieldInitializations();
        Collection<MethodSignature> toImplement = OverrideImplementExploreUtil.getMethodSignaturesToImplement(superclass);
        if (!toImplement.isEmpty()) {
            superClassModifierList.setModifierProperty(PsiModifier.ABSTRACT, true);
        }
        return superclass;
    } finally {
        project.getMessageBus().syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).refactoringDone(REFACTORING_EXTRACT_SUPER_ID, createAfterData(superclass));
    }
}
Also used : CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager) MethodSignature(com.intellij.psi.util.MethodSignature) PullUpProcessor(com.intellij.refactoring.memberPullUp.PullUpProcessor)

Example 4 with PullUpProcessor

use of com.intellij.refactoring.memberPullUp.PullUpProcessor in project intellij-community by JetBrains.

the class PullAsAbstractUpFix method pullUp.

private static void pullUp(PsiMethod method, PsiClass containingClass, PsiClass baseClass) {
    if (!FileModificationService.getInstance().prepareFileForWrite(baseClass.getContainingFile()))
        return;
    final MemberInfo memberInfo = new MemberInfo(method);
    memberInfo.setChecked(true);
    memberInfo.setToAbstract(true);
    new PullUpProcessor(containingClass, baseClass, new MemberInfo[] { memberInfo }, new DocCommentPolicy(DocCommentPolicy.ASIS)).run();
}
Also used : MemberInfo(com.intellij.refactoring.util.classMembers.MemberInfo) PullUpProcessor(com.intellij.refactoring.memberPullUp.PullUpProcessor) DocCommentPolicy(com.intellij.refactoring.util.DocCommentPolicy)

Example 5 with PullUpProcessor

use of com.intellij.refactoring.memberPullUp.PullUpProcessor 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

PullUpProcessor (com.intellij.refactoring.memberPullUp.PullUpProcessor)6 DocCommentPolicy (com.intellij.refactoring.util.DocCommentPolicy)3 MemberInfo (com.intellij.refactoring.util.classMembers.MemberInfo)3 JavaTestUtil (com.intellij.JavaTestUtil)2 com.intellij.psi (com.intellij.psi)2 PullUpConflictsUtil (com.intellij.refactoring.memberPullUp.PullUpConflictsUtil)2 MultiMap (com.intellij.util.containers.MultiMap)2 Arrays (java.util.Arrays)2 NotNull (org.jetbrains.annotations.NotNull)2 Project (com.intellij.openapi.project.Project)1 LanguageLevel (com.intellij.pom.java.LanguageLevel)1 CodeStyleManager (com.intellij.psi.codeStyle.CodeStyleManager)1 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)1 MethodSignature (com.intellij.psi.util.MethodSignature)1 PsiTreeUtil (com.intellij.psi.util.PsiTreeUtil)1 JavaRefactoringListenerManager (com.intellij.refactoring.listeners.JavaRefactoringListenerManager)1 MoveMemberListener (com.intellij.refactoring.listeners.MoveMemberListener)1 UIUtil (com.intellij.util.ui.UIUtil)1 HashSet (java.util.HashSet)1 TreeSet (java.util.TreeSet)1