Search in sources :

Example 6 with MemberInfo

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

the class PushDownMultifileTest method doTest.

private void doTest(final boolean fail, final String sourceClassName, final String targetClassName) throws Exception {
    try {
        doTest((rootDir, rootAfter) -> {
            final PsiClass srcClass = myJavaFacade.findClass(sourceClassName, GlobalSearchScope.allScope(myProject));
            assertTrue("Source class not found", srcClass != null);
            final PsiClass targetClass = myJavaFacade.findClass(targetClassName, GlobalSearchScope.allScope(myProject));
            assertTrue("Target class not found", targetClass != null);
            final PsiMethod[] methods = srcClass.getMethods();
            assertTrue("No methods found", methods.length > 0);
            final MemberInfo memberInfo = new MemberInfo(methods[0]);
            memberInfo.setChecked(true);
            new PushDownProcessor<>(srcClass, Collections.singletonList(memberInfo), new DocCommentPolicy(DocCommentPolicy.ASIS)).run();
        //LocalFileSystem.getInstance().refresh(false);
        //FileDocumentManager.getInstance().saveAllDocuments();
        });
    } catch (BaseRefactoringProcessor.ConflictsInTestsException e) {
        if (fail) {
            return;
        } else {
            throw e;
        }
    }
    if (fail) {
        fail("Conflict was not detected");
    }
}
Also used : MemberInfo(com.intellij.refactoring.util.classMembers.MemberInfo) PsiMethod(com.intellij.psi.PsiMethod) PsiClass(com.intellij.psi.PsiClass) DocCommentPolicy(com.intellij.refactoring.util.DocCommentPolicy)

Example 7 with MemberInfo

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

the class PushDownMultifileTest method testUsagesInXml.

public void testUsagesInXml() throws Exception {
    try {
        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 PsiField[] fields = srcClass.getFields();
            assertTrue("No methods found", fields.length > 0);
            final MemberInfo memberInfo = new MemberInfo(fields[0]);
            memberInfo.setChecked(true);
            new PushDownProcessor<>(srcClass, Collections.singletonList(memberInfo), new DocCommentPolicy(DocCommentPolicy.ASIS)).run();
        //LocalFileSystem.getInstance().refresh(false);
        //FileDocumentManager.getInstance().saveAllDocuments();
        });
    } catch (BaseRefactoringProcessor.ConflictsInTestsException e) {
        assertEquals(e.getMessage(), "Class <b><code>b.B</code></b> is package-private and will not be accessible from file <b><code>A.form</code></b>.");
        return;
    }
    fail("Conflict was not detected");
}
Also used : MemberInfo(com.intellij.refactoring.util.classMembers.MemberInfo) PsiField(com.intellij.psi.PsiField) PsiClass(com.intellij.psi.PsiClass) DocCommentPolicy(com.intellij.refactoring.util.DocCommentPolicy)

Example 8 with MemberInfo

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

the class RefactoringTestUtil method findMembers.

public static MemberInfo[] findMembers(final PsiClass sourceClass, final MemberDescriptor... membersToFind) {
    MemberInfo[] infos = new MemberInfo[membersToFind.length];
    for (int i = 0; i < membersToFind.length; i++) {
        final Class<? extends PsiMember> clazz = membersToFind[i].myClass;
        final String name = membersToFind[i].myName;
        PsiMember member = null;
        boolean overrides = false;
        PsiReferenceList refList = null;
        if (PsiClass.class.isAssignableFrom(clazz)) {
            member = sourceClass.findInnerClassByName(name, false);
            if (member == null) {
                final PsiClass[] supers = sourceClass.getSupers();
                for (PsiClass superTypeClass : supers) {
                    if (superTypeClass.getName().equals(name)) {
                        member = superTypeClass;
                        overrides = true;
                        refList = superTypeClass.isInterface() ? sourceClass.getImplementsList() : sourceClass.getExtendsList();
                        break;
                    }
                }
            }
        } else if (PsiMethod.class.isAssignableFrom(clazz)) {
            final PsiMethod[] methods = sourceClass.findMethodsByName(name, false);
            Assert.assertEquals(1, methods.length);
            member = methods[0];
        } else if (PsiField.class.isAssignableFrom(clazz)) {
            member = sourceClass.findFieldByName(name, false);
        }
        Assert.assertNotNull(member);
        infos[i] = new MemberInfo(member, overrides, refList);
        infos[i].setToAbstract(membersToFind[i].myAbstract);
    }
    return infos;
}
Also used : MemberInfo(com.intellij.refactoring.util.classMembers.MemberInfo)

