Search in sources :

Example 1 with ExtractClassProcessor

use of com.intellij.refactoring.extractclass.ExtractClassProcessor in project intellij-community by JetBrains.

the class ExtractClassTest method testPublicVisibility.

public void testPublicVisibility() throws Exception {
    doTest((rootDir, rootAfter) -> {
        PsiClass aClass = myJavaFacade.findClass("Test", GlobalSearchScope.projectScope(myProject));
        assertNotNull("Class Test not found", aClass);
        final ArrayList<PsiMethod> methods = new ArrayList<>();
        methods.add(aClass.findMethodsByName("foos", false)[0]);
        final ArrayList<PsiField> fields = new ArrayList<>();
        fields.add(aClass.findFieldByName("myT", false));
        final ExtractClassProcessor processor = new ExtractClassProcessor(aClass, fields, methods, new ArrayList<>(), "", null, "Extracted", PsiModifier.PUBLIC, false, Collections.<MemberInfo>emptyList());
        processor.run();
        LocalFileSystem.getInstance().refresh(false);
        FileDocumentManager.getInstance().saveAllDocuments();
    });
}
Also used : PsiMethod(com.intellij.psi.PsiMethod) PsiField(com.intellij.psi.PsiField) PsiClass(com.intellij.psi.PsiClass) ArrayList(java.util.ArrayList) ExtractClassProcessor(com.intellij.refactoring.extractclass.ExtractClassProcessor)

Example 2 with ExtractClassProcessor

use of com.intellij.refactoring.extractclass.ExtractClassProcessor in project intellij-community by JetBrains.

the class ExtractClassTest method testPublicFieldDelegation.

public void testPublicFieldDelegation() throws Exception {
    doTest((rootDir, rootAfter) -> {
        PsiClass aClass = myJavaFacade.findClass("Test", GlobalSearchScope.projectScope(myProject));
        assertNotNull("Class Test not found", aClass);
        final ArrayList<PsiField> fields = new ArrayList<>();
        fields.add(aClass.findFieldByName("myT", false));
        ExtractClassProcessor processor = new ExtractClassProcessor(aClass, fields, new ArrayList<>(), new ArrayList<>(), "", "Extracted");
        processor.run();
        LocalFileSystem.getInstance().refresh(false);
        FileDocumentManager.getInstance().saveAllDocuments();
    });
}
Also used : PsiField(com.intellij.psi.PsiField) PsiClass(com.intellij.psi.PsiClass) ArrayList(java.util.ArrayList) ExtractClassProcessor(com.intellij.refactoring.extractclass.ExtractClassProcessor)

Example 3 with ExtractClassProcessor

use of com.intellij.refactoring.extractclass.ExtractClassProcessor 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)

Example 4 with ExtractClassProcessor

use of com.intellij.refactoring.extractclass.ExtractClassProcessor in project intellij-community by JetBrains.

the class ExtractClassTest method doTest.

private static void doTest(final PsiClass aClass, final ArrayList<PsiMethod> methods, final ArrayList<PsiField> fields, final String conflicts, boolean generateGettersSetters, boolean inner) {
    try {
        ExtractClassProcessor processor = new ExtractClassProcessor(aClass, fields, methods, new ArrayList<>(), StringUtil.getPackageName(aClass.getQualifiedName()), null, "Extracted", null, generateGettersSetters, Collections.<MemberInfo>emptyList());
        processor.setExtractInnerClass(inner);
        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 : TreeSet(java.util.TreeSet) ExtractClassProcessor(com.intellij.refactoring.extractclass.ExtractClassProcessor)

Example 5 with ExtractClassProcessor

use of com.intellij.refactoring.extractclass.ExtractClassProcessor in project intellij-community by JetBrains.

the class ExtractClassTest method doTestInnerClass.

private void doTestInnerClass() throws Exception {
    doTest((rootDir, rootAfter) -> {
        PsiClass aClass = myJavaFacade.findClass("Test", GlobalSearchScope.projectScope(myProject));
        assertNotNull("Class Test not found", aClass);
        final ArrayList<PsiClass> classes = new ArrayList<>();
        classes.add(aClass.findInnerClassByName("Inner", false));
        ExtractClassProcessor processor = new ExtractClassProcessor(aClass, new ArrayList<>(), new ArrayList<>(), classes, "", "Extracted");
        processor.run();
        LocalFileSystem.getInstance().refresh(false);
        FileDocumentManager.getInstance().saveAllDocuments();
    });
}
Also used : PsiClass(com.intellij.psi.PsiClass) ArrayList(java.util.ArrayList) ExtractClassProcessor(com.intellij.refactoring.extractclass.ExtractClassProcessor)

Aggregations

ExtractClassProcessor (com.intellij.refactoring.extractclass.ExtractClassProcessor)5 ArrayList (java.util.ArrayList)4 PsiClass (com.intellij.psi.PsiClass)3 PsiField (com.intellij.psi.PsiField)2 TreeSet (java.util.TreeSet)2 PsiMethod (com.intellij.psi.PsiMethod)1 MemberInfo (com.intellij.refactoring.util.classMembers.MemberInfo)1