Example 9 with MemberInfo

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

the class ExtractSuperClassTest method doTest.

private void doTest(@NonNls final String className, @NonNls final String newClassName, String[] conflicts, String targetPackageName, RefactoringTestUtil.MemberDescriptor... membersToFind) throws Exception {
    String rootBefore = getRoot() + "/before";
    PsiTestUtil.removeAllRoots(myModule, IdeaTestUtil.getMockJdk14());
    final VirtualFile rootDir = PsiTestUtil.createTestProjectStructure(myProject, myModule, rootBefore, myFilesToDelete);
    PsiClass psiClass = myJavaFacade.findClass(className, ProjectScope.getAllScope(myProject));
    assertNotNull(psiClass);
    final MemberInfo[] members = RefactoringTestUtil.findMembers(psiClass, membersToFind);
    doTest(members, newClassName, targetPackageName, rootDir, psiClass, conflicts);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) MemberInfo(com.intellij.refactoring.util.classMembers.MemberInfo)

Example 10 with MemberInfo

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

the class ExtractEnumTest method doTest.

private void doTest(final String conflicts, final boolean generateAccessors, final RefactoringTestUtil.MemberDescriptor... memberDescriptors) throws Exception {
    doTest((rootDir, rootAfter) -> {
        final PsiClass aClass = myJavaFacade.findClass("Test", GlobalSearchScope.projectScope(myProject));
        assertNotNull("Class Test not found", aClass);
        final ArrayList<PsiField> fields = new ArrayList<>();
        final ArrayList<PsiMethod> methods = new ArrayList<>();
        final List<MemberInfo> enumConstants = new ArrayList<>();
        for (MemberInfo memberInfo : RefactoringTestUtil.findMembers(aClass, memberDescriptors)) {
            final PsiMember member = memberInfo.getMember();
            if (member instanceof PsiField) {
                fields.add((PsiField) member);
                if (member.hasModifierProperty(PsiModifier.STATIC) && member.hasModifierProperty(PsiModifier.FINAL) && ((PsiField) member).hasInitializer()) {
                    if (memberInfo.isToAbstract()) {
                        enumConstants.add(memberInfo);
                        memberInfo.setChecked(true);
                    }
                }
            } else if (member instanceof PsiMethod) {
                methods.add((PsiMethod) member);
            }
        }
        try {
            final ExtractClassProcessor processor = new ExtractClassProcessor(aClass, fields, methods, new ArrayList<>(), "", null, "EEnum", null, generateAccessors, enumConstants);
            processor.run();
            LocalFileSystem.getInstance().refresh(false);
            FileDocumentManager.getInstance().saveAllDocuments();
        } catch (BaseRefactoringProcessor.ConflictsInTestsException e) {
            if (conflicts != null) {
                TreeSet expectedConflictsSet = new TreeSet(Arrays.asList(conflicts.split("\n")));
                TreeSet actualConflictsSet = new TreeSet(Arrays.asList(e.getMessage().split("\n")));
                Assert.assertEquals(expectedConflictsSet, actualConflictsSet);
                return;
            } else {
                fail(e.getMessage());
            }
        }
        if (conflicts != null) {
            fail("Conflicts were not detected: " + conflicts);
        }
    });
}
Also used : ArrayList(java.util.ArrayList) MemberInfo(com.intellij.refactoring.util.classMembers.MemberInfo) TreeSet(java.util.TreeSet) ExtractClassProcessor(com.intellij.refactoring.extractclass.ExtractClassProcessor)

